Android Sqlite表有多个主键

时间:2014-08-31 05:30:43

标签: android sqlite android-sqlite

以下代码是我的数据库架构,运行onCreate方法后,我收到此错误:

08-28 08:13:23.984    3214-3214/ir.tsms E/Database﹕ Failure 1 (table "ReceiveFields" has more than one primary key) on 0x1175f8 when preparing 'CREATE TABLE ReceiveFields(id INTEGER PRIMARY KEY   AUTOINCREMENT, lastId INTEGER  , smsNumber VARCHAR  , mobileNumber VARCHAR , senderName VARCHAR , smsBody TEXT , receiveDate VARCHAR , PRIMARY KEY (id));'.

DataBase创建架构变量:

private static final String DATABASE_CREATE = "CREATE TABLE " + RECEIVE_FIELDS_TABLE + "("
        + COLUMN_ID +            " INTEGER PRIMARY KEY   AUTOINCREMENT, "
        + COLUMN_LASTID +        " INTEGER  , "
        + COLUMN_SMSNUMBER +     " VARCHAR  , "
        + COLUMN_MOBILENUMBER +  " VARCHAR , "
        + COLUMN_SENDERNAME +    " VARCHAR , "
        + COLUMN_SMSBODY +       " TEXT , "
        + COLUMN_RECEIVEDATE +   " VARCHAR , PRIMARY KEY (" + COLUMN_ID + "));";

Log.i DATABASE_CREATE的结果:

CREATE TABLE ReceiveFields(

    id INTEGER PRIMARY KEY   AUTOINCREMENT, 
    lastId INTEGER  , 
    smsNumber VARCHAR  , 
    mobileNumber VARCHAR , 
    senderName VARCHAR , 
    smsBody TEXT , 
    receiveDate VARCHAR , 
    PRIMARY KEY (id)

);

我的创建表问题是什么?感谢

1 个答案:

答案 0 :(得分:0)

PRIMARY KEY (" + COLUMN_ID + ")查询字符串的末尾删除DATABASE_CREATE 将您的代码更改为:

private static final String DATABASE_CREATE = "CREATE TABLE " + RECEIVE_FIELDS_TABLE + "("
    + COLUMN_ID +            " INTEGER PRIMARY KEY   AUTOINCREMENT, "
    + COLUMN_LASTID +        " INTEGER  , "
    + COLUMN_SMSNUMBER +     " VARCHAR  , "
    + COLUMN_MOBILENUMBER +  " VARCHAR , "
    + COLUMN_SENDERNAME +    " VARCHAR , "
    + COLUMN_SMSBODY +       " TEXT , "
    + COLUMN_RECEIVEDATE +   " VARCHAR );";