菜鸟教程小白 发表于 2022-12-12 09:35:33

ios - SQLite 数据库不可写


                                            <p><p>我有一个使用 Firefox SQLite Manager 插件创建的预填充 SQLite 数据库。
我已将数据库包含到我的项目中,添加到目标并复制到目标组的文件夹中。然后我创建了这个函数来复制文档文件夹中的数据库:</p>

<pre><code>-(void) createEditableDatabase{
    BOOL success;
    NSFileManager *fileManager = ;
    NSError *error;
    NSString *writableDB = [ stringByAppendingPathComponent:@&#34;DB.sqlite&#34;];
    success = ;
    if (success){
      return;
    }
    NSString *defaultPath = [[ resourcePath]stringByAppendingPathComponent:@&#34;DB.sqlite&#34;];
    error = nil;
    success = ;
    if (!success){
      NSAssert1(0, @&#34;Failed to create writable database file:%@&#34;, );
    }
    if (error){
      NSLog(@&#34;error = %@&#34;, );
    }
}
</code></pre>

<p>然后我在 <code>-(void)viewDidLoad</code> 中调用这个函数,如果我 checkin 模拟器文件夹,一旦我启动应用程序就会出现一个 DB 副本。
该应用程序运行良好,我可以使用从数据库中检索到的数据填充 <code>UICollectionView</code>。
然后,当我尝试插入一些数据时,我没有收到错误,但没有数据被添加到数据库中。
这是我在 <code>MyViewController.h</code> 中使用的代码:</p>

<pre><code>@property (nonatomic) NSString *DBPath;
@property (nonatomic) sqlite3 *myAppSQLITE;
</code></pre>

<p>这是我在 <code>MyViewController.m</code> 中使用的代码:</p>

<pre><code>-(IBAction)done:(id)sender{
    DBPath = [ stringByAppendingPathComponent:@&#34;DB.sqlite&#34;];
    const char *dbpath = ;
    sqlite3_stmt *statement;
    if (sqlite3_open(dbpath, &amp;myAppSQLITE) == SQLITE_OK){
      NSString *querySQL = ;
      const char *query_stmt = ;
      if (sqlite3_prepare_v2(myAppSQLITE, query_stmt, -1, &amp;statement, NULL) == SQLITE_OK){
            sqlite3_finalize(statement);
      }
      sqlite3_close(myAppSQLITE);
    }
}
</code></pre>

<p>我没有收到错误/警告,但数据从未更新,即使使用 <code>;</code>。如果我在 Firefox 中使用 SQLite Manager 运行相同的查询,一切正常。如果我使用 SQLite 管理器打开 Simulator Documents 文件夹中的数据库,则数据库完好无损,没有更新数据。我在 iPad 上运行应用程序的结果相同。
我该如何解决这个问题?
提前谢谢你。</p>

<p>P.S:大卫的建议是对的,这是创建可编辑数据库的更好方法:</p>

<pre><code>- (void) createEditableDatabase{
    BOOL success;
    NSFileManager *fileManager = ;
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = ;
    NSString *writableDBPath = ;
    success = ;
    if (success){
      return;
    }
    NSString *defaultDBPath = [[ resourcePath] stringByAppendingPathComponent:@&#34;DB.sqlite&#34;];
    success = ;
    if (!success) {
      NSAssert1(0, @&#34;Failed to create writable database file with message &#39;%@&#39;.&#34;, );
    }
}
</code></pre>

<p>P.S2:这可能是一个适当的错误检查,让我知道它是否正确。</p>

<pre><code>int rc;
while ((rc = sqlite3_step(statement)) == SQLITE_ROW){
    NSLog(@&#34;ROW&#34;);
}
if (rc != SQLITE_DONE){
    NSLog(@&#34;%s: step error: %d: %s&#34;, __FUNCTION__, rc, sqlite3_errmsg(myAppSQLite));
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您永远不会通过调用 <code>sqlite3_step</code> 实际执行查询。</p>

<p>你想要:</p>

<pre><code>if (sqlite3_prepare_v2(myAppSQLITE, query_stmt, -1, &amp;statement, NULL) == SQLITE_OK){
    sqlite3_step(statement); // add appropriate error checking
    sqlite3_finalize(statement);
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - SQLite 数据库不可写,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/23369131/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/23369131/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - SQLite 数据库不可写