gpt4 book ai didi

Android SQLite 插入错误

转载 作者:搜寻专家 更新时间:2023-11-01 07:37:53 28 4
gpt4 key购买 nike

我遇到了一个我无法弄清楚的错误。希望对您有所帮助!

我正在制作一个组名称表,其中唯一的 ID 是主键。

public class GroupDatabaseAdapter {

public static final String KEY_ID = "id";
public static final String KEY_NAME = "name";
private static final String DATABASE_TABLE = "groep";
private Context context;
private SQLiteDatabase database;
private GroupDatabaseHandler dbHandler;

public GroupDatabaseAdapter(Context context) {
this.context = context;
}

public GroupDatabaseAdapter open() throws SQLException {
dbHandler = new GroupDatabaseHandler(context);
database = dbHandler.getWritableDatabase();
return this;
}

public void close() {
dbHandler.close();
}

/**
* Create a new group If the group is successfully created return the new
* rowId for that note, otherwise return a -1 to indicate failure.
*/
public long createGroup(long id, String name)
{
ContentValues values = createContentValues(id, name);

return database.insert(DATABASE_TABLE, null, values);
}
/**
* Return a Cursor over the list of all groups in the database
*
* @return Cursor over all groups
*/
public Cursor fetchAllGroups() {
return database.query(DATABASE_TABLE, new String[] { KEY_ID,
KEY_NAME }, null, null, null,
null, null);
}

private ContentValues createContentValues(Long id, String name) {
ContentValues values = new ContentValues();
values.put(KEY_ID, id);
values.put(KEY_NAME, name);
return values;
}
}



public class GroupDatabaseHandler extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "groups.db";

private static final int DATABASE_VERSION = 1;

// Database creation sql statement
private static final String DATABASE_CREATE = "CREATE TABLE lesson (id INTEGER PRIMARY KEY NOT NULL , name TEXT NOT NULL);";

public GroupDatabaseHandler(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase database)
{
database.execSQL(DATABASE_CREATE);
}

}

我使用以下代码从另一个类调用这些类:

GroupDatabaseAdapter groupDb = new GroupDatabaseAdapter(this).open();
long id = groupDb.createGroup(641, "3 bac group 2");
System.out.println(id);
id = groupDb.createGroup(642, "3 bac group 3");
System.out.println(id);

我得到的错误是

11-24 23:08:07.036: I/Database(11436): sqlite returned: error code = 1, msg = near "group": syntax error
11-24 23:08:07.075: E/Database(11436): Error inserting id=1 name=3 bac group 2
11-24 23:08:07.075: E/Database(11436): android.database.sqlite.SQLiteException: near "group": syntax error: , while compiling: INSERT INTO group(id, name) VALUES(?, ?);

在同一个程序中,我制作了另一个有效的表格。该表与该表之间的唯一区别在于主键。在另一个表中它是自动递增的,这里我希望 rowid 是组的 id。希望有人看到我的错误。

好的,我将组更改为 groep(组的荷兰语),现在我收到以下错误:

11-25 00:09:10.812: I/Database(457): sqlite returned: error code = 1, msg = no such table: groep
11-25 00:09:11.082: E/Database(457): Error inserting _id=1 name=3 bac group 2
11-25 00:09:11.082: E/Database(457): android.database.sqlite.SQLiteException: no such table: groep: , while compiling: INSERT INTO groep(_id, name) VALUES(?, ?);

干杯!吉

最佳答案

只是一个预感:尝试将数据库表重命名为“group”以外的名称(例如“groups”)。我很确定“group”是 SQL 中的保留关键字。

关于Android SQLite 插入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8263037/

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