使用外键添加新表时出错

时间:2014-11-08 16:53:22

标签: java android sqlite create-table

之前我在我的数据库中创建了一个表,并且我想再创建两个表, 我创建的第一个表是MS_MEMBER(用户名为PK), 第二个是MS_GROUP(Group_Id为PK), 最后一个是MS_GROUP_DETAIL, 并且MS_Group_detail有1个PK(GROUP_ID)和2个FK(GROUP_ID& USERNAME), 我试图创建它,但日志猫说MS_GROUP_DETAIL还没有创建。 这是我的代码,我想知道我的代码中是否有任何错误? 谢谢......

package com.thesis.teamizer;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Database {

public static final String DATABASE_NAME = "TeamizerDB.db";
public static final String MS_MEMBER = "Member";
public static final int DATABASE_VERSION = 3;
public static final String MEMBER_USERNAME = "Username";
public static final String MEMBER_PASSWORD = "Password";
public static final String MEMBER_EMAIL = "Email";
public static final int MEMBER_NOTIF_FLAG = 0;

public static final String MS_GROUP = "MsGroup";
public static final String GROUP_ID = "GroupId";
public static final String GROUP_NAME = "GroupName";
public static final String GROUP_DESCRIPTION = "GroupDescription";

public static final String MS_GROUP_DETAIL = "MsGroupDetail";

public DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

public static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE IF NOT EXISTS " + MS_MEMBER + " ("
                + MEMBER_USERNAME + " TEXT PRIMARY KEY NOT NULL, "
                + MEMBER_PASSWORD + " TEXT NOT NULL, " + MEMBER_EMAIL
                + " TEXT NOT NULL" + ");");

        db.execSQL("CREATE TABLE IF NOT EXISTS " + MS_GROUP + " ("
                + GROUP_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + GROUP_NAME + " TEXT NOT NULL, " + GROUP_DESCRIPTION
                + " TEXT NOT NULL);");

        db.execSQL("CREATE TABLE IF NOT EXISTS " + MS_GROUP_DETAIL + " ("
                + GROUP_ID + " INTEGER PRIMARY KEY, " + MEMBER_USERNAME
                + " TEXT, FOREIGN KEY (" + MEMBER_USERNAME
                + ") REFERENCES " + MS_MEMBER + "(" + MEMBER_USERNAME
                + "), FOREIGN KEY (" + GROUP_ID + ") REFERENCES "
                + MS_GROUP + "(" + GROUP_ID + ")");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        // If you need to add a column
        /*
         * if (newVersion > oldVersion) { db.execSQL("ALTER TABLE " +
         * MS_MEMBER + " ADD COLUMN " + MEMBER_NOTIF_FLAG +
         * " INTEGER DEFAULT 0"); }
         */
        db.execSQL("DROP TABLE IF EXISTS " + MS_MEMBER);
        db.execSQL("DROP TABLE IF EXISTS " + MS_GROUP);
        db.execSQL("DROP TABLE IF EXISTS " + MS_GROUP_DETAIL);
        onCreate(db);
    }

}

public Database(Context c) {
    ourContext = c;
}

public Database open() throws SQLException {
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {

    ourHelper.close();
}

public String getDataFromMsGroup() {
    String[] columns = new String[] { GROUP_ID, GROUP_NAME,
            GROUP_DESCRIPTION };

    Cursor c = ourDatabase.query(MS_GROUP, columns, null, null, null, null,
            null);

    /*
     * String SelectAll = "SELECT * FROM " + MS_GROUP; Cursor c =
     * ourDatabase.rawQuery(SelectAll, null);
     */
    String result = "";
    int iGroupId = c.getColumnIndex(GROUP_ID);
    int iGroupName = c.getColumnIndex(GROUP_NAME);
    int iGroupDesc = c.getColumnIndex(GROUP_DESCRIPTION);
    if (c.moveToFirst()) {
        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            result = result + c.getString(iGroupId) + " "
                    + c.getString(iGroupName) + " "
                    + c.getString(iGroupDesc) + "\n";
        }
    }

    return result;
}

public void createGroup(String gname, String gdesc, String username) {

    ContentValues cv = new ContentValues();
    cv.put(GROUP_NAME, gname);
    cv.put(GROUP_DESCRIPTION, gdesc);
    ourDatabase.insert(MS_GROUP, null, cv);

    ContentValues cv2 = new ContentValues();
    cv2.put(GROUP_NAME, gname);
    cv2.put(MEMBER_USERNAME, username);
    ourDatabase.insert(MS_GROUP_DETAIL, null, cv2);
}

/*
 * } public void createGroup(String gname, String gdesc, String username) {
 * // TODO Auto-generated method stub ContentValues cv = new
 * ContentValues(); cv.put(GROUP_NAME, gname); cv.put(GROUP_DESCRIPTION,
 * gdesc); cv.put(); ContentValues cv = new ContentValues();
 * cv.put(MEMBER_USERNAME, username); cv.put(MEMBER_PASSWORD, password);
 * cv.put(MEMBER_EMAIL, email); ourDatabase.insert(MS_MEMBER, null, cv);
 * return true;}
 */

}

0 个答案:

没有答案