我创建了两个表,这些表中有一个主键和外键。我想编写代码,当我将其插入到table1中,然后再插入到table2中时,因此将自动插入与table1的主键相同的table2的table2外键列中的值。 >
我正在android studio中为我的android应用程序创建这些表和数据库,并使用sqlite3。
CREATE TABLE table1(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR,
fathername VARCHAR,
dob DATE,
age INT
);
CREATE TABLE table2(
m_id INTEGER PRIMARY KEY AUTOINCREMENT,
treatdate DATE,
hospital VARCHAR,
city VARCHAR,
id2 INT,
FOREIGN KEY (id2) REFERENCES table1(id)
);
答案 0 :(得分:0)
如果使用SQLiteDatabse 插入方法,它将在插入行上返回 rowid (对于未使用WITHOUT ROWID定义的表)。由于您已经使用INTEGER PRIMARY KEY定义了 id 列,因此它是 rowid 列的别名,因此分配给 id 的值将返回(如果已成功插入该列,则返回-1)。
例如
ContentValues cv = new ContentValues();
cv.put("name","Fred");
cv.put("fathername","George");
cv.put("dob","2000-01-01");
cv.put("age",21)
long returned_id = your_sqlite_database.insert("table1",null,cv);
returned_id 将是插入到 table1 中的行的ID。
通常情况下,上述方法位于以名称,父亲名称,出生日期和年龄作为参数并返回ID的方法中。
这将创建数据库和表并定义3种方法:-
getAllFromTable1JoinedWithTable2 按照方法名称的含义进行操作
注意:SQLiteOpenHelper的onConfigure方法被覆盖以启用外键支持(默认情况下未启用)
:-
public class DBHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mydb";
public static final int DBVERSION = 1;
public static final String TBL_TABLE1 = "table1";
public static final String TBL_TABLE2 = "table2";
public static final String COL_TABLE1_ID = "id";
public static final String COL_TABLE1_NAME = "name";
public static final String COL_TABLE1_FATHERNAME = "fathername";
public static final String COL_TABLE1_DOB = "dob";
public static final String COL_TABLE1_AGE = "age";
public static final String COL_TABLE2_ID = "m_id";
public static final String COL_TABLE2_TREATDATE = "treatdate";
public static final String COL_TABLE2_HOSPITAL = "hospital";
public static final String COL_TABLE2_CITY = "city";
public static final String COL_TABLE2_ID2 = "id2";
SQLiteDatabase mDB;
public DBHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
mDB = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
String table1_crtsql = "CREATE TABLE IF NOT EXISTS " + TBL_TABLE1 + "(" +
COL_TABLE1_ID + " INTEGER PRIMARY KEY, " +
COL_TABLE1_NAME + " TEXT, " +
COL_TABLE1_FATHERNAME + " TEXT, " +
COL_TABLE1_DOB + " TEXT, " +
COL_TABLE1_AGE + " INTEGER" +
")";
db.execSQL(table1_crtsql);
String table2_crtsql = "CREATE TABLE IF NOT EXISTS " + TBL_TABLE2 + "(" +
COL_TABLE2_ID + " INTEGER PRIMARY KEY, " +
COL_TABLE2_TREATDATE + " TEXT, " +
COL_TABLE2_HOSPITAL + " TEXT, " +
COL_TABLE2_CITY + " TEXT, " +
COL_TABLE2_ID2 + " INTEGER REFERENCES " + TBL_TABLE1 + "(" + COL_TABLE1_ID + ")" +
")";
db.execSQL(table2_crtsql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
@Override
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
db.setForeignKeyConstraintsEnabled(true);
}
public long addTable1Row(String name, String fathername, String dob, int age) {
ContentValues cv = new ContentValues();
cv.put(COL_TABLE1_NAME,name);
cv.put(COL_TABLE1_FATHERNAME,fathername);
cv.put(COL_TABLE1_DOB,dob);
cv.put(COL_TABLE1_AGE,age);
return mDB.insert(TBL_TABLE1,null,cv);
}
public long addTable2Row(String treatdate, String hospital, String city, long table1_reference) {
ContentValues cv = new ContentValues();
cv.put(COL_TABLE2_TREATDATE,treatdate);
cv.put(COL_TABLE2_HOSPITAL,hospital);
cv.put(COL_TABLE2_CITY,city);
cv.put(COL_TABLE2_ID2,table1_reference);
return mDB.insert(TBL_TABLE2,null,cv);
}
public Cursor getAllFromTable1JoinedWithTable2() {
String tables = TBL_TABLE1 +
" JOIN " + TBL_TABLE2 +
" ON " +
TBL_TABLE2 + "." + COL_TABLE2_ID2 +
" = " +
TBL_TABLE1 + "." + COL_TABLE1_ID;
return mDB.query(tables,null,null,null,null,null,null);
}
}
这将实例化一个DBHelper对象,然后使用可用的方法向每个表中添加一些行。表1中添加了2行,表2中添加了3行(弗雷德有2次就诊)。
然后根据table2和table1之间的关系提取数据,因此提取了3行。这些都转储到系统日志中。
public class MainActivity extends AppCompatActivity {
DBHelper mDBhlpr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDBhlpr = new DBHelper(this);
//Add Fred
long parentid = mDBhlpr.addTable1Row("Fred","George","2000-01-01",19);
// Add a hospital visit for Fred
mDBhlpr.addTable2Row("2019-01-01","Prince Fred","Timbukto",parentid);
//Add Mary and a hospital visit all in one go
mDBhlpr.addTable2Row("2019-01-02","St Barts","Springfield",
mDBhlpr.addTable1Row("Mary","Tom","1999-12-23",19)
);
// Another hospital visit for Fred
mDBhlpr.addTable2Row("2019-01-03","John Radcliffe","Oxford",parentid);
// Get the joined data and dump it to the log
Cursor csr = mDBhlpr.getAllFromTable1JoinedWithTable2();
DatabaseUtils.dumpCursor(csr);
}
}
日志包括:-
I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@3fd1bbe9
I/System.out: 0 {
I/System.out: id=1
I/System.out: name=Fred
I/System.out: fathername=George
I/System.out: dob=2000-01-01
I/System.out: age=19
I/System.out: m_id=1
I/System.out: treatdate=2019-01-01
I/System.out: hospital=Prince Fred
I/System.out: city=Timbukto
I/System.out: id2=1
I/System.out: }
I/System.out: 1 {
I/System.out: id=2
I/System.out: name=Mary
I/System.out: fathername=Tom
I/System.out: dob=1999-12-23
I/System.out: age=19
I/System.out: m_id=2
I/System.out: treatdate=2019-01-02
I/System.out: hospital=St Barts
I/System.out: city=Springfield
I/System.out: id2=2
I/System.out: }
I/System.out: 2 {
I/System.out: id=1
I/System.out: name=Fred
I/System.out: fathername=George
I/System.out: dob=2000-01-01
I/System.out: age=19
I/System.out: m_id=3
I/System.out: treatdate=2019-01-03
I/System.out: hospital=John Radcliffe
I/System.out: city=Oxford
I/System.out: id2=1
I/System.out: }
I/System.out: <<<<<