如何以编程方式将多行插入到iOS的sqlite3表中?这是我目前的方法的代码片段:在SQLite3中插入多行
sqlite3 *database;
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "insert into TestTable (id, colorId) VALUES (?, ?)";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
for (int i = 0; i < colorsArray.count; i++) {
sqlite3_bind_int(compiledStatement, 1, elementId);
long element = [[colorsArray objectAtIndex:i] longValue];
sqlite3_bind_int64(compiledStatement, 2, element);
}
}
if(sqlite3_step(compiledStatement) == SQLITE_DONE) {
sqlite3_finalize(compiledStatement);
}
else {
NSLog(@"%d",sqlite3_step(compiledStatement));
}
}
sqlite3_close(database);
这样,我只得到了第一行插入,我怎么能告诉我希望每个“for”循环是一个行插入源码?我找不到任何这样的例子...
谢谢!
如果最后一次插入失败,该代码会忘记调用'sqlite3_finalize' /'reset'。请不要在'sqlite3_finalize'之前调用'sqlite3_reset'是无害的。 –
明白了,谢谢! –
嗨(int i = 0; i