Android SQLITE外键列为空。

huangapple 未分类评论48阅读模式
英文:

Android SQLITE foreign key column is null

问题

项目包含2个表,一个是Duser,包含所有用户信息;第二个是test表。我想要将用户的ID(在Duser表中作为主键)作为外键传递到test表中。但当我打开数据库时,外键列总是为空。我已经使用pragma启用了外键,我需要手动传递外键还是SQLite会自动处理?如果需要手动处理,该怎么做?请帮忙。

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("CREATE TABLE Duser(id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT, name TEXT, password TEXT, confirm TEXT, number TEXT)");
    sqLiteDatabase.execSQL("CREATE TABLE test(Iid INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, f_id INTEGER NOT NULL, FOREIGN KEY(f_id) REFERENCES Duser(id) ON UPDATE RESTRICT ON DELETE RESTRICT)");
    onConfigure(sqLiteDatabase);
}

@Override
public void onConfigure(SQLiteDatabase db) {
    super.onConfigure(db);
    db.execSQL("PRAGMA foreign_keys=ON;");
}

public boolean Insert(String email, String name, String password, String confirm, String number) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("email", email);
    contentValues.put("name", name);
    contentValues.put("password", password);
    contentValues.put("confirm", confirm);
    contentValues.put("number", number);
    long ins = db.insert("Duser", null, contentValues);
    return ins != -1;
}

public boolean addInterest(String name) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("name", name);
    long ins = db.insert("test", null, contentValues);
    return ins != -1;
}
英文:

The project contains 2 tables one is Duser which contains all the user information and the second one is the test table. I want to pass the User's id which is the primary key in Duser Table as a foreign key in the test table. when I'm opening the database foreign key column is always null. I have also enabled foreign-key by using pragrma, Do I need to pass foreign key manually or SQLite will do it? if manually then how? Please help.

 @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("Create Table Duser(id integer primary key autoincrement,email Text, name text,password text, confirm text, number string)");
        sqLiteDatabase.execSQL("Create table test(Iid integer primary key autoincrement, name Text, f_id integer NOT NULL , Foreign key(f_id)   REFERENCES DUSER(id) on update restrict on delete restrict)");
        onConfigure(sqLiteDatabase);
    }

    @Override
    public void onConfigure(SQLiteDatabase db) {
        super.onConfigure(db);
        db.execSQL("PRAGMA foreign_keys=ON;");
    }

  public boolean Insert(String email, String name, String password, String confirm, String number) {


        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();

        contentValues.put("email", email);
        contentValues.put("name", name);
        contentValues.put("password", password);
        contentValues.put("confirm", confirm);
        contentValues.put("number", number);

        long ins = db.insert("Duser", "null", contentValues);
        if (ins == -1)
            return false;
        else return true;
    }

  public boolean addInterest(String name) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", name);
        long ins = db.insert("test", null, contentValues);

        if (ins == -1)
            return false;
        else return true;

    }

huangapple
  • 本文由 发表于 2020年5月29日 16:48:28
  • 转载请务必保留本文链接:https://java.coder-hub.com/62082046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定