0

I have a project have to save some register into to database SQLite, but at the same time insert into another table.

Example:

tables : contact_enc contact_det
contact_enc columns: id, name, telefono
contact_det columns: id, id_ce, name, address, status

I need to insert into contact_enc the field and same times contact_det the field.

4

3 回答 3

2

只需使用两次插入操作

于 2012-12-19T16:05:06.280 回答
1
String sql = "INSERT INTO contact_enc yourdatabase"; 
long lastID = this.database.execSQL(sql); 

int lastInsertId = Integer.ValueOf(lastId);

if(lastInsertId > 0) {
    String sql2 = "INSERT INTO contact_det (id_ce) VALUES(lastInsertId)"; 
    this.database.execSQL(sql2);
}
于 2012-12-19T16:17:35.817 回答
0

如果 的idcontact_enc定义为INTEGER PRIMARY KEY,您可以使用last_insert_rowid()函数,就像下面的 SQL 语句一样,一个接一个地执行:

BEGIN TRANSACTION;
INSERT INTO contact_enc (name, telefono) VALUES (:Name, :Telefono);
INSERT INTO contact_det (id_ce, name, address, status) SELECT last_insert_rowid(), :Name, :Address, :Status;
COMMIT;

(注意 , :Name, :Telefono, :Name,:Address:Status是按名称绑定的参数。如果您希望按数字绑定参数,这在 Android 中似乎更容易做到,只需将每个占位符替换为 ? 问号)。

于 2012-12-20T17:33:06.287 回答