【问题标题】:Sqlite doesn't work on actual device (on my iPhone)Sqlite 在实际设备上不起作用(在我的 iPhone 上)
【发布时间】:2013-03-07 14:14:00
【问题描述】:

我编写了一个使用 Sqlite 数据库的简单应用程序。它在 iPhone 模拟器上运行良好,但在我的 iPhone 上不起作用。

-(NSString *) getFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,YES);
NSString *documentsDir=[paths objectAtIndex:0];


return [documentsDir stringByAppendingPathComponent:@"database.sql"];
}

-(void)openDatabase
{
    //Open

    if (sqlite3_open([[self getFilePath] UTF8String], &db) != SQLITE_OK ) {
        sqlite3_close(db);
        NSAssert(0, @"Database failed to open.");
    }
}

启动应用后在 Xcode 上的输出:

2013-03-07 02:12:16.525 SqliteWorkApp[464:907] *** Assertion failure in -[SqliteWorkAppViewController insertRecord], /Users/cmltkt/Objective-C Apps/SqliteWorkApp/SqliteWorkApp/SqliteWorkAppViewController.m:77
2013-03-07 02:12:16.529 SqliteWorkApp[464:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error updating table.'
*** First throw call stack:
(0x3398e2a3 0x3b82797f 0x3398e15d 0x34263ab7 0x19b3b 0x195bd 0x357b5595 0x357f5d79 0x357f1aed 0x358331e9 0x357f683f 0x357ee84b 0x35796c39 0x357966cd 0x3579611b 0x374885a3 0x374881d3 0x33963173 0x33963117 0x33961f99 0x338d4ebd 0x338d4d49 0x357ed485 0x357ea301 0x19147 0x3bc5eb20)
libc++abi.dylib: terminate called throwing an exception

插入记录函数:

-(void)insertRecord
{
    NSString *sql = [NSString stringWithFormat:@"INSERT OR REPLACE INTO 'countries' ('name', 'flag') " "VALUES ('Sample Data','Sample Data')"];

    char *err;
    if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err)
        != SQLITE_OK) {
        sqlite3_close(db);
        NSAssert(0, @"Error updating table.");
    }
}

【问题讨论】:

  • 错误在您的insertRecord 方法中。显示该方法的代码。
  • 另外,实际文件是否存在于您设备上的 Documents 目录中?正如所写,您的应用程序只是打开一个空的(不存在的)数据库。您要么需要复制包含所有空表的预制数据库文件,要么需要执行 SQL 来创建表。
  • @rmaddy 说得很好;除非您在捆绑包中包含预配置的数据库,否则您需要做的第一件事是创建数据库及其架构。
  • 我在问题中添加了 insertRecord 函数,是的,database.sql 位于文档目录中?
  • @CemilTokatlı 应用程序第一次运行时,数据库文件将不存在。您的应用需要初始化数据库文件。您的插入将失败,因为该表不存在。您是否有在 Documents 目录中的数据库文件中创建表的代码?

标签: iphone ios sqlite


【解决方案1】:

我有同样的问题,

我在应用程序中使用了 SQLite Db 便携式文件,它在模拟器上运行得很好,但在真实设备上却不行。

所以在挖了很多之后,我发现,当我将sqlite db文件拖到我的项目中时,Xcode并没有将它添加到捆绑资源中。

请!往这边走

  1. 选择您的项目,转到“构建阶段”
  2. 将您的 database.sqlite(据我所知应该是.sqlite)文件添加到捆绑资源中。

为了处理所有 SQLite 的东西,我的数据库助手类代码是

#import "ASCODBHelper.h"
#import <sqlite3.h>

@implementation ASCODBHelper

static ASCODBHelper *db;


+(ASCODBHelper *)database{

    if (db == nil) {

        db = [[ASCODBHelper alloc] init];
    }

    return db;
}

- (id)init{

    self = [super init];

    if (self) {

        NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"IOSMeeting" ofType:@"sqlite"];

        if (sqlite3_open([sqLiteDb UTF8String], &db) != SQLITE_OK) {

            NSLog(@"Failed to open database!");       
        }

    }
    return self;
}

-(void)getPresentationDeatilById:(NSString *)presentationid andSessionId:(NSString *)sessionid{

    NSInteger pId = [presentationid integerValue];
    NSInteger sId = [sessionid integerValue];

    NSString *queryString = [[NSString alloc] initWithFormat:@"SELECT distinct mediaID,mediaURL,meetingName,trackName FROM Media WHERE presentationID='%d' and sessionID ='%d'",pId,sId];

    NSLog(@"query is  : %@",queryString);

    sqlite3_stmt *selectStatement;

    if (sqlite3_prepare_v2(db, [queryString UTF8String], -1, &selectStatement, nil) == SQLITE_OK) {
        while (sqlite3_step(selectStatement) == SQLITE_ROW) {

            char *mediaid = (char *) sqlite3_column_text(selectStatement, 0);
            char *mediaurl = (char *) sqlite3_column_text(selectStatement, 1);
            char *meetingname = (char *) sqlite3_column_text(selectStatement, 2);
            char *topicname = (char *) sqlite3_column_text(selectStatement, 3);

            NSString *mediaID = [[NSString alloc] initWithUTF8String:mediaid];
            NSString *mediaURL = [[NSString alloc] initWithUTF8String:mediaurl];
            NSString *topicName = [[NSString alloc] initWithUTF8String:topicname];
            NSString *meetingName = [[NSString alloc] initWithUTF8String:meetingname];

            //you can log here results            

        }
        sqlite3_finalize(selectStatement);
    }  
}        

@end

这是我如何使用它的代码。

只导入

#import "ASCODBHelper.h"

并以这种方式调用我们的 db 辅助方法

[[ASCODBHelper database] getPresentationDeatilById:presentationId andSessionId:sessionId];

如果需要帮助,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多