在数据库上插入数据时出错

时间:2014-06-04 13:28:58

标签: android sqlite

我正在尝试在本地数据库中保存一些值。此数据库除de ID列外还有3列,分别为:NAME,GROUP,ORDER。

public class TravelOrderDatabaseHelper extends SQLiteOpenHelper {
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
                TravelOrder._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                TravelOrder.NAME + " TEXT NOT NULL, " +
                TravelOrder.GROUP + " TEXT NOT NULL, " +
                TravelOrder.ORDER + " TEXT NOT NULL " +
                ");");
    }

-

public class TravelOrderProvider extends ContentProvider {
    public class TravelOrder implements BaseColumns {
        public static final String NAME = "name";
        public static final String GROUP = "group";
        public static final String ORDER = "order";
    }

在主要活动中,保存db中的值我使用此方法:

private void insertTraveler(String name, String group, String order){
    ContentValues newValues = new ContentValues();
    newValues.put(TravelOrder.NAME, name);
    newValues.put(TravelOrder.GROUP, group);
    newValues.put(TravelOrder.ORDER, order);

    getContentResolver().insert(TravelOrderProvider.CONTENT_URI, newValues);
}

但这就是我在LogCat上的内容:

06-04 15:21:46.919: E/AndroidRuntime(30381): FATAL EXCEPTION: main
06-04 15:21:46.919: E/AndroidRuntime(30381): android.database.sqlite.SQLiteException: near "group": syntax error: , while compiling: CREATE TABLE travelorder (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT NOT NULL, group TEXT NOT NULL, order TEXT NOT NULL );
06-04 15:21:46.919: E/AndroidRuntime(30381):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:134)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:2006)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1946)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at com.irvvin.carpooling.data.TravelOrderDatabaseHelper.onCreate(TravelOrderDatabaseHelper.java:29)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:165)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at com.irvvin.carpooling.data.TravelOrderProvider.insert(TravelOrderProvider.java:73)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at android.content.ContentProvider$Transport.insert(ContentProvider.java:200)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at android.content.ContentResolver.insert(ContentResolver.java:734)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at com.irvvin.carpooling.NewTravelActivity.insertTraveler(NewTravelActivity.java:429)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at com.irvvin.carpooling.NewTravelActivity.sendTravelersToOrder(NewTravelActivity.java:243)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at com.irvvin.carpooling.NewTravelActivity.access$1(NewTravelActivity.java:238)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at com.irvvin.carpooling.NewTravelActivity$1.onClick(NewTravelActivity.java:145)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at android.view.View.performClick(View.java:3549)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at android.view.View$PerformClick.run(View.java:14393)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at android.os.Handler.handleCallback(Handler.java:605)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at android.os.Looper.loop(Looper.java:154)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at android.app.ActivityThread.main(ActivityThread.java:4944)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at java.lang.reflect.Method.invokeNative(Native Method)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at java.lang.reflect.Method.invoke(Method.java:511)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-04 15:21:46.919: E/AndroidRuntime(30381):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:3)

您不能将group用作表名,因为它是在sqlite的关键字中内置的..使用不同的关键字

答案 1 :(得分:3)

grouporder都是Sqlite的关键词。您不能在此处将它们用作列名。更改

的值
public static final String GROUP = "group"; // "group_column"
public static final String ORDER = "order"; // "order_colomn"

请参阅SQLite Keywords

列表

答案 2 :(得分:2)

您使用的是sqlite关键字作为列名,例如grouporder这是限制

sqliteKeyword

了解详情