我相信错误在于我设置 DATABASE_CREATE 的方式。我在这里错过了什么吗?我收到长 id = -1 的 SQLiteException。如果我提供了足够的详细信息,请告诉我。我试过设置 KEY_ID = "_id";
E/SQLiteLog(2385): (1) near "group": syntax error
E/SQLiteDatabase(2385): Error inserting group=demo
E/SQLiteDatabase(2385): android.database.sqlite.SQLiteException: near "group": syntax error (code 1): , while compiling: INSERT INTO groups(group) VALUES (?)
LOG(2385): Inserting record...
LOG(2385): mGroupName = demo
LOG(2385): long id = -1
DBAdapter.java
public static final String KEY_ID = "id";
public static final String KEY_GROUP_NAME = "group";
public static final String TAG = "DBAdapter";
public static final String DATABASE_NAME = "GroupsDB";
public static final String DATABASE_TABLE = "groups";
public static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table if not exists groups (id integer primary key autoincrement, group VARCHAR not null);";
. . .
//---insert a record into the database
public long insertRecord(String group) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_GROUP_NAME, group);
return db.insert(DATABASE_TABLE, null, initialValues);
}
MainActivity.java
EditText groupNameText = (EditText) findViewById(R.id.groupNameEditText);
String mGroupname = groupNameText.getText().toString();
...
try {
String destPath = "/data/data/" + getPackageName() + "/databases/GroupsDB";
File f = new File(destPath);
if(!f.exists()) {
CopyDB( getBaseContext().getAssets().open("mydb"),
new FileOutputStream(destPath));
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
DBAdapter db = new DBAdapter(this);
//try hard coding the record here, if unable to insertRecord for any reason it will return -1
db.open();
long id = db.insertRecord(mGroupname);
Log.i(TAG, "Inserting record...");
Log.i(TAG, "mGroupName = " + mGroupname);
Log.i(TAG, "long id = " + id);
db.close();