sqlite prepare语句错误 - 没有这样的表 [英] sqlite prepare statement error - no such table

查看:186
本文介绍了sqlite prepare语句错误 - 没有这样的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的sqlite prepare语句有些困难。我得到一个错误,说我的桌子不存在,虽然我已经检查了多个地方,但确实存在,所以我很困惑。

I'm having some difficulty with my sqlite prepare statement. I get an error saying my table does not exist, although I've checked in multiple places for it, and it does exist, so I'm confuzzled.


  • 该文件位于正确的iPhone模拟器应用程序文件夹中

  • 该文件已添加到我的项目中可以在项目导航器中查看

  • 它也在我的构建阶段 - 复制捆绑资源区域。

  • 我已经清理并再次开始运行。

  • 数据库存在且运行我的sql语句只能获得
    我预期的结果。

  • The file is in the correct iPhone Simulator Application folder
  • The file is added to my project and viewable in the project navigator
  • It is also in my build phases- Copy Bundle Resources area.
  • I've cleaned and started running again.
  • The database exists and running my sql statement gets me just the results I expected.

- (NSMutableArray *) getMyWorkout{
NSMutableArray *workoutArray = [[NSMutableArray alloc] init];
@try {
NSFileManager *fileMgr = [NSFileManager defaultManager];

NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"IOSDB.sqlite"];
    NSLog(@"Db path is %@",dbPath);
BOOL success = [fileMgr fileExistsAtPath:dbPath];

if(!success) {
    NSLog(@"Cannot locate database file '%@'.", dbPath);
}

if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)){
    sqlite3_close(db);
    NSLog(@"Failed to open database with message '%s'.", sqlite3_errmsg(db));
}

const char *sql = "SELECT Id, type, difficulty, duration, description FROM workoutTbl";
sqlite3_stmt *sqlStatement;

if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK){

NSLog(@"%s Prepare failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(db), sqlite3_errcode(db));
}        //...


当我运行它时,我得到文件路径和以下错误

When I run it, I get the file path and the following error

    2013-02-01 18:07:08.060 TriShake[9251:c07] -[MyWorkoutList getMyWorkout] Prepare failure 'no such table: workoutTbl' (1)

我已经检查了这些其他问题,但一直无法找到解决方案

I've checked out these other questions, but have been unable to find a solution

  • Sqlite Prepare Failed: no such table<tablename>
  • Sqlite3 gives "no such table" error on iPhone

据我所知,如果数据库路径不存在,sqlite3_open()会为您创建一个空数据库,但是我知道它存在,所以沮丧随之而来。非常感谢你给我的任何帮助或指导。

I understand sqlite3_open() creates an empty database for you if the database path does not exist, but i know it exists, so frustration ensues. Any help or guidance you could give me would be much appreciated.

推荐答案

就你眼前的问题而言,这将是一个问题。简单。

In terms of your immediate problem, it's going to be something simple.


  1. 你说你已经清理并再次构建了,但是你真的从你的模拟器中删除了旧的应用程序?手动删除应用程序,或者更简单,只需从iOS模拟器菜单中选择重置内容和设置即可完全重置模拟器。有时Xcode不知道要复制哪些文件(特别是在这种情况下,你在设备上运行它可能会改变模拟器包中文件的时间戳!)

  1. You say you've "cleaned and built again", but have you actually removed the old app from your simulator? Either remove the app manually, or, easier, just reset the simulator entirely by choosing "reset content and settings" from the "iOS Simulator" menu. Sometimes Xcode is not good about knowing what files to copy over (particularly in a case like this where your running it on the device may be changing the timestamp of the file in the simulator's bundle!)

再次运行该应用程序。

如果应用程序无法正常工作,请打开模拟器文件夹中的数据库Mac并检查数据库以确保表格在那里并且正如您预期的那样。因此,导航到应用程序,打开捆绑包(您可能必须选择显示包内容选项),确认数据库的存在,但同样重要的是,在您喜欢的Mac中打开数据库的这个特定副本 sqlite3 选择工具并确认那里存在的表。

If the app doesn't work as expected, open up the database in the simulator folder from the Mac and check out the database to make sure the table is there and precisely as you expected it to be. So navigate to the app, open the bundle (you may have to choose the "show package contents" option), confirm the existence of the database, but just as importantly, open it up this particular copy of the database in your favorite Mac sqlite3 tool of choice and confirm the existence of the table there.

告诉我们你的发现。同样,它必须是简单的,例如:

Let us know what you find. Again, it's got to be something simple such as:


  • 也许重建应用程序的过程并没有重新安装所有内容;我偶尔遇到一些问题,Xcode选择在我的模拟器上安装时没有重新复制某些内容;

  • Perhaps the process of rebuilding the app was not reinstalling everything; I've occasionally had problems where Xcode elected to not re-copy something during the install on my simulator;

也许您项目中的数据库被意外地放入一个子目录,更糟糕的是,你可能有两个副本坐在不同的目录中;

Perhaps your database in your project was accidentally put in a subdirectory, worse, you might have two copies sitting in different directories;

也许你的Xcode项目中的数据库丢失了(或者有一个错字或(特别是在设备的情况下)在表或文件的名称中具有不正确的文件名大写;

Perhaps the database in your Xcode project is missing (or has a typo or (esp in the case of the device) has incorrect filename capitalization) in the name of the table or file;

等等。

对于很多这些错误,在完全重置模拟器本身之前,您不会发现问题。它可能有一百万件小事,但希望完全重置模拟器并重新开始将帮助您找到问题。当涉及到这些问题时,它总是总是

For a lot of these errors, you won't notice the problem until you completely reset the simulator itself. There are a million little things it could be, but hopefully completely resetting the simulator and starting over will help you find the issue. It's always something simple when it comes to these sorts of issues.

其他一些小的观察:


  1. 您可能不应该从捆绑包中打开数据库。以编程方式将其从捆绑包复制到 Documents 文件夹,然后从那里打开数据库。我知道这似乎是不必要的,但这有很多原因很重要(如果db在应用程序运行期间发生变化,如果db意外地创建了你,请不要让Xcode对变化的事情感到困惑(即使只是文件时间戳)在Xcode的背后更改的捆绑包等。)

  1. You probably should not be opening databases from the bundle. Programmatically copy it from the bundle to the Documents folder, and open the database from there. I know it seems unnecessary, but it's important for a myriad of reasons (if db changes during operation of the app, if db accidentally gets created on you, don't let Xcode get confused about things that changed (even if only file timestamps) in the bundle changing behind Xcode's back, etc.)

如果你需要数据库,你应该使用 sqlite3_open_v2 ,使用SQLITE_OPEN_READWRITE或SQLITE_OPEN_READONLY标记(但执行包括 SQLITE_OPEN_CREATE )。它会让头痛给sqlite一个机会为你创建一个空白数据库,或者修改它,所以永远不要给它机会。

You should, if you need the database to be there, use sqlite3_open_v2, using either SQLITE_OPEN_READWRITE or SQLITE_OPEN_READONLY for flags (but do not include SQLITE_OPEN_CREATE). It causes headaches to ever give sqlite a chance to create a blank database for you, or otherwise modify it, so never give it an opportunity to so.

这篇关于sqlite prepare语句错误 - 没有这样的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆