Search code examples
iossqlitefmdb

SQLite file in project catalogue, use fmdb open it failed


I create a table in SQLite Manager and export it to desktop , its name is db_fo_dic.sqlite, and I can open the db_fo_dic.sqlite use SQLite Manager, so I copy the db_fo_dic.sqlite to my Xcode project catalogue, I try to open the sqlite file by FMDB, but it failed:

NSString *path = [[NSBundle mainBundle] pathForResource:@"db_fo_dic" ofType:@"sqlite"];

FMDatabase *db = [FMDatabase databaseWithPath:path];

The result info is : 2016-08-01 19:22:34.955 test_sqlite[15380:425242] The FMDatabase <FMDatabase: 0x7ff589c38f10> is not open.

This is the location path of my sqlite: /Users/youpude/Library/Developer/CoreSimulator/Devices/87D0245A-47F7-4741-81B2-209625CB3C5E/data/Containers/Bundle/Application/47B3AD1F-C08E-4E34-8858-494A95FB9EA5/test_sqlite.app/db_fo_dic.sqlite


Solution

  • 8 hours go through, I find the method to solve my problem finally.

    #import "FMDatabaseQueue.h"
    
    static FMDatabaseQueue *_queue;
    

    in - (void)viewDidLoad:

    _queue = [FMDatabaseQueue databaseQueueWithPath:path];

    and I can query my database:

    [_queue inDatabase:^(FMDatabase *db) {
    
            FMResultSet *result = [db executeQuery:@"select * from dic_content limit 10;"];
    
            while ([result next]) {
    
                NSString *str = [result stringForColumn:@"tit"];
    
                NSLog(@"%@",str);
            }
    
        }];
    

    I use this method to replace the previous method.