我正在尝试创建一个包含两个表的SQLite数据库。第二个表(days_table)应该保存List的数据,因此需要通过外键连接到第一个表。 (不确定我是否做得对。)
编辑:更新的代码:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "alarms.db";
private static final String TABLE_NAME = "alarm_table";
private static final String COL_1 = "ID";
private static final String COL_2 = "NAME";
//private static final String COL_3 = "DAYS";
private static final String COL_4 = "RINGTONE";
private static final String COL_5 = "HOUR";
private static final String COL_6 = "MINUTE";
private static final String TABLE_NAME_DAYS = "days_table";
private static final String DAYS_COL_1 = "MONDAY";
private static final String DAYS_COL_2 = "TUESDAY";
private static final String DAYS_COL_3 = "WEDNESDAY";
private static final String DAYS_COL_4 = "THURSDAY";
private static final String DAYS_COL_5 = "FRIDAY";
private static final String DAYS_COL_6 = "SATURDAY";
private static final String DAYS_COL_7 = "SUNDAY";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
+ COL_1 + " TEXT PRIMARY KEY NOT NULL, "
+ COL_2 + " TEXT NOT NULL, "
+ COL_4 + " TEXT NOT NULL, "
+ COL_5 + " INTEGER NOT NULL, "
+ COL_6 + " INTEGER NOT NULL" +
")");
db.execSQL("CREATE TABLE " + TABLE_NAME_DAYS + " ("
+ COL_1 + " TEXT PRIMARY KEY NOT NULL, "
+ DAYS_COL_1 + " TEXT, "
+ DAYS_COL_2 + " TEXT, "
+ DAYS_COL_3 + " TEXT, "
+ DAYS_COL_4 + " TEXT, "
+ DAYS_COL_5 + " TEXT, "
+ DAYS_COL_6 + " TEXT, "
+ DAYS_COL_7 + " TEXT "
+
")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
public boolean addObjectToDB(Alarm inData){
SQLiteDatabase db = getWritableDatabase();
boolean rValue;
long result;
// ============== CV : start ==============
ContentValues cv = new ContentValues();
cv.put(COL_1, inData.getId());
cv.put(COL_2, inData.getName());
// COL_3 - DAYS
cv.put(COL_4, inData.getRingtone().toString());
cv.put(COL_5, inData.getHour());
cv.put(COL_6, inData.getMinute());
result = db.insert(TABLE_NAME, null, cv);
if(result == -1){
rValue = false;
}
else{
// ============== CV2 : start ==============
ContentValues cv2 = new ContentValues();
List<String> days = inData.getDays();
cv2.put(COL_1, inData.getId());
cv2.put(DAYS_COL_1, days.get(0));
cv2.put(DAYS_COL_2, days.get(1));
cv2.put(DAYS_COL_3, days.get(2));
cv2.put(DAYS_COL_4, days.get(3));
cv2.put(DAYS_COL_5, days.get(4));
cv2.put(DAYS_COL_6, days.get(5));
cv2.put(DAYS_COL_7, days.get(6));
result = db.insert(TABLE_NAME_DAYS, null, cv2);
rValue = result != -1;
}
return rValue;
}
}
我更新了我的代码,如上所示。现在我得到以下错误:
插入SATURDAY时出错= null FRIDAY = null THURSDAY = null TUESDAY = Tue SUNDAY = null MONDAY = null WEDNESDAY = null ID = Alarm_ade731f0-f5e4-44c2-9c4a-9f35eaf8941e
android.database.sqlite.SQLiteException:没有这样的表:days_table(Sqlite代码1):,同时编译:INSERT INTO days_table(星期六,星期五,星期四,星期二,星期日,星期一,星期三,ID)值(?, ?,?,?,?,?,?,?),(操作系统错误 - 2:没有这样的文件或目录)
好像我的第二个Create Table sql查询中有错误?