我正在开发一个 Android 应用程序。它涉及 SQLite 数据库。我在一个数据库中创建了 5 个表并创建了 5 个 DBAdapter。
我在这些表中有主键(buddiesList、titles、event、likes & dislikes)和 4 个表中的外键(titles、event、likes & dislikes)。这些表是在 SQLite 数据库中创建的。
哦,一个 DBAdapter 是我编写代码以创建 5 个表的地方,并且需要将这些表链接在一起,并且将主键和外键链接在一起。而其他 4 个 DBAdapter 用于 5 个表中的每一个。我只是发布代码,您可能会明白我在说什么以及我需要什么。
AnniversaryDBAdapter - 创建了 5 个表。
public class AnniversaryDBAdapter
{
private static final String DATABASE_NAME = "Tables";
private static final int DATABASE_VERSION = 2;
private static final String CREATE_TABLE_TITLE = "create table titles(title_id integer primary key autoincrement, title text not null, image text not null);";
private static final String CREATE_TABLE_BUDDIESLIST = "create table buddiesList(name_id integer primary key autoincrement, name text not null);";
private static final String CREATE_TABLE_LIKES = "create table likes(likes_id integer primary key autoincrement,like text not null, name_id integer not null, FOREIGN KEY(name_id) REFERENCES buddiesList(name_id));";
private static final String CREATE_TABLE_DISLIKES = "create table dislikes(dlike_id integer primary key autoincrement, dislike text not null, name_id integer not null, FOREIGN KEY(name_id) REFERENCES buddiesList(name_id));";
private static final String CREATE_TABLE_EVENTS = "create table event(event_id integer primary key autoincrement, title_id integer not null, location text not null, starttime text not null, endtime text not null, desc text not null, date text not null, name_id integer not null, FOREIGN KEY(title_id) REFERENCES titles(title_id));";
private final Context context;
private static final String TAG = "DBAdapter";
private DatabaseHelper DBHelper;
protected SQLiteDatabase db;
private static String DB_PATH = "/data/data/main.page/Tables/";
public AnniversaryDBAdapter(Context aContext)
{
this.context = aContext;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
/*try
{
database.execSQL(CREATE_TABLE_BUDDIESLIST);
database.execSQL(CREATE_TABLE_LIKES);
database.execSQL(CREATE_TABLE_EVENTS);
database.execSQL(CREATE_TABLE_TITLE);
database.execSQL(CREATE_TABLE_DISLIKES);
}catch(SQLException e)
{
e.printStackTrace();
}*/
db.execSQL(CREATE_TABLE_BUDDIESLIST);
db.execSQL(CREATE_TABLE_LIKES);
db.execSQL(CREATE_TABLE_EVENTS);
db.execSQL(CREATE_TABLE_TITLE);
db.execSQL(CREATE_TABLE_DISLIKES);
/* database.execSQL("CREATE TRIGGER fk_budevent_nameid" +
"BEFORE INSERT" +
"ON events FOR EACH ROW BEGIN" +
"SELECT CASE WHEN((SELECT name_id FROM buddiesList WHERE name_id = new.name_id) IS NULL)" +
"THEN RAISE(ABORT, 'Foreign Key Violation')END;" +
"END;");
*/
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version "+oldVersion+" to "+newVersion+", which will destroy all old data");
onCreate(db);
}
}
public AnniversaryDBAdapter open() throws SQLException
{
db = this.DBHelper.getWritableDatabase();
/* if(!db.isReadOnly())
{
db.execSQL("PRAGMA foreign_keys = ON;");
}*/
return this;
}
public void close()
{
DBHelper.close();
}
/*
public long insertEvent(String title,String location,String starttime,String endtime,String desc,String date, String name)
{
ContentValues cValues = new ContentValues();
cValues.put(KEY_TITLE, title);
cValues.put(KEY_LOCATION, location);
cValues.put(KEY_START, starttime);
cValues.put(KEY_END, endtime);
cValues.put(KEY_DESC, desc);
cValues.put(KEY_ALARM, alarm);
cValues.put(KEY_DATE, date);
cValues.put(KEY_NAME, name);
return db.insert(CREATE_TABLE_EVENTS, null, cValues);
}
public boolean deleteEvent(long rowId)
{
return db.delete(CREATE_TABLE_EVENTS, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor getAllEvents()
{
return db.query(CREATE_TABLE_EVENTS, new String[] {KEY_ROWID, KEY_TITLE, KEY_LOCATION, KEY_START, KEY_END, KEY_DESC, KEY_DATE, KEY_NAME}, null, null, null, null, null);
}
public Cursor getEvent(long rowId) throws SQLException
{
Cursor c = db.query(true,CREATE_TABLE_EVENTS, new String[] {KEY_ROWID, KEY_TITLE, KEY_LOCATION, KEY_START, KEY_END, KEY_DESC, KEY_DATE, KEY_NAME}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
if(c != null)
{
c.moveToFirst();
}
return c;
}
*/
}
正如您在 AnniversaryDBAdapter 中看到的,在 buddiesList 表中,name_id 是链接到事件、喜欢和不喜欢表的主键。event, likes and dislikes 表中的 name_id 是从 buddiesList 表中检索的外键。但是,当我在事件页面中键入事件详细信息并将它们保存到事件表中时,表中的 name_id 应该是从 buddiesList 表中检索到的数字,而是存储为名称而不是 id。喜欢和不喜欢也是如此。
另外,如果取消注释if(!db.isReadOnly())
{
db.execSQL("PRAGMA foreign_keys = ON;");
}
我无法打开信息、事件和 SMS 页面,我会收到强制关闭消息。
BuddyDBAdapter - buddiesList 表
public class BuddyDBAdapter extends AnniversaryDBAdapter
{
public static final String KEY_ROWID = "name_id";
public static final String KEY_NAME = "name";
private static final String TAG = "DBAdapter";
private static final String CREATE_TABLE_BUDDIESLIST = "buddiesList";
//private SQLiteDatabase db;
public BuddyDBAdapter(Context aContext)
{
super(aContext);
}
public long insertNames(String name)
{
ContentValues buddyValues = new ContentValues();
buddyValues.put(KEY_NAME, name);
return db.insert(CREATE_TABLE_BUDDIESLIST, null, buddyValues);
}
public boolean deleteNames(long rowId)
{
return db.delete(CREATE_TABLE_BUDDIESLIST, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor getAllNames()
{
return db.query(CREATE_TABLE_BUDDIESLIST, new String[] { KEY_ROWID, KEY_NAME }, null, null, null, null, null);
}
public Cursor getNames(long rowId) throws SQLException
{
Cursor c = db.query(true, CREATE_TABLE_BUDDIESLIST, new String[] { KEY_ROWID, KEY_NAME }, KEY_ROWID + "=" + rowId, null, null, null, null, null);
if(c != null)
{
c.moveToFirst();
}
return c;
}
}
BuddyDBAdapter 中的代码类似于 CalendarAdapter(事件表)、 LikesDBAdapter(likes table) & DislikesDBAdapter(dislikes table) 除了表和列的名称
我的问题是,我不确定如何将表格连接在一起。以及,如何在 SQLite 数据库中启用外键。
即使我输入喜欢
private static final String CREATE_TABLE_TITLE = "create table titles(title_id integer primary key autoincrement, title text not null, image text not null);";
private static final String CREATE_TABLE_BUDDIESLIST = "create table buddiesList(name_id integer primary key autoincrement, name text not null);";
private static final String CREATE_TABLE_LIKES = "create table likes(likes_id integer primary key autoincrement,like text not null, name_id integer not null, FOREIGN KEY(name_id) REFERENCES buddiesList(name_id));";
private static final String CREATE_TABLE_DISLIKES = "create table dislikes(dlike_id integer primary key autoincrement, dislike text not null, name_id integer not null FOREIGN KEY(name_id) REFERENCES buddiesList(name_id));";
private static final String CREATE_TABLE_EVENTS = "create table event(event_id integer primary key autoincrement, title_id integer not null, location text not null, starttime text not null, endtime text not null, desc text not null, date text not null, name_id integer not null, FOREIGN KEY(name_id) REFERENCES buddiesList(name_id), FOREIGN KEY(title_id) REFERENCES titles(title_id));";
和
if(!db.isReadOnly())
{
db.execSQL("PRAGMA foreign_keys = ON;");
}
我仍然有强制关闭错误。
另外,我不确定如何将 buddiesList 表中的 name_id 连接到事件、喜欢和不喜欢表。同样,将titles表中的title_id连接到事件表中的title_id。
谁能帮我解决这个问题?提供的任何帮助将不胜感激。