【问题标题】:Android SQLite INSERT errorAndroid SQLite 插入错误
【发布时间】:2011-11-24 22:25:02
【问题描述】:

我遇到了一个我无法弄清楚的错误。希望你能帮忙!

我正在制作一个组名表,其中包含一个唯一 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(荷兰语为 group),现在我收到以下错误:

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(?, ?);

干杯! 凯特

【问题讨论】:

  • 我只是建议您,对于数据库表的主键,您应该始终使用标识符“_id”,因为某些 Android 功能依赖于此标准。在您的查询中,您总是可以说“id as _id”,但仍然......

标签: android eclipse sqlite


【解决方案1】:

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

【讨论】:

  • 我修改了代码,现在又出现了一个错误。我编辑了我的问题。
  • 对,那是因为onCreate(SQLiteDatabase database) 没有运行。转到应用程序(在您的手机/模拟器中的设置下)并卸载您的应用程序。这将删除以前版本的数据库。
  • 我只是在安卓模拟器上运行它。我已经认为我必须重置程序或数据。但我找不到怎么做。我尝试删除数据库文件,但仍然无法正常工作。
【解决方案2】:

您创建了一个名为 course 的表并插入到一个名为 groep 的表中。

【讨论】:

  • 哦,这太愚蠢了!从我自己的代码中复制粘贴。谢谢!它现在正在工作!
猜你喜欢
  • 2015-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多