ios - SQLite 使用 FMDB : Insert record not working/no such table
<p><p>请帮我找出问题所在。 </p>
<p>插入语句不起作用,当我检查保存在 <strong>/Users/jppangilinan/Library/Application Support/iPhone Simulator/4.3/Applications/61BBA03F-C240-414D-9A64-6CE3B34DF9C2/Documents/person.sqlite3</strong> 似乎保存在该位置的数据库没有任何表,这就是插入语句不起作用的原因。为什么它没有在我的项目的资源文件夹中复制我的 sqlite db? TIA</p>
<pre><code>NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = ;
NSString *path = ;
FMDatabase *database = ;
;
;
NSString *query = ;
NSLog(@" %@",path);
NSLog(@" %@",query);
BOOL y= ;
if (!y)
{
NSLog(@"insert failed!!");
}
NSLog(@"Error %d: %@", , );
;
;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>为了在应用程序的 Documents 文件夹中访问您的资源数据库,您首先需要在应用程序启动时将其复制到那里。</p>
<p>如果数据库文件在你的 xcode 项目的资源文件夹中,它不会自动复制到应用程序的文档目录中。</p>
<p>为此,您可以使用:</p>
<pre><code>NSString *documentsDirectory = ;
NSError *error;
NSString *databasePath = [[ resourcePath] stringByAppendingPathComponent:@"person.sqlite3"];
[ copyItemAtPath:databasePath
toPath:
error:&error];
</code></pre>
<p>您将能够使用现有代码访问现在位于文档文件夹中的数据库。</p>
<p>发生的情况是,由于没有复制现有的 DB 文件,当您调用 <code>databaseWithPath</code> 时会创建一个新的空文件,这就是您收到 <code>Error 1: no 的原因这样的表:人</code>错误。</p>
<p>来自 <a href="https://github.com/ccgus/fmdb" rel="noreferrer noopener nofollow">FMDB documentation</a> :</p>
<blockquote>
<p>Database Creation</p>
<p>An FMDatabase is created with a path to a SQLite database file. This
path can be one of these three:</p>
<p><strong>A file system path. The file does not have to exist on disk. If it
does not exist, it is created for you.</strong>
An empty string (@""). An empty database is created at a temporary location. This database is deleted with the FMDatabase connection is
closed.</p>
<p>NULL. An in-memory database is created. This database will be
destroyed with the FMDatabase connection is closed.</p>
<p>FMDatabase *db = ;</p>
</blockquote></p>
<p style="font-size: 20px;">关于ios - SQLite 使用 FMDB : Insert record not working/no such table ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/8499588/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/8499588/
</a>
</p>
页:
[1]