SQLiteException:插入数据库时​​出错:语法错误(代码1)

时间:2013-10-22 01:41:06

标签: android sql android-sqlite sqliteopenhelper sqlite

我认为错误在于我设置DATABASE_CREATE的方式。我在这里错过了什么吗?我得到SQLiteException,其中long id = -1。如果我提供足够的细节,请告诉我。我尝试过设置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();

1 个答案:

答案 0 :(得分:5)

group是一个SQLite关键字。请参阅:http://www.sqlite.org/lang_keywords.html

将对“group”的所有引用更改为“mgroup”