Sqlite表创建不允许创建表

时间:2013-09-09 07:54:14

标签: android sql android-sqlite

@Override
public void onCreate(SQLiteDatabase db) {
    final String CREATE_IMAGE_TABLE = "CREATE TABLE " + TABLE_IMG + "("
            + KEY_ID + " INTEGER PRIMARY KEY autoincrement," + KEY_IMAGE_NAME + " TEXT,"
            + KEY_PATH + " TEXT" + ")";


    final String CREATE_TAG_TABLE = "CREATE TABLE " + TABLE_TAG + "("
            + KEY_ID + " INTEGER PRIMARY KEY autoincrement," + KEY_TAG_NAME + " TEXT,"
            + KEY_TAG_DATE + " INTEGER" + ")";



   final String CREATE_IMGTAG_TABLE = "CREATE TABLE " + TABLE_IMAGE_TAG + "("  + TAG_ID            + " TEXT REFERENCES TABLE_TAG (tag_id)," + IMG_ID + " TEXT REFERENCES TABLE_IMG (img_id),"
                + KEY_ID + " INTEGER, " + "INTEGER PRIMARY KEY(IMG_ID,TAG_ID,KEY_ID)" + ")";



    db.execSQL(CREATE_IMAGE_TABLE);
    db.execSQL(CREATE_TAG_TABLE);
    db.execSQL(CREATE_IMAGE_TAG_TABLE);

    Log.i("TAG", "TABLES CREATED" + CREATE_IMAGE_TAG_TABLE);
}

Hello All

我正在尝试创建3个包含图像数据,标签数据的表格,以及第3个具有图像和标签数据ID的表格。我想创建第3个表,其中包含图像表的Id和标记表的Id的引用,它们都是它们自己的表中的主键。现在,如果我想将这些表的id值添加到我的第三个表中,我已经触发了上面的查询,但它不允许我创建它。它给了我语法错误:

09-10 14:52:13.741: E/Database(9850): Failure 1 (near "(": syntax error) on 0x223a80 when preparing 'CREATE TABLE img_tag_table(tag_id TEXT REFERENCES TABLE_TAG (tag_id),img_id TEXT REFERENCES TABLE_IMG (img_id),id INTEGER, INTEGER PRIMARY KEY(IMG_ID,TAG_ID,KEY_ID))'.

2 个答案:

答案 0 :(得分:0)

我猜你的意思是这样的。

final String CREATE_IMAGE_TAG_TABLE = "CREATE TABLE " + TABLE_IMAGE_TAG + "("
            + IMAGE_ID + " INTEGER REFERENCES " +  TABLE_IMG + "(id)," + TAG_ID + " INTEGER REFERENCES " + TABLE_TAG + "(id),"
            + IMG_TAG + " PRIMARY KEY (IMAGE_ID, TAG_ID) " + ")";

答案 1 :(得分:0)

  

img_tag PRIMARY KEY (IMAGE_ID, TAG_ID)

此列规范为您提供(上的语法错误。你想要实现什么目标?

如果您想使IMAGE_ID, TAG_ID对唯一/主键,只需将其更改为表约束

PRIMARY KEY (IMAGE_ID, TAG_ID)

在您的代码中:

final String CREATE_IMAGE_TAG_TABLE = "CREATE TABLE " + TABLE_IMAGE_TAG + "("
        + IMAGE_ID + " INTEGER REFERENCES CREATE_IMAGE_TABLE(id)," + TAG_ID + " INTEGER REFERENCES CREATE_TAG_TABLE(id),"
        + " PRIMARY KEY (IMAGE_ID, TAG_ID) " + ")";

在您编辑的问题之后,问题是:

  

INTEGER PRIMARY KEY(IMG_ID,TAG_ID,KEY_ID)

这既不是列规范,也不是表约束。要使其成为表约束,请删除INTEGER(上面的代码)。要制作INTEGER PRIMARY KEY列,请将其更改为columnname INTEGER PRIMARY KEY