Android SQLite 插入错误

标签 android eclipse sqlite

我收到一个我无法弄清楚的错误。希望您能帮忙!

我正在制作一个组名称表,其唯一 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。 希望有人看到我的错误。

好的,我将 group 更改为 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/

相关文章:

c# - 在 sqlite 中更新数据

android - LayoutInflater 类有什么作用? (在安卓中)

android - GCM 是否需要 google play services 应用程序才能工作?

java - 有人使用 Eclipse/Maven 的 JRuby 测试框架吗?

eclipse - Eclipse-> Glassfish将不会增量部署-JBOSS可以

c++ - ODB C++实例中sqlite数据库文件存放在哪里

java - 如何将多个功能添加到一个命令中?

Android - 测量通过 Intent 打开 Activity 与收到结果之间的时间

eclipse - 工作区中的项目未显示在项目 -> clean 下

python - 如何将 Python 字典值保存到 SQLite 模型?