0

I have to make a project using sqlite, i would rather use simple plists, xml or even core data as i don't have any experience with sql. However it has to be able to read from sqlite database. I downloaded the FMDB wrapper and set it up quickly however when i try and read from my database it does not display in the tableview, Here is my code:

TermsArray = [[NSMutableArray alloc] init];

NSString *path  = [[NSBundle mainBundle] pathForResource:@"Database" ofType:@"sql"];
FMDatabase *db  = [[FMDatabase alloc] initWithPath:path];
[db open];

FMResultSet *fResult= [db executeQuery:@"SELECT * FROM country WHERE id=1"];

while([fResult next])
{

    CountryString = [fResult stringForColumn:@"name"];

    [CountryArray addObject:CountryString];

    NSLog(@"The data is %@=",CountryString);
}
[db close];
  }

and i display it in my tableview using this:

NSString *Text = [CountryArray objectAtIndex:indexPath.row];
cell.textLabel.text = Text;

Now i don't get any errors, however the array is always empty, in the console i don't get any warnings etc either but i noticed that the NSLOG never shows up, so i don't think that part is working. Heres my sql file in a text editor:

 CREATE TABLE country ( id INTEGER PRIMARY KEY, name VARCHAR(250),location TEXT,number INTERGER);
  INSERT INTO country (name,location,number) VALUES('UK','country location here',1);

Also how are sqlite files supposed to be like? I created one using a sqlite editor and it produced a sql file, when i open it in a text editor i just see sql statements?

It would be really great it someone could help me with this,

Thanks!

6
  • Is the file extension correct ? Most of the time, the file extension for SQLite DBs is .sqlite, not .sql.
    – DarkDust
    Jun 3, 2011 at 11:07
  • @DarkDust the extension for SQLite database files doesn't matter.
    – user142019
    Jun 3, 2011 at 11:08
  • Hey thanks, but i've tried both and their was no change.
    – ApiMail
    Jun 3, 2011 at 11:21
  • @WTP: It does matter insofar as the pathForResource:ofType: must match his filename in the bundle. If the file does not exist and SQLite isn't told explicitly to open the file read-only, then SQLite simply creates an empty DB which would totally explain the problem.
    – DarkDust
    Jun 3, 2011 at 11:26
  • Thanks but it is definitely the right filename.
    – ApiMail
    Jun 4, 2011 at 12:53

2 Answers 2

0

just copy the sql statements and run it in the sql console after that u have to backup the database using .backup

1
  • Hello, Thanks for the answer, i think this may be the problem as the .sql file is just a list of commands. How would i go about doing this? Thanks!
    – ApiMail
    Jun 4, 2011 at 12:57
0

I have fixed it, It was a problem with my sql file, it now works great! Thanks for all your comments!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.