ios - Sqlite 数据库锁定错误 - iOS
<p><p>我正在使用 sqlite 在表格 View 中使用插入和更新数据。我只能更新一次数据,下次尝试显示数据库已锁定。即使正在关闭数据库,请帮忙。下面是代码。</p>
<pre><code>-(void)saveUserCredential: (NSString *)email :(NSString *)userName :(NSString *)loginTime :(NSString *)source
{
NSDateFormatter *dateformate=[init];
; // Date formater
NSString *todayDate = ];
const char *dbpath = ;
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString* query = @"SELECT * from users";
int rc =sqlite3_prepare_v2(database, , -1, &statement, NULL);
if(rc == SQLITE_OK)
{
if(sqlite3_step(statement) == SQLITE_ROW)
{
NSString *updateSQL = ;
const char *update_stmt = ;
sqlite3_prepare_v2(database, update_stmt, -1, &statement, NULL );
//sqlite3_bind_int(statement, 1, 1);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"successfully updated");
}
else{
NSLog(@"Error while updating. '%s'", sqlite3_errmsg(database));
}
}
else
{
NSString *insertSQL = ;
NSLog(@"INS SQL: %@", insertSQL);
const char *insert_stmt = ;
sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"INSERTED");
}
else
{
NSLog(@"NOT INSERTED");
}
NSLog(@"hello ");
}
sqlite3_finalize(statement);
sqlite3_close(database);
}
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>你需要打电话</p>
<pre><code>sqlite3_finalize(statement);
</code></pre>
<p>对于每个成功的 <code>sqlite_prepare_v2</code> 调用。它应该是平衡的。还要为每个查询使用不同的 <code>sqlite3_stmt</code>。</p>
<p>所以你的代码需要修改如下:</p>
<pre><code>const char *dbpath = ;
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString* query = @"SELECT * from users";
int rc =sqlite3_prepare_v2(database, , -1, &statement, NULL);
if(rc == SQLITE_OK)
{
if(sqlite3_step(statement) == SQLITE_ROW)
{
NSString *updateSQL = ;
const char *update_stmt = ;
sqlite3_stmt *upStmt;
sqlite3_prepare_v2(database, update_stmt, -1, &upStmt, NULL );
//sqlite3_bind_int(upStmt, 1, 1);
if (sqlite3_step(upStmt) == SQLITE_DONE)
{
NSLog(@"successfully updated");
}
else
{
NSLog(@"Error while updating. '%s'", sqlite3_errmsg(database));
}
sqlite3_finalize(upStmt);
}
else
{
NSString *insertSQL = ;
NSLog(@"INS SQL: %@", insertSQL);
const char *insert_stmt = ;
sqlite3_stmt *inStmt;
sqlite3_prepare_v2(database, insert_stmt,-1, &inStmt, NULL);
if (sqlite3_step(inStmt) == SQLITE_DONE)
{
NSLog(@"INSERTED");
}
else
{
NSLog(@"NOT INSERTED");
}
NSLog(@"hello ");
sqlite3_finalize(inStmt);
}
sqlite3_finalize(statement);
sqlite3_close(database);
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - Sqlite 数据库锁定错误 - iOS,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/35622772/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/35622772/
</a>
</p>
页:
[1]