使用外键ANDROID创建db表

时间:2016-05-02 04:08:48

标签: android sqlite

我试图创建一个(目前)有4个表{USERS,TOPICS,GROUPS,CREATED_TOPICS}的数据库。当我尝试运行它时,我不断收到此错误...

  

语法错误(代码1):,编译时:创建表   created_topics_table(CREATED_TOPIC_ID INTEGER PRIMARY KEY   AUTOINCREMENT,TOPIC_ID INTEGER,USER_ID INTEGER,FOREIGN KEY(TOPIC_ID)   REFERENCES topic_table(_ID),FOREIGN KEY(USER_ID)REFERENCES   users_table(_ID)。

继承人代码......

public class databaseHelper extends SQLiteOpenHelper
{
//DATABASE NAME
public static final String DATABASE_NAME = "users.db";
//USERS TABLE
public static final String TABLE_NAME_USERS = "users_table";
public static final String COL_USER_ID = "_ID";
public static final String COL_USER_NAME = "USER_NAME";
public static final String COL_USER_EMAIL = "USER_EMAIL";
public static final String COL_USER_PASSWORD = "USER_PASSWORD";
//TOPICS TABLE
public static final String TABLE_NAME_TOPICS = "topics_table";
public static final String COL_TOPIC_ID = "_ID";
public static final String COL_TOPIC_NAME = "TOPIC_NAME";


//GROUPS TABLE
public static final String TABLE_NAME_GROUPS = "groups_table";
public static final String COL_GROUP_ID = "GROUP_ID";
public static final String COL_GROUP_NAME = "GROUP_NAME";

public static final String TABLE_NAME_CREATED_TOPICS =
"created_topics_table";
public static final String COL_CREATED_TOPIC_ID = "CREATED_TOPIC_ID";
public static final String COL_FK_TOPIC_TOPIC_ID = "TOPIC_ID";
public static final String COL_FK_TOPIC_USER_ID = "USER_ID";

public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME_USERS + " (_ID INTEGER PRIMARY KEY   
AUTOINCREMENT,USER_NAME TEXT,USER_EMAIL TEXT,USER_PASSWORD TEXT)");
db.execSQL("create table " + TABLE_NAME_TOPICS + " (_ID INTEGER PRIMARY KEY     
AUTOINCREMENT,TOPIC_NAME TEXT)");
db.execSQL("create table " + TABLE_NAME_GROUPS + " (GROUPS_ID INTEGER PRIMARY    
KEY AUTOINCREMENT,GROUP_NAME TEXT)");
db.execSQL("create table " + TABLE_NAME_CREATED_TOPICS + " (CREATED_TOPIC_ID 
INTEGER PRIMARY KEY AUTOINCREMENT, TOPIC_ID INTEGER,USER_ID INTEGER, FOREIGN
KEY(TOPIC_ID) REFERENCES " +TABLE_NAME_TOPICS + "(_ID)," +                                                                                                                                                            
"FOREIGN KEY(USER_ID) REFERENCES " +TABLE_NAME_USERS + "(_ID)");
}

2 个答案:

答案 0 :(得分:5)

你错过了最后的括号:

create table created_topics_table (CREATED_TOPIC_ID INTEGER PRIMARY KEY AUTOINCREMENT, TOPIC_ID INTEGER,USER_ID INTEGER, FOREIGN KEY(TOPIC_ID) REFERENCES topics_table(_ID),FOREIGN KEY (USER_ID) REFERENCES users_table(_ID))

关于sqlite外键的另一个注意事项。它们默认是关闭的。每次打开数据库连接时都需要使用"PRAGMA foreign_keys=ON"

修改

变化:

db.execSQL("create table " + TABLE_NAME_CREATED_TOPICS + " (CREATED_TOPIC_ID 
INTEGER PRIMARY KEY AUTOINCREMENT, TOPIC_ID INTEGER,USER_ID INTEGER, FOREIGN
KEY (TOPIC_ID) REFERENCES " +TABLE_NAME_TOPICS + " (_ID)," +                                                                    
"FOREIGN KEY (USER_ID) REFERENCES " +TABLE_NAME_USERS + " (_ID))");

答案 1 :(得分:1)

FOREIGN KEY(TOPIC_ID)REFERENCES topic_table(_ID), FOREIGN KEY(USER_ID)

之间添加空格

(CREATED_TOPIC_ID INTEGER PRIMARY KEY AUTOINCREMENT,TOPIC_ID INTEGER,USER_ID INTEGER,FOREIGN KEY(TOPIC_ID)REFERENCES topics_table(_ID),FOREIGN KEY(USER_ID)REFERENCES users_table(_ID)

使用这个希望它会起作用。