无法访问iOS SQLite数据库:sqlite3_prepare_v2和';没有此类表格';sqlite3_errmsg
这是我第一次尝试在iOS中创建和访问SQLite db,我遵循了一个教程,但我无法做到这一点。以下是我遵循的步骤:无法访问iOS SQLite数据库:sqlite3_prepare_v2和';没有此类表格';sqlite3_errmsg,ios,ios5,sqlite,ios-simulator,Ios,Ios5,Sqlite,Ios Simulator,这是我第一次尝试在iOS中创建和访问SQLite db,我遵循了一个教程,但我无法做到这一点。以下是我遵循的步骤: 通过Firefox插件(“testDatabase”)和 包含多个列的表(“testTable”) 将“testDatabase.sqlite”文件保存在一个目录中 将此类文件拖放到我的“支持文件”组中 Xcode项目 在构建阶段>链接二进制文件中添加了“libsqlite3.dylib” 图书馆 在生成设置>链接>其他链接器标志> 调试和发布 在ViewController中,称
-(NSString *)copyDatabaseToDocuments {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"testDatabase.sqlite"];
if ( ![fileManager fileExistsAtPath:filePath] ) {
NSString *bundlePath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"testDatabase.sqlite"];
[fileManager copyItemAtPath:bundlePath toPath:filePath error:nil];
}
return filePath;
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"testDatabase.sqlite"];
sqlite3 *database;
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "insert into testTable (id) VALUES (?)";
sqlite3_stmt *compiledStatement;
if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_bind_int(compiledStatement, 1, newObject.ID);
}
if(sqlite3_step(compiledStatement) == SQLITE_DONE) {
sqlite3_finalize(compiledStatement);
}
}
sqlite3_close(database);
NSString*filePath=[[NSBundle mainBundle]pathForResource:@“testDatabase”类型:@“sqlite”]
但是它是空的,并且多次在模拟器中低估并重新部署了我的应用程序
提前感谢我通过简单地删除在/Users/[user]/Library/Application Support/iPhone Simulator/5.1/Applications/中生成的应用程序文件夹,最终实现了这一点。当运行模拟器时,将再次创建该文件夹当您构建应用程序时,您是否验证了testDatabase.sqlite文件实际上是应用程序包的一部分(与Info.plist和您的其他资源位于同一目录中?是的,它位于该目录中,并且也在目标的构建阶段>复制捆绑资源中列出。)