-1

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...?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
bbbbbird1
  • 55
  • 2
  • 6

1 Answers1

0

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.

bryanmac
  • 38,941
  • 11
  • 91
  • 99
  • but if the DB is empty... why the `[rs columnCount]` return 5?(total numbers of my columns) – bbbbbird1 Jan 13 '12 at 03:31
  • good point - when you created the DB with the tool, did you just create the schema (tables) or did you also insert data (rows)? And if so, how many rows? BTW, you still need to copy to write ... – bryanmac Jan 13 '12 at 03:37
  • ... what's the output when you run the select statement from the cmdline? – bryanmac Jan 13 '12 at 03:38
  • OK - what's the default value (null?) and what's the output from the cmdline? – bryanmac Jan 13 '12 at 03:46
  • the default value is some string i type randomly... and I'm trying use cmdline to read the DB... – bbbbbird1 Jan 13 '12 at 03:50
  • 1
    I think the problem is here.. I never copy the DB file from resources to Document or lib... is that so ? – bbbbbird1 Jan 13 '12 at 04:01
  • I know that will cause a problem with writes - not sure about reads. You should copy so worth a try. Copy if not exist @ dest and open from dest. Also, rs next does log so look at log output. See src @ http://code.google.com/p/flycode/source/browse/trunk/fmdb/src/FMResultSet.m?r=25 – bryanmac Jan 13 '12 at 04:08
  • I'm sure the file is existing at the path by using `- (BOOL)fileExistsAtPath:(NSString *)path` the return Value is YES.... – bbbbbird1 Jan 13 '12 at 05:31