Android创建表错误

时间:2012-04-02 11:38:57

标签: android sql

当我第一次尝试创建表时,我总是收到此错误消息:

  

sqlite返回:错误代码= 1,msg =仅允许AUTOINCREMENT   在INTEGER PRIMARY KEY

我的代码如下所示:

// debuggin标识符     private static final String 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编程的新手。

4 个答案:

答案 0 :(得分:1)

+ "INTEGER PRIMARY KEY AUTOINCREMENT, " + NOTE_CONFERENCE 
你错过了那边的空白......

+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + NOTE_CONFERENCE

答案 1 :(得分:1)

您的代码是正确的只是您在第一个整数语句之前错过了一个空格。写下:

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);";

答案 2 :(得分:0)

整数主键不需要AUTOINCREMENT,它的语义已包含在“主键”中,请参阅here

答案 3 :(得分: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);";