Iphone 如何将数组写入Objective-C的sqlite数据库?
我已经将数据库中的数据放入数组,现在我希望它使用insert查询将数据发送回数据库Iphone 如何将数组写入Objective-C的sqlite数据库?,iphone,objective-c,sqlite,Iphone,Objective C,Sqlite,我已经将数据库中的数据放入数组,现在我希望它使用insert查询将数据发送回数据库 如何将数据数组插入数据库?迭代数组的内容并建立insert查询。插入数据库与从数据库中读取几乎是相反的。您没有说明阵列的结构是什么,但以下是一些您应该能够适应需要的基本代码: // Assuming you have the sqlite database opened already // sqlite3 *sqldb = ...; // Also assuming you have an array of
如何将数据数组插入数据库?迭代数组的内容并建立
insert
查询。插入数据库与从数据库中读取几乎是相反的。您没有说明阵列的结构是什么,但以下是一些您应该能够适应需要的基本代码:
// 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);