2012-08-15 44 views
0

我已经创建了一个数据库适配器,包含我在数据库中拥有的所有表,但是您都可以看到3个表中存在的name_id。但它是CREATE_TABLE_LIKES和CREATE_TABLE_DISLIKES中的外键,所以有什么方法可以使它成为2个表中的外键?任何帮助将不胜感激。谢谢!如何在我的DBAdapter类中创建一个外键?

代码数据库适配器:

package main.page; 


import android.content.Context; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteDatabase.CursorFactory; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class AnniversaryDBAdapter 
{ 

    private static final String DATABASE_NAME = "AllTables"; 
    private static final int DATABASE_VERSION = 2; 

    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(name_id integer primary key autoincrement,likes text not null);"; 
    private static final String CREATE_TABLE_DISLIKES = " create table dislikes(name_id integer primary key autoincrement, dislikes text not null);"; 
    private static final String CREATE_TABLE_EVENTS = "create table events(date_id integer primary key autoincrement, name_id text not null, date text not null, title_id text not null, starttime text not null, endtime text not null);"; 
    private static final String CREATE_TABLE_TITLE = "create table titles(title_id integer primary key autoincrement, name text not null, image text not null);"; 

    private final Context context; 
    private static final String TAG = "DBAdapter"; 

    private DatabaseHelper DBHelper; 
    private SQLiteDatabase db; 

    public AnniversaryDBAdapter(Context ctx) 
    { 
     this.context = ctx; 
     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) 
    { 
     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); 
    } 

    @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 
{ 
    this.db = this.DBHelper.getWritableDatabase(); 
    return this; 
} 

public void close() 
{ 
    this.DBHelper.close(); 
} 

} 

回答

0

这是无法使用自动递增的外键,因为它应该永远是一个值在另一个表存在,它可能是简单的创建新专栏只是为了建立这种关系。