简体   繁体   中英

Why I can't read my SQlite with FMDB in IOS

I've create a DB file from firefox tool SQlite manager names myTestDB

the db has a table names ViewController1,and the table content 5 columns(there value are all TEXT)

I want read the value by columns name,so here's my code....

- (void)viewDidLoad
{
[super viewDidLoad];
NSString *myDbPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myTestDB.sqlite"];
FMDatabase *myDB = [FMDatabase databaseWithPath:myDbPath];
if (![myDB open]) {      
    NSLog(@"Could not open db.");    
    return ;
}
else
{
    NSLog(@"DB open!");
}
FMResultSet *rs = [myDB executeQuery:@"SELECT * FROM ViewController1"]; 
for (int i =0; i<[rs columnCount]; i++) {
    NSLog(@"%@",[rs stringForColumnIndex:i]);
} 
}

there's many sample codes use the method

while([rs next])
{
//Loading somethin...
}

but if i use it,the code won't get into the while loop even once

and my code just log 5 (null) to me....

why is that...?

thanks for your help!

您也可以用中文回答我,謝謝您!

Try logging out the complete path in the emulator and then go to that path in terminal and use the sqlite3 cmdline tool to: - verify the db is there and - you can execute the query.

http://zetcode.com/databases/sqlitetutorial/tool/

Remember that sqlite is lazy - it will create a DB on demand (on first write) so you may be creating an empty database on demand instead of opening the DB you think you're opening.

You also need to copy the db from resources to library or documents if you ever want to write to it. The DB in your resources should be a starting template.

EDIT:

The suggestions above were not the cause. I will leave the answer and not delete to help others know what isn't the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM