3
我已經把數據從數據庫中取出到數組中,現在我希望它通過使用插入查詢將其發送回數據庫。如何將數據數組插入數據庫中。如何將數組寫入sqlite數據庫的objective-c?
我已經把數據從數據庫中取出到數組中,現在我希望它通過使用插入查詢將其發送回數據庫。如何將數據數組插入數據庫中。如何將數組寫入sqlite數據庫的objective-c?
插入數據庫幾乎與讀取數據的方式相反。你沒有說你陣列的結構是什麼,但是這裏有一些骨架代碼,你應該可以適應你的需求:
// Assuming you have the sqlite database opened already
// sqlite3 *sqldb = ...;
// Also assuming you have an array of MyItems holding your data
// NSMutableArray *items = ...;
sqlite3_stmt *insert_statement;
// Prepare the insert statement
const char*sql = "INSERT INTO mytable (field1, field2, field3) VALUES(?,?,?)";
sqlite3_prepare_v2(sqldb, sql, -1, &insert_statement, NULL);
// Iterate over an array of dictionaries
for (MyItem *item in items) {
// Bind variables - assumed to all be integers
sqlite3_bind_int(insert_statement, 1, item.field1);
sqlite3_bind_int(insert_statement, 2, item.field2);
sqlite3_bind_int(insert_statement, 3, item.field3);
// Execute the insert
if (sqlite3_step(insert_statement) != SQLITE_DONE) {
NSLog(@"Insert failed: %s", sqlite3_errmsg(sqldb));
}
// Reset the statement
sqlite3_reset(insert_statement);
}
// release the statement
sqlite3_finalize(insert_statement);
遍歷數組的內容並建立INSERT
查詢。