我试图通过外键创建3个表之间的关系。有些东西是错误的,因为弹出一个完整的错误。
SQLiteException: near "code_element" which is equal to KEY_ELEMENTCODE in DATABASE_TABLEELEMENTS
代码如下:
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(" CREATE TABLE " + DATABASE_TABLESTUDY + " (" +
KEY_ROWSTUDYID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_STUDYCODE + " TEXT UNIQUE NOT NULL, " +
KEY_STUDYNAME + " TEXT " +
KEY_STUDANALYST + " TEXT " +
KEY_STUDYPHOTO + " BLOB);"
);
db.execSQL(" CREATE TABLE " + DATABASE_TABLEELEMENTS + " (" +
KEY_ROWELEMENTID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_STUDYID + " TEXT NOT NULL, FOREIGN KEY ("+KEY_STUDYID+") REFERENCES "+DATABASE_TABLESTUDY+" ("+KEY_ROWSTUDYID+"), " +
KEY_ELEMENTCODE + " TEXT UNIQUE NOT NULL, " +
KEY_ELEMENTNAME + " TEXT " +
KEY_ELEMENTPHOTO + " BLOB);"
);
db.execSQL(" CREATE TABLE " + DATABASE_TABLEDATA + " (" +
KEY_ROWDATATID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_ELEMENTID + " TEXT NOT NULL, FOREIGN KEY ("+KEY_ELEMENTID+") REFERENCES " + DATABASE_TABLEELEMENTS + " ("+KEY_ROWELEMENTID+"), " +
KEY_DATEDATA + " TEXT NOT NULL, " +
KEY_TIMEDATA + " TEXT NOT NULL, " +
KEY_ACTIVITYDATA + " TEXT NOT NULL, " +
KEY_HOURDATA + " TEXT NOT NULL, );"
);
答案 0 :(得分:0)
所有3个Create Table语句都是错误的:
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(" CREATE TABLE " + DATABASE_TABLESTUDY + " (" +
KEY_ROWSTUDYID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_STUDYCODE + " TEXT UNIQUE NOT NULL, " +
KEY_STUDYNAME + " TEXT " + // Missing comma!
KEY_STUDANALYST + " TEXT " + // Missing comma!
KEY_STUDYPHOTO + " BLOB);"
);
db.execSQL(" CREATE TABLE " + DATABASE_TABLEELEMENTS + " (" +
KEY_ROWELEMENTID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_STUDYID + " TEXT NOT NULL, FOREIGN KEY ("+KEY_STUDYID+") REFERENCES "+DATABASE_TABLESTUDY+" ("+KEY_ROWSTUDYID+"), " +
KEY_ELEMENTCODE + " TEXT UNIQUE NOT NULL, " +
KEY_ELEMENTNAME + " TEXT " + // Missing comma!
KEY_ELEMENTPHOTO + " BLOB);"
);
db.execSQL(" CREATE TABLE " + DATABASE_TABLEDATA + " (" +
KEY_ROWDATATID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_ELEMENTID + " TEXT NOT NULL, FOREIGN KEY ("+KEY_ELEMENTID+") REFERENCES " + DATABASE_TABLEELEMENTS + " ("+KEY_ROWELEMENTID+"), " +
KEY_DATEDATA + " TEXT NOT NULL, " +
KEY_TIMEDATA + " TEXT NOT NULL, " +
KEY_ACTIVITYDATA + " TEXT NOT NULL, " +
KEY_HOURDATA + " TEXT NOT NULL, );" // Extra comma!
);
他们应该是:
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(" CREATE TABLE " + DATABASE_TABLESTUDY + " (" +
KEY_ROWSTUDYID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_STUDYCODE + " TEXT UNIQUE NOT NULL, " +
KEY_STUDYNAME + " TEXT, " +
KEY_STUDANALYST + " TEXT, " +
KEY_STUDYPHOTO + " BLOB);"
);
db.execSQL(" CREATE TABLE " + DATABASE_TABLEELEMENTS + " (" +
KEY_ROWELEMENTID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_STUDYID + " TEXT NOT NULL, FOREIGN KEY ("+KEY_STUDYID+") REFERENCES "+DATABASE_TABLESTUDY+" ("+KEY_ROWSTUDYID+"), " +
KEY_ELEMENTCODE + " TEXT UNIQUE NOT NULL, " +
KEY_ELEMENTNAME + " TEXT, " +
KEY_ELEMENTPHOTO + " BLOB);"
);
db.execSQL(" CREATE TABLE " + DATABASE_TABLEDATA + " (" +
KEY_ROWDATATID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_ELEMENTID + " TEXT NOT NULL, FOREIGN KEY ("+KEY_ELEMENTID+") REFERENCES " + DATABASE_TABLEELEMENTS + " ("+KEY_ROWELEMENTID+"), " +
KEY_DATEDATA + " TEXT NOT NULL, " +
KEY_TIMEDATA + " TEXT NOT NULL, " +
KEY_ACTIVITYDATA + " TEXT NOT NULL, " +
KEY_HOURDATA + " TEXT NOT NULL );"
);
<强> [编辑] 强>
我更新了我的回答以反映您的最新更改:
你的两个Create Table语句仍然是错误的(最后是额外的逗号):
db.execSQL(" CREATE TABLE " + DATABASE_TABLEELEMENTS + " (" +
KEY_ROWELEMENTID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_STUDYID + " TEXT NOT NULL, FOREIGN KEY ("+KEY_STUDYID+") REFERENCES "+DATABASE_TABLESTUDY+" ("+KEY_ROWSTUDYID+"), " +
KEY_ELEMENTCODE + " TEXT UNIQUE NOT NULL, " +
KEY_ELEMENTNAME + " TEXT, " +
KEY_ELEMENTPHOTO + " BLOB, );" // Extra comma!
);
db.execSQL(" CREATE TABLE " + DATABASE_TABLEDATA + " (" +
KEY_ROWDATATID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_ELEMENTID + " TEXT NOT NULL, FOREIGN KEY ("+KEY_ELEMENTID+") REFERENCES " + DATABASE_TABLEELEMENTS + " ("+KEY_ROWELEMENTID+"), " +
KEY_DATEDATA + " TEXT NOT NULL, " +
KEY_TIMEDATA + " TEXT NOT NULL, " +
KEY_ACTIVITYDATA + " TEXT NOT NULL, " +
KEY_HOURDATA + " TEXT NOT NULL, );" // Extra comma!
);
他们应该是:
db.execSQL(" CREATE TABLE " + DATABASE_TABLEELEMENTS + " (" +
KEY_ROWELEMENTID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_STUDYID + " TEXT NOT NULL, FOREIGN KEY ("+KEY_STUDYID+") REFERENCES "+DATABASE_TABLESTUDY+" ("+KEY_ROWSTUDYID+"), " +
KEY_ELEMENTCODE + " TEXT UNIQUE NOT NULL, " +
KEY_ELEMENTNAME + " TEXT, " +
KEY_ELEMENTPHOTO + " BLOB );"
);
db.execSQL(" CREATE TABLE " + DATABASE_TABLEDATA + " (" +
KEY_ROWDATATID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_ELEMENTID + " TEXT NOT NULL, FOREIGN KEY ("+KEY_ELEMENTID+") REFERENCES " + DATABASE_TABLEELEMENTS + " ("+KEY_ROWELEMENTID+"), " +
KEY_DATEDATA + " TEXT NOT NULL, " +
KEY_TIMEDATA + " TEXT NOT NULL, " +
KEY_ACTIVITYDATA + " TEXT NOT NULL, " +
KEY_HOURDATA + " TEXT NOT NULL );"
);
答案 1 :(得分:0)
在
KEY_STUDYNAME +&#34;文字&#34; + KEY_STUDANALYST +&#34;文字&#34; +
和
KEY_ELEMENTNAME + " TEXT " +
<强>&#39;&#39; 强>
在TEXT错过之后。
请求
db.execSQL("CREATE TABLE DATABASE_TABLESTUDY (
KEY_ROWSTUDYID INTEGER PRIMARY KEY AUTOINCREMENT,
KEY_STUDYCODE TEXT UNIQUE NOT NULL,
KEY_STUDYNAME TEXT,
KEY_STUDANALYST TEXT,
KEY_STUDYPHOTO BLOB);"
db.execSQL("CREATE TABLE DATABASE_TABLEELEMENTS (
KEY_ROWELEMENTID INTEGER PRIMARY KEY AUTOINCREMENT,
KEY_STUDYID TEXT NOT NULL,
KEY_ELEMENTCODE TEXT UNIQUE NOT NULL,
KEY_ELEMENTNAME TEXT ,
KEY_ELEMENTPHOTO BLOB, FOREIGN KEY (KEY_STUDYID) REFERENCES DATABASE_TABLESTUDY ( KEY_ROWSTUDYID ));"
db.execSQL("CREATE TABLE DATABASE_TABLEDATA (
KEY_ROWDATATID INTEGER PRIMARY KEY AUTOINCREMENT,
KEY_ELEMENTID TEXT NOT NULL,
KEY_DATEDATA TEXT NOT NULL,
KEY_TIMEDATA TEXT NOT NULL,
KEY_ACTIVITYDATA TEXT NOT NULL,
KEY_HOURDATA TEXT NOT NULL, FOREIGN KEY (KEY_ELEMENTID) REFERENCES DATABASE_TABLEELEMENTS (KEY_ROWELEMENTID) )"
成功。请尝试,只需将静态字符串更改为变量。