创建SQLite数据库时出现运行时异常

时间:2015-07-18 17:00:30

标签: android database sqlite

有谁能告诉我什么是错的。以前它适用于一个表“书”,但现在我正在创建一个包含3个表的新数据库,但它没有执行。 这是辅助类。

public class DatabaseHelper extends SQLiteOpenHelper{

    private static final String DATABASE_NAME="MyDatabse1.db";
    public DatabaseHelper(Context context)
    {
        super(context,DATABASE_NAME,null,1);
    }
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL("CREATE TABLE userdetails (_id INTEGER AUTOINCREMENT, username TEXT UNIQUE, password TEXT, name TEXT, phonenumber TEXT, address TEXT, fine INTEGER, bookid INTEGER, PRIMARY KEY (_id, username, password));");
        db.execSQL("CREATE TABLE issue (_id INTEGER AUTOINCREMENT, book_id INTEGER UNIQUE, username TEXT, issue_date DATE, return_date DATE, PRIMARY KEY (_id, username, book_id));");
        db.execSQL("CREATE TABLE bookstable (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, author TEXT, category TEXT, availability TEXT);");
}
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)     {

        db.execSQL("DROP TABLE IF EXISTS userdetails");
        db.execSQL("DROP TABLE IF EXISTS issue");
        db.execSQL("DROP TABLE IF EXISTS bookstable");

        onCreate(db);

        }

这是堆栈跟踪

07-18 12:47:22.686: E/SQLiteLog(3423): (1) near "AUTOINCREMENT": syntax error
07-18 12:47:22.916: E/AndroidRuntime(3423): FATAL EXCEPTION: main
07-18 12:47:22.916: E/AndroidRuntime(3423): java.lang.IllegalStateException:     Could not execute method of the activity

07-18 12:47:22.916: E/AndroidRuntime(3423): Caused by: android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error  (code 1): , while compiling: CREATE TABLE userdetails (_id INTEGER AUTOINCREMENT, username TEXT UNIQUE, password TEXT, name TEXT, phonenumber TEXT, address TEXT, fine INTEGER, bookid INTEGER, PRIMARY KEY (_id, username, password));
07-18 12:47:22.916: E/AndroidRuntime(3423):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-18 12:47:22.916: E/AndroidRuntime(3423):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
07-18 12:47:22.916: E/AndroidRuntime(3423):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
07-18 12:47:22.916: E/AndroidRuntime(3423):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
07-18 12:47:22.916: E/AndroidRuntime(3423):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
07-18 12:47:22.916: E/AndroidRuntime(3423):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
07-18 12:47:22.916: E/AndroidRuntime(3423):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
07-18 12:47:22.916: E/AndroidRuntime(3423):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
07-18 12:47:22.916: E/AndroidRuntime(3423):     at com.example.libraryplus.DatabaseHelper.onCreate(DatabaseHelper.java:26)
07-18 12:47:22.916: E/AndroidRuntime(3423):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
07-18 12:47:22.916: E/AndroidRuntime(3423):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
07-18 12:47:22.916: E/AndroidRuntime(3423):     at com.example.libraryplus.DatabaseHelper.add_books(DatabaseHelper.java:53)
07-18 12:47:22.916: E/AndroidRuntime(3423):     at com.example.libraryplus.AddBooksActivity.addNewBooks(AddBooksActivity.java:42)

1 个答案:

答案 0 :(得分:1)

AUTOINCREMENT列必须是PRIMARY KEY。

无法使用自动增量列和多列主键。