我试图在Android中同时添加两个表。我想尝试2个小时,但我现在失去了。我说有2张桌子。它们是Body和Person表。人员表工作正常,但身体没有。 Body有一个指向Person的PK的外键。
这就是我迷失的地方。
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "healthMonitor.db";
// Contacts table name
private static final String TABLE_NAME = "Person";
private static final String TABLE_NAME2 = "Body";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String BODY_KEY_ID = "bId";
private static final String FOREIGN_KEY_ID = "personId";
private static final String KEY_NAME = "name";
private static final String KEY_ARM = "arm";
private static final String KEY_BRAIN = "brain";
private static final String KEY_EYE = "eye";
private static final String KEY_HEART = "heart";
private static final String KEY_LEG = "leg";
private static final String KEY_SURNAME = "surname";
private static final String KEY_AGE = "age";
private static final String KEY_EYECOLOR = "eyeColor";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME , null, DATABASE_VERSION);
}
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
private String createFirstTable(){
String query = "CREATE TABLE " + TABLE_NAME + " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT, " +
KEY_SURNAME + " TEXT, "+
KEY_AGE + " INTEGER, " +
KEY_EYECOLOR + " TEXT);";
return query;
}
private String createSecondTable(){
String query = "CREATE TABLE " + TABLE_NAME2 + " (" +
BODY_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_ARM + " INTEGER, " +
KEY_BRAIN + " INTEGER, "+
KEY_EYE + " INTEGER, " +
KEY_HEART + " INTEGER, "+
KEY_LEG +" INTEGER, " +
FOREIGN_KEY_ID + " INTEGER, "+
FOREIGN_KEY_ID + " FOREIGN KEY REFERENCES "+TABLE_NAME+ "("+KEY_ID+");";
return query;
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createFirstTable());
db.execSQL(createSecondTable());
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + "," + TABLE_NAME2);
// Create tables again
onCreate(db);
}
boolean addContact(MainActivity.Person person) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, person.getName()); // Person Name
values.put(KEY_SURNAME, person.getSurname()); // Person Surname
values.put(KEY_AGE, person.getAge()); // Person Age
values.put(KEY_EYECOLOR, person.getEyeColor()); // Person Surname
// Inserting Row
db.insert(TABLE_NAME, null, values);
//db.close(); // Closing database connection
return true;
}
boolean addBody() {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ARM, 5); // Person Name
values.put(KEY_BRAIN, 4); // Person Surname
values.put(KEY_EYE, 3); // Person Age
values.put(KEY_HEART, 2); // Person Surname
values.put(KEY_LEG, 1); // Person Age
values.put(FOREIGN_KEY_ID, 1); // Person Surname
// Inserting Row
db.insert(TABLE_NAME2, null, values);
//db.close(); // Closing database connection
return true;
}
请检查onCreate()函数。我真的输了。感谢
" FOREIGN KEY(" + FOREIGN_KEY_ID +")REFERENCES" + TABLE_NAME + "(&#34 + + KEY_ID");"
和
FOREIGN_KEY_ID +" INTEGER REFERENCES" + TABLE_NAME +"(" + KEY_ID +");";
这个不起作用
FOREIGN_KEY_ID +" INTEGER REFERENCES" + TABLE_NAME +"(" + KEY_ID +"));&#34 ;;
答案 0 :(得分:0)
您错过了第二个查询中的右括号
您的代码:FOREIGN_KEY_ID + " FOREIGN KEY REFERENCES "+TABLE_NAME+ "("+KEY_ID+");";
已更新:FOREIGN_KEY_ID + " FOREIGN KEY REFERENCES "+TABLE_NAME+ "("+KEY_ID+"));";