iOS - 连接到真实设备上的数据库时出错

Posted

技术标签:

【中文标题】iOS - 连接到真实设备上的数据库时出错【英文标题】:iOS - Error connecting to db on real device 【发布时间】:2015-06-22 05:57:14 【问题描述】:

我一直在为 ios 开发 Ionic Phonegap 项目。 Appdelegate.m 中实现了一种方法,它发出 AJAX 请求以从服务器下载文本文件,其中包含连接到另一台服务器的 URL,以便应用程序运行。

我做了两节课, WebContentWebCustomContent

WebContent.m 中,我将从文本文件中获取的特定 URL 插入到 sqlite DB,然后使用 WebCustomContent.m 检索它

参考以下代码块

-(NSString*)getDataBasePath

    //CHECK
    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* foofile = [documentsPath stringByAppendingPathComponent:@"webcontentdb.sqlite"];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];
    NSLog(@"%d", fileExists);


    //END OF CHECK



    //SIMULATOR
    NSString *databasePath1 = [[NSBundle mainBundle] pathForResource:@"webcontentdb" ofType:@"sqlite"];
   // return databasePath1;


    //REAL DEVICE
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];
    NSString* databasePath = [documentsDirectory stringByAppendingPathComponent:@"webcontentdb.sqlite"];
    return databasePath;



-(void)updateUserAgeRange:(NSString*)age

    NSString* databasePath = [self getDataBasePath];
    sqlite3 *database;


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

        NSString *query = [NSString stringWithFormat:@"update  user_setting set valstr =   '%@'  where keystr = 'AGE' ", age];

        NSLog(@"update %@" , query);

        const char * sql = [query UTF8String];
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(_database, sql, -1, &compiledStatement, NULL) == SQLITE_OK) 
            sqlite3_step(compiledStatement); // Here is the added step.
            NSLog(@"updateContact SUCCESS - executed command %@",query);
        
        else 
            NSLog(@"updateContact FAILED - failed to execute command %@",query);
        

        sqlite3_finalize(compiledStatement);

    
    else 
        //NSLog(@"pdateContact FAILED - failed to open database");
    

    sqlite3_close(database);




- (NSString *)getUserPreferenceValues:(NSString*)keystr 



    NSString *retval = [[NSString alloc] init] ;
    NSString *query = [NSString stringWithFormat:@"SELECT valstr  FROM user_setting where keystr = '%@'  " , keystr];

    NSLog(@" query  %@", query);


    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) 
        while (sqlite3_step(statement) == SQLITE_ROW) 


            char *nameChars = (char *) sqlite3_column_text(statement, 0 );

            NSString *name = [[NSString alloc] initWithUTF8String:nameChars];

            NSLog(@" valstr  %@", name);
            retval = name;


        
        sqlite3_finalize(statement);
    


    return retval;


-(void)insertDatabaseCommonValues:(NSString*)urlstr

    NSString* databasePath = [self getDataBasePath];


    sqlite3 *database;
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 

        NSString *query = [NSString stringWithFormat:@"delete from  url_preference"];


        const char * sql = [query UTF8String];
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(_database, sql, -1, &compiledStatement, NULL) == SQLITE_OK) 
            sqlite3_step(compiledStatement); // Here is the added step.
            // NSLog(@"updateContact SUCCESS - executed command %@",query);
        
        else 
            //NSLog(@"updateContact FAILED - failed to execute command %@",query);
        

        sqlite3_finalize(compiledStatement);

    
    else 
        //NSLog(@"pdateContact FAILED - failed to open database");
    




    //************************************INSERT************************************//

    //sqlite3 *database;
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 


        //NSLog(@"URL STRING %@",urlstr);

        NSString *query = [NSString stringWithFormat:@"insert into url_preference (name) values (  '%@'  ) ", urlstr];

        NSLog(@"inset %@" , query);

        const char * sql = [query UTF8String];
        sqlite3_stmt *compiledStatement;

        NSLog(@" error code.. %d",sqlite3_prepare_v2(_database, sql, -1, &compiledStatement, NULL));

        if(sqlite3_prepare_v2(_database, sql, -1, &compiledStatement, NULL) == SQLITE_OK) 
            sqlite3_step(compiledStatement); // Here is the added step.
            NSLog(@"updateContact SUCCESS - executed command %@",query);
        
        else 
            NSLog(@"updateContact FAILED - failed to execute command %@",query);
        

        sqlite3_finalize(compiledStatement);

    
    else 
        //NSLog(@"pdateContact FAILED - failed to open database");
    

    sqlite3_close(database);


在这里,当我打印 BOOL 变量 fileExists 时,它会打印 YES,这意味着数据库存在于 Documents 文件夹中。 但是插入和更新查询失败如下;

2015-06-22 11:18:18.215 App Name[5510:60b] URL http://www.google.lk
2015-06-22 11:18:22.082 App Name[5510:60b] 1
2015-06-22 11:18:24.103 App Name[5510:60b] success to open database!
2015-06-22 11:18:26.197 App Name[5510:60b] 1
2015-06-22 11:18:28.673 App Name[5510:60b] inset insert into url_preference (name) values (  'http://www.google.lk'  ) 
2015-06-22 11:18:28.676 App Name[5510:60b]  error code.. 1
2015-06-22 11:18:28.679 App Name[5510:60b] updateContact FAILED - failed to execute command insert into url_preference (name) values (  'http://www.google.lk'  ) 

我已经把数据库文件放到了项目文件夹中,如下图;

我似乎无法弄清楚我做错了什么。请帮忙。

【问题讨论】:

【参考方案1】:

这可能是文件权限问题吗?我建议在Finder中右击webcontentdb.sqlite文件,选择Get Info,在共享和权限下让所有用户都拥有Read & Write权限。

如果这不起作用,我会使用 SQLite 浏览器应用程序来验证数据库文件是否可以写入并且没有以任何方式损坏。

【讨论】:

以上是关于iOS - 连接到真实设备上的数据库时出错的主要内容,如果未能解决你的问题,请参考以下文章

iOS 9.1 导致我的 JSON NSURL 连接到 PHP 页面时出错

使用 socket.io 连接到带有套接字的 mysql 数据库时出错

同一设备上的多个 ios 应用程序可以连接到同一个外围设备吗?

初始化 ST-Link 设备时出错 - 无法连接到设备

将 Flutter 连接到真实设备中的 localhost 服务器

iOS:在真实设备上运行项目时出错