iOS sqlite blob写入失败并显示“未知错误”

时间:2016-04-07 16:17:31

标签: ios sqlite blob

我有一个基于SQLite的应用程序,可以在Windows,Android和Java上正常运行。我正在尝试将其移植到iOS:设备运行的是iOS 9.3.1。

除了blob写入之外,其他所有功能都有效,在sqlite_step调用中出现“未知错误”。该代码基于this SE post

这是低级SQLite界面的精简版本:查询来自下一级别。

NSLog (@"Preparing query {%s}", query);
rc = sqlite3_prepare_v2 (hdb, query, -1, &stmt, &tail);
if (rc == SQLITE_OK) {
   NSLog (@"Binding blob: size= %d", (int)[blob length]);
   rc = sqlite3_bind_blob (stmt, 1, [blob bytes], (int) [blob length], SQLITE_STATIC);
   if (rc == SQLITE_OK) {
      rc = sqlite3_step (stmt);
      if (rc != SQLITE_OK)
        [self sql_error: @"sqlDo" line: __LINE__];
   }
   if (sqlite3_finalize (stmt) != SQLITE_OK)
     [self sql_error: @"sqlEndQuery" line: __LINE__];

 }

这是NSLog输出:

Preparing query {INSERT OR REPLACE INTO xxx VALUES (18356, '', ?)}
Binding blob: size= 5388
SQL error at line 157 in sqlDo: query:{INSERT OR REPLACE INTO xxx VALUES (18356, '', ?)} message:{unknown error}

1 个答案:

答案 0 :(得分:1)

执行sqlite3_step INSERT语句后,结果为SQLITE_DONE,而不是SQLITE_OK

所以,你想要:

rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
    [self sql_error: @"sqlDo" line: __LINE__];