gpt4 book ai didi

android - ContentValues 对表的字段进行了错误排序,为什么?以及如何解决?

转载 作者:搜寻专家 更新时间:2023-10-30 20:42:24 24 4
gpt4 key购买 nike

大家早上好;

我正在尝试在 android 中创建一个数据库类,它已经完成,我也成功地创建了一个表。当我想使用 ContentValues 向表中插入行时,问题就来了,它会尝试以与表字段不同的顺序插入行,这会导致 logcat 中出现错误:没有这样的表,如下所示:

这些是我的变量:

    private static final String row_ID="RID";
private static final String row_NAME="NAME";
private static final String row_EMAIL="EMAIL";
private static final String row_WEBSITE="WEBSITE";
private static final String row_TELEPHONE1="PHONE_NUMBER1";
private static final String row_TELEPHONE2="PHONE_NUMBER2";
private static final String row_TELEPHONE3="PHONE_NUMBER3";
private static final String row_TELEPHONE4="PHONE_NUMBER4";
private static final String row_TELEPHONE5="PHONE_NUMBER5";
private static final String db_NAME="ContactsDb";
private static final String tab_NAME="ContactsTab";
private static final int db_VERSION=2;
private dbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

我的 table :

    String CREATE_TABLE="CREATE TABLE " + db_NAME + " ("
+ row_ID + " INT PRIMARY KEY AUTOINCREMENT,"
+ row_NAME + " TEXT, "
+ row_EMAIL + " TEXT, "
+ row_WEBSITE + " TEXT, "
+ row_TELEPHONE1 + " TEXT, "
+ row_TELEPHONE2 + " TEXT, "
+ row_TELEPHONE3 + " TEXT, "
+ row_TELEPHONE4 + " TEXT, "
+ row_TELEPHONE5 + " TEXT);";
db_name.execSQL(CREATE_TABLE);

使用 ContentValues 插入行:

    public long createEntry(String name, String email, String website, String telephone1, String telephone2, String telephone3, String telephone4, String telephone5)
{
ContentValues cv = new ContentValues ();

Log.v("STATUS", "getting input data..");

cv.put(row_NAME, name);
cv.put(row_EMAIL, email);
cv.put(row_WEBSITE, website);
cv.put(row_TELEPHONE1, telephone1);
cv.put(row_TELEPHONE2, telephone2);
cv.put(row_TELEPHONE3, telephone3);
cv.put(row_TELEPHONE4, telephone4);
cv.put(row_TELEPHONE5, telephone5);
Log.v("STATUS", "got input data, inserting data....");

return ourDatabase.insert(tab_NAME, null, cv);
}

因此,当我运行此应用程序时,它会成功执行所有这些操作,直到它到达 ourDatabase.insert(tab_NAME, null, cv); 它显示错误:

    SQLiteLog            (1) no such table: ContactsTab
SQLiteDatabase Error inserting Name=Ahlam M. Hussain PHONE_NUMBER5=
PHONE_NUMBER4= PHONE_NUMBER3= PHONE_NUMBER2=059999999
PHONE_NUMBER1= 09299999 EMAIL=ahlam@ahlam.com EWEBSITE=
SQLiteDatabase android.database.sqlite.SQLiteException: no such table:
ContactsTab (code 1): ,while compiling: INSERT INTO
ContactsTab(NAME, PHONE_NUMBER5 PHONE_NUMBER4 PHONE_NUMBER3
PHONE_NUMBER2 PHONE_NUMBER1, EMAIL, WEBSITE) VALUES
(?,?,?,?,?,?,?,?)

你可以注意到它试图在不同的字段中插入行 orderm 因此它无法识别表格..

那怎么办???我真的受够了..

最佳答案

你忘了戴上大括号

  • row_TELEPHONE5 + "TEXT;";

必须是

  • row_TELEPHONE5 + "TEXT);";

编辑

尝试使用“CREATE TABLE IF NOT EXISTS”和“INTEGER PRIMARY KEY AUTOINCREMENT”而不是“INT PRIMARY KEY AUTOINCREMENT”。此外,您在 CREATE-TABLE 字符串中设置了 db_name 而不是 table_name。将其更改为您的表名:

     String CREATE_TABLE="CREATE TABLE IF NOT EXISTS " + tab_NAME + " ("
+ row_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ row_NAME + " TEXT, "
+ row_EMAIL + " TEXT, "
+ row_WEBSITE + " TEXT, "
+ row_TELEPHONE1 + " TEXT, "
+ row_TELEPHONE2 + " TEXT, "
+ row_TELEPHONE3 + " TEXT, "
+ row_TELEPHONE4 + " TEXT, "
+ row_TELEPHONE5 + " TEXT);";
db_name.execSQL(CREATE_TABLE);

关于android - ContentValues 对表的字段进行了错误排序,为什么?以及如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13988013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com