ios - 从iPhone设备读取和写入sqlite数据库中的数据时出错

标签 ios ios4 ios5

我创建了一个应用程序,将数据存储到数据库中并读取它。我创建了一个 .sql 文件并将其添加到资源文件夹中。我可以通过模拟器保存和检索数据,我的应用程序在模拟器上运行良好,但是当我在设备上安装相同的应用程序时,我收到错误“没有这样的表”

任何人都可以提供此问题的解决方案吗?

这是我正在使用的代码块:

///////AppDelegate.m 内部>>开始>>>

/////检查数据库是否存在>>>>

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;

NSString *dbPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TempDatabase.sql"];
BOOL success = [fileManager fileExistsAtPath:dbPath]; 

if(!success) {

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TempDatabase.sql"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];


    if (!success) 

        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);  

} else {

    NSLog(@"Database file found at path %@", dbPath);

}

///////AppDelegate.m 内部 <<

///////在数据库中写入文件的逻辑>>>开始>>

           sqlite3 *pDb; 
    char *databaseName; 

    databaseName = @"TempDatabase.sql";

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    //databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

    databasePath =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TempDatabase.sql"];

    //[self copyDatabaseIfNeeded];


    if(sqlite3_open([databasePath UTF8String], &pDb) == SQLITE_OK) 
    {

        const char *sqlStatement = "INSERT INTO tablename (name) VALUES(?)";

        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(pDb, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
        {


             sqlite3_bind_text( compiledStatement, 1, [strTxtFldValue UTF8String], -1, SQLITE_TRANSIENT);
        }
        if(sqlite3_step(compiledStatement) != SQLITE_DONE ) 
        {
             NSLog( @"~~~~~~~~~~~ 1 Error: %s", sqlite3_errmsg(pDb) );
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Name name is not added." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            [alert release];

        } else {

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Confirmation" message:@"name is added successfully." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            [alert release];

                }
        sqlite3_finalize(compiledStatement);
    }

    sqlite3_close(pDb);

///////将文件写入数据库的逻辑 <<

最佳答案

您是否尝试过在运行时创建数据库。在运行时执行查询并创建表,尝试一下,这肯定会对您有所帮助。

关于ios - 从iPhone设备读取和写入sqlite数据库中的数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8851079/

相关文章:

ios - singleton swift 的困难

ios - 仪器中的 xcrun 错误

ios - UIScrollView、PanGestureRecognizer 和 iOs 4

iphone - 点击 UITextField 时在 UIActionSheet 中显示 UIDatePicker

iphone - MKAnnotation:iOS 5.0 中的标题

ios - scrollview 在 iPhone 中不滚动

ios - 更改 UITableView 静态单元格中的标题

iphone - 一步一步为 iOS 编写自己的手机间隙插件

ios - 两个标签栏项目应该显示相同的 ViewController

iphone - iOS5 detail UITableView 的 UIViewController 我应该为每个选择的行重置或重新分配吗?