0

我正在尝试使用 1 个表创建简单的数据库并将 activityandroid 用作它的 ORM。但是在开始项目后,我有这样的错误:

 03-30 17:41:01.379: INFO/Database(345): sqlite returned: error code = 1, msg = duplicate column name: id
    03-30 17:41:01.379: ERROR/Database(345): Failure 1 (duplicate column name: id) on 0x1e2140 when preparing 'CREATE TABLE IF NOT EXISTS Quote (Id INTEGER PRIMARY KEY AUTOINCREMENT, author TEXT, id INTEGER, text TEXT);'.
    03-30 17:41:01.398: DEBUG/AndroidRuntime(345): Shutting down VM
    03-30 17:41:01.398: WARN/dalvikvm(345): threadid=1: thread exiting with uncaught exception (group=0x40015560)
    03-30 17:41:01.418: ERROR/AndroidRuntime(345): FATAL EXCEPTION: main
            java.lang.RuntimeException: Unable to create application com.activeandroid.app.Application: android.database.sqlite.SQLiteException: duplicate column name: id: CREATE TABLE IF NOT EXISTS Quote (Id INTEGER PRIMARY KEY AUTOINCREMENT, author TEXT, id INTEGER, text TEXT);
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3275)
            at android.app.ActivityThread.access$2200(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
            Caused by: android.database.sqlite.SQLiteException: duplicate column name: id: CREATE TABLE IF NOT EXISTS Quote (Id INTEGER PRIMARY KEY AUTOINCREMENT, author TEXT, id INTEGER, text TEXT);
            at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)
            at com.activeandroid.DatabaseHelper.createTable(DatabaseHelper.java:138)
            at com.activeandroid.DatabaseHelper.onCreate(DatabaseHelper.java:51)
            at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
            at com.activeandroid.Registry.openDatabase(Registry.java:149)
            at com.activeandroid.Registry.initialize(Registry.java:107)
            at com.activeandroid.ActiveAndroid.initialize(ActiveAndroid.java:8)
            at com.activeandroid.app.Application.onCreate(Application.java:9)
            at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:969)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3272)
            ... 10 more

我什至在任何地方都没有这样的代码。我的 SQL 代码:

public class QuoteDatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME = "DB1";
    private static final String TABLE_NAME = "Quote";
    private static final String KEY_ID = "id";
    private static final String KEY_TEXT = "text";
    private static final String KEY_AUTHOR = "author";

    public QuoteDatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createQuery = "CREATE TABLE " + TABLE_NAME +
                "(" +
                KEY_ID + " INTEGER PRIMARY KEY," +
                KEY_TEXT + " TEXT," +
                KEY_AUTHOR + " TEXT" +
                ")";
        db.execSQL(createQuery);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

一些建议为什么会这样?我试图调试它,但在创建数据库之前它失败了..

4

1 回答 1

1

Active Android 将尝试为您创建表格。我认为问题出在这样一个事实,即活动的 android 会在创建的每个表中自动创建一个名为“Id”的列。您的代码还有一个名为 id 的列。

看这里:

duplicate column name: id: CREATE TABLE IF NOT EXISTS Quote
  (Id INTEGER PRIMARY KEY AUTOINCREMENT, author TEXT, id INTEGER, text TEXT)

有 Id 整数主键和 id 整数。

于 2013-05-16T19:38:04.053 回答