我得到一个错误我找不出来。希望你能帮助!Android SQLite插入错误
我正在制作一个具有唯一标识的组名表,这是主键。
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(?, ?);
干杯! Kat
我只是告诉你,对于数据库表的主键,您应该始终使用标识符“_id”,因为某些Android功能依赖于此标准。在你的查询中,你总是可以说“ID为_id”,但仍然... – hovanessyan