2012-04-02 159 views
0

當我嘗試創建一個表中的第一次,我總是收到此錯誤信息:的Android創建表錯誤

sqlite的返回:錯誤碼= 1,味精= AUTOINCREMENT只允許 上INTEGER PRIMARY KEY

我的代碼如下所示:

// debuggin標識 私有靜態最後絃樂TAG = DatabaseOpenHandler.class.getSimpleName();

// name and verison of the database 
private static final String DATABASE_NAME = "OpenConfApp"; 
private static final int DATABASE_VERSION = 1; 

// name and attributes of the table notes 
public static final String TABLE_NAME_NOTES = "notes"; 
public static final String _NOTE_ID = "_noteId"; 
public static final String NOTE_CONFERENCE = "conferenceId"; 
public static final String NOTE_DATE = "dateInMillis"; 
public static final String NOTE_TEXT = "noteText"; 

// create table notes 
public static final String TABLE_NOTES_CREATE = "CREATE TABLE " 
     + TABLE_NAME_NOTES + " (" + _NOTE_ID 
     + "INTEGER PRIMARY KEY AUTOINCREMENT, " + NOTE_CONFERENCE 
     + " INTEGER, " + NOTE_DATE + " INTEGER, " + NOTE_TEXT 
     + " INTEGER);"; 

// drop table notes 
public static final String TABLE_NOTES_DROP = "DROP TABLE IF EXIST " 
     + TABLE_NAME_NOTES; 

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

@Override 
public void onCreate(SQLiteDatabase db) { 
    Log.d(TAG, "onCreate called"); 
    db.execSQL(TABLE_NOTES_CREATE); 
} 

對不起,我是Android編程新手。

回答

1

你的代碼是正確的只有你的第一個整數statement.Write之前錯過了空間這樣的:

public static final String TABLE_NOTES_CREATE = "CREATE TABLE " 
    + TABLE_NAME_NOTES + " (" + _NOTE_ID 
    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NOTE_CONFERENCE 
    + " INTEGER, " + NOTE_DATE + " INTEGER, " + NOTE_TEXT 
    + " INTEGER);"; 
+0

你爲什麼要重複我嗎? -_- – Joe 2012-04-02 12:25:39

+0

@ sebbl.sche醜聞。我是第一個給出這個答案的。 – Joe 2012-04-02 12:54:31

+0

sry親愛的,但我真的沒有看到你的答案,因爲我的網速慢。真的是sry。 – Dhruvisha 2012-04-02 13:11:59

0

整數主鍵不需要AUTOINCREMENT,它的語義已經包含在「主鍵」中,請參閱here

1
+ "INTEGER PRIMARY KEY AUTOINCREMENT, " + NOTE_CONFERENCE 

你錯過由於打字錯誤 那邊一片空白......

+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + NOTE_CONFERENCE 
0

這是一個問題,有

NOTE_ID和INTEGER PRIMARY KEY AUTOINCREMENT

之間沒有空格
public static final String TABLE_NOTES_CREATE = "CREATE TABLE " 
     + TABLE_NAME_NOTES + " (" + _NOTE_ID 
     + "INTEGER PRIMARY KEY AUTOINCREMENT, " + NOTE_CONFERENCE 
     + " INTEGER, " + NOTE_DATE + " INTEGER, " + NOTE_TEXT 
     + " INTEGER);"; 

你應該給他們之間的空間

public static final String TABLE_NOTES_CREATE = "CREATE TABLE " 
     + TABLE_NAME_NOTES + " (" + _NOTE_ID 
     + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NOTE_CONFERENCE 
     + " INTEGER, " + NOTE_DATE + " INTEGER, " + NOTE_TEXT 
     + " INTEGER);";