外键错误

时间:2014-10-13 10:39:03

标签: sql-server database foreign-keys

  1. CREATE TABLE IF NOT NOT EXISTS用户(id INTEGER PRIMARY KEY,uname TEXT,fname TEXT,lname TEXT,pwd TEXT,mailid TEXT)
  2. CREATE TABLE IF NOT EXISTS组(groupid INTEGER PRIMARY KEY,groupname TEXT,userid INTEGER,groupdate TEXT,groupdescription TEXT,groupnote TEXT,FOREIGN KEY(userid)REFERENCES users(id))
  3. PROGRAM

    private static final String TABLE_USERS = "users";
    private static final String TABLE_GROUP = "group";
    
    // user Table Columns names
    
    private static final String KEY_ID = "id";
    private static final String KEY_USERNAME = "uname";
    private static final String KEY_FIRSTNAME = "fname";
    private static final String KEY_LASTNAME = "lname";
    private static final String KEY_PASSWORD = "pwd";
    private static final String KEY_EMAILID = "mailid";
    
    // group Table Columns names
    
    private static final String KEY_GPID = "groupid";
    private static final String KEY_GNAME = "groupname";
    private static final String KEY_GUSERID = "userid";
    private static final String KEY_GDATE = "groupdate";
    private static final String KEY_GDESCRIPTION = "groupdescription";
    private static final String KEY_GNOTE = "groupnote";
    
    private static final String TYPE_TEXT = " TEXT";
    private static final String TYPE_INTEGER = " INTEGER";
    private static final String TYPE_REAL = " REAL";
    private static final String COMMA_SEP = ",";
    private static final String REFER  = " REFERENCES ";
    private static final String FOREIGN  = " FOREIGN KEY ";
    private static final String LEFT_BRACKET  = "(";
    private static final String RIGHT_BRACKET  = ")";
    
    
    String CREATE_USER_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_USERS + LEFT_BRACKET 
                + KEY_ID+ " INTEGER PRIMARY KEY," 
                + KEY_USERNAME + TYPE_TEXT+ COMMA_SEP 
                + KEY_FIRSTNAME + TYPE_TEXT + COMMA_SEP
                + KEY_LASTNAME + TYPE_TEXT + COMMA_SEP 
                + KEY_PASSWORD+ TYPE_TEXT + COMMA_SEP 
                + KEY_EMAILID + TYPE_TEXT + RIGHT_BRACKET;
    
    
    String CREATE_GROUP_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_GROUP + LEFT_BRACKET
                + KEY_GPID+ " INTEGER PRIMARY KEY," 
                + KEY_GNAME + TYPE_TEXT+ COMMA_SEP
                + KEY_GUSERID+ TYPE_INTEGER +COMMA_SEP
                + KEY_GDATE + TYPE_TEXT+ COMMA_SEP
                + KEY_GDESCRIPTION + TYPE_TEXT + COMMA_SEP 
                + KEY_GNOTE+ TYPE_TEXT +COMMA_SEP
                +FOREIGN+LEFT_BRACKET+KEY_GUSERID+RIGHT_BRACKET+REFER+TABLE_USERS+LEFT_BRACKET+KEY_ID+RIGHT_BRACKET+ RIGHT_BRACKET;
    
    try
    {
        db.execSQL(CREATE_USER_TABLE);
        db.execSQL(CREATE_GROUP_TABLE);
    }
    catch(SQLiteException e)
    {
        e.printStackTrace();
    }
    

    例外

    android.database.sqlite.SQLiteException:near" group&#34 ;:语法错误(代码1):,编译时:CREATE TABLE IF NOT EXISTS组(groupid INTEGER PRIMARY KEY,groupname TEXT, userid INTEGER,groupdate TEXT,groupdescription TEXT,groupnote TEXT,FOREIGN KEY(userid)REFERENCES users(id))

    请告诉我这个pbm的解决方案???????  什么是pbm ?????  为什么会发生????????

2 个答案:

答案 0 :(得分:1)

请尝试这种方式,希望这有助于您解决问题。

'组'和' GROUP'是SQL / SQLite中的关键字,因此您要么更改表名,要么只在组表名称关键字周围添加双引号,如:

CREATE TABLE IF NOT EXISTS "group"(groupid INTEGER PRIMARY KEY,groupname TEXT,userid INTEGER,groupdate TEXT,groupdescription TEXT,groupnote TEXT, FOREIGN KEY (userid) REFERENCES users(id))

答案 1 :(得分:0)

GROUP是SQL中的关键字。重命名表格或将其放入"double quotes"