Sqlite准备失败:没有这样的表< tablename> [英] Sqlite Prepare Failed: no such table<tablename>
问题描述
我已经创建了sqlite数据库.
I have created sqlite database.
我将这个sqlite文件添加到我的应用程序中,但是当我在模拟器中运行该应用程序并在文档目录中检查sqlite文件的路径时,那时我在创建的表中未在文档目录路径中打开sqlite文件出现.
I add this sqlite file in my application but when I run the app in the simulator and check the sqlite file's path in document directory and I open the sqlite file on document directory's path at that time the table which I have created does not appear.
在本地副本中,我也可以在项目文件夹中看到该表,但是可以在模拟器的路径上看到该表
In local copy I can see the table also in my project folder I can see the table but on the path of simulator I am not able to see the table
所以在我的控制台中,它向我显示了错误:
so in my console it shows me the error:
Sqlite准备失败:没有这样的表
Sqlite Prepare failed: no such table
推荐答案
我在下面的答复假定您的数据库格式正确且可以正常运行.您可以通过命令行轻松进行测试.在我自己的应用程序(所有标签栏控制器应用程序)中,我将数据库访问代码放置在应用程序委托中.应用程序委托中的两种方法如下:
My response below assumes that your database is properly formatted and functional. You could test easily via the command line. In my own apps, all tab bar controllers apps, I place the db access code in the app delegate. The two methods in the app delegate are as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions {
[self createEditableCopyOfDatabaseIfNeeded];
[self.window setRootViewController:rootController];
[window makeKeyAndVisible];
return YES;
}
也:
- (void)createEditableCopyOfDatabaseIfNeeded {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath =[documentsDir stringByAppendingPathComponent:@"dbName.sqlite"];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success)
{
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"dbName.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
这篇关于Sqlite准备失败:没有这样的表< tablename>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!