2012-10-03 109 views
0

我正在開發Android應用程序。它涉及SQLite數據庫。我在一個數據庫中創建了5個表並創建了5個DBAdapter。如何使用SQLite將數據庫表和列連接在一起數據庫

我有這些表(buddiesList,職稱,事件,喜歡不喜歡&)鍵和外鍵的4個表(標題,事件,喜歡不喜歡&)的主鍵。這些表是在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表,該名_ID是鏈接到事件,愛憎表的主鍵。事件中的name_id,喜歡和不喜歡的表是從buddiesList表中檢索的外鍵。但是,當我在「事件」頁面中鍵入事件詳細信息並將它們保存到事件表中時,表中的name_id應該是從buddiesList表中檢索的數字,但是它會以名稱而非ID的形式存儲。喜歡和不喜歡也一樣。

另外,如果取消對if(!db.isReadOnly()) { db.execSQL("PRAGMA foreign_keys = ON;"); }
我不能公開的信息,事件&短信頁面,我得到了強制關閉的消息。

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(喜歡錶)& DislikesDBAdapter(不喜歡錶)除外表的和列的名字

我的問題是,我不知道如何將表連接在一起。而且,如何在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。

任何人都可以幫我解決這個問題嗎?任何幫助提供將不勝感激。

+0

如果您顯示錯誤的'logcat'輸出將會很有幫助。 –

回答

0

你缺乏一些指標;見Required and Suggested Database Indexes

更改數據時,請確保它始終有效,例如,在其子級之前插入父級,並刪除其父級之前的所有子級。