Ios 在SQLite3中插入多行
如何以编程方式在iOS的sqlite3表中插入几行?这是我当前方法的代码片段:Ios 在SQLite3中插入多行,ios,xcode,sqlite,Ios,Xcode,Sqlite,如何以编程方式在iOS的sqlite3表中插入几行?这是我当前方法的代码片段: sqlite3 *database; if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) { const char *sqlStatement = "insert into TestTable (id, colorId) VALUES (?, ?)"; sqlite3_stmt *compiledStatement;
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);
sqlite3*数据库;
if(sqlite3_打开([filePath UTF8String],&database)==SQLITE_确定){
const char*sqlStatement=“插入测试表(id,colorId)值(?,)”;
sqlite3_stmt*编译语句;
if(sqlite3\u prepare\u v2(数据库,sqlStatement,-1,&compiledStatement,NULL)==SQLITE\u OK)
{
for(int i=0;i
这样,我只插入第一行,如何告诉sqlite我希望每个“for”循环都是行插入?我找不到任何这样的例子
谢谢 您必须运行以下语句:
sqlite3_step(compiledStatement) == SQLITE_DONE
每次插入后,在代码中,我看到您最后只运行了一次。我让它工作了,这是我的代码:
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) {
if (i == (colorsArray.count - 1))
sqlite3_finalize(compiledStatement);
else
sqlite3_reset(compiledStatement);
}
else {
NSLog(@"row insertion error");
}
}
}
}
sqlite3_close(database);
sqlite3*数据库;
if(sqlite3_打开([filePath UTF8String],&database)==SQLITE_确定){
const char*sqlStatement=“插入测试表(id,colorId)值(?,)”;
sqlite3_stmt*编译语句;
if(sqlite3\u prepare\u v2(数据库,sqlStatement,-1,&compiledStatement,NULL)==SQLITE\u OK)
{
for(int i=0;i
如果最后一次插入失败,此代码忘记调用sqlite3\u finalize
/reset
。请不要认为在sqlite3\u finalize
之前立即调用sqlite3\u reset
是无害的。Hi for(int i=0;i