views:

51

answers:

2

I want to use sqlite3 for my iphone application. But it is not working. Scenario is :- On click event of a button a method should run and retrieve the data from the database. But My sequence of program is not going into

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    const char *sqlStatement = "select * from records";
    sqlite3_stmt *compiledStatement;
    0NSLog(@"%s", sqlite3_errmsg(database)); // Results - no error
-
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)    {
//here NSLog statement does not work.

There is no problem in opening a database. What should be the reason? Any solution appreciable. Thanks

+1  A: 

Try this: NSLog(@"%s", sqlite3_errmsg(database)); and see if there are some errors in your query.

Simon
no error... I edited my question that you can have a better perception.
alis
As I can see from your code, your if statement doesn't return SQLITE_OK.I use sqlite3 and I have separated this into two blocks, first I do the prepare, without the if, and then I use a `while(sqlite3_step(compiledStatement) == SQLITE_ROW)` to loop through the results. Try and see if it makes any changes.
Simon
@alis: You have the NSLog in the wrong place. You wantr to see what the error message from the prepare is. Put it in the else part of your if.
JeremyP
Well yes, you need to have the NSLog AFTER the prepare, you don't need to have the NSLog in the if
Simon
@Simon, @JeremyP: I follow your instruction and i got the error no such table: records(my table name). But My database has a table named records and it is also not empty. So, what will be the reason behind this?
alis
The reason is simple but sometimes hard to find, your app cannot find the database or theres no table with that name. Have you tried open the database with a external program, like SQLite Database browser?My first thoughts about this problem was and still are that the database is never created or that you try to access the database at wrong location.
Simon
A: 

Here is a modified example of my own code :

  NSString *file = @"youdatabase.db"
  sqlite3 *db;    
  if(sqlite3_open([file UTF8String], &db) == SQLITE_OK)
  {
    NSString *req = @"SELECT * FROM totoTable";
    sqlite3_stmt *returnReq;

    if(sqlite3_prepare(db, [req cStringUsingEncoding:NSUTF8StringEncoding], -1, &returnReq, NULL) == SQLITE_OK)
    {
      while (sqlite3_step(returnReq) == SQLITE_ROW)
      {
         //Printf first returned value
        NSLog(@"%s", (char*)sqlite3_column_text(returnReq, 0));
      }     
    }
    sqlite3_close(db);
  }

Hope it will help you ;-)

Human-Behind