Skizz
Skizz

Reputation: 1271

Database not being found... iOS/SQLite

I working on an app that takes input from a text field and puts it into a string. I have a table with a field in it that I want to check the value of the string from the input against the value in the field in the database. I'm new to iOS and fairly new to SQLite.

Code:

-(IBAction)setInput:(id)sender
{
NSString *strStoreNumber;
NSString *strRegNumber;

strStoreNumber = StoreNumber.text;
strRegNumber = RegNumber.text;
lblStoreNumber.text = strStoreNumber;
lblRegNumber.text = strRegNumber;

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString* documentsDirectory = [paths lastObject];
//    NSString* databasePath = [documentsDirectory stringByAppendingPathComponent:@"tblStore.sqlite"];
NSString* databasePath = [[NSBundle mainBundle] pathForResource:@"tblStore" ofType:@"sqlite"];

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 
{

    NSLog(@"Opened sqlite database at %@", databasePath);

    //...stuff
} 
else 
{
    NSLog(@"Failed to open database at %@ with error %s", databasePath, sqlite3_errmsg(database));
    sqlite3_close (database);
}

NSString *querystring;

// create your statement
querystring = [NSString stringWithFormat:@"SELECT strStore FROM tblStore WHERE strStore = %@;", strStoreNumber];  

const char *sql = [querystring UTF8String];

NSString *szStore = nil;
NSString *szReg = nil;


if (sqlite3_prepare_v2(database, sql, -1, &databasePath, NULL)!=SQLITE_OK) //queryString = Statement
{
    NSLog(@"sql problem occured with: %s", sql);
    NSLog(@"%s", sqlite3_errmsg(database));
}
else
{
    // you could handle multiple rows here

    while (sqlite3_step(databasePath) == SQLITE_ROW) // queryString = statement
    {            
        szStore = [NSString stringWithUTF8String:(char*)sqlite3_column_text(databasePath, 0)];
        szReg = [NSString stringWithUTF8String:(char*)sqlite3_column_text(databasePath, 1)];
    } // while        

}

sqlite3_finalize(databasePath);

// Do something with data...  
}

It gets to the line "NSLog(@"Opened sqlite database at %@", databasePath);", so it appears as though it has access to the database. However, when I run the app, I get the "NSLog(@"sql problem occured with: %s", sql);" error, which I can see in the console. Additionally, in the console, it says "No such table: tblStore".

I created the table using the Firefox add-on SQLite Manager. I added the sqlite3 library to the project. I dragged and dropped the database table I created in SQLite manager into my project, above my two AppDelegate files and my two ViewController files.

Any help or input would be greatly appreciated. Thanks!

EDIT: I have properly added the file to the project, and it appears as though the table is found now. Now I have some strange warnings, though:

"Incompatible pointer types passing 'const char *' to parameter of type 'sqlite3_stmt *' (aka 'struct sqlite3_stmt *')"

This warning appears on the following lines of code:

if (sqlite3_prepare_v2(database, sql, -1, &databasePath, NULL)!=SQLITE_OK)

while (sqlite3_step(sql) == SQLITE_ROW)

szStore = [NSString stringWithUTF8String:(char*)sqlite3_column_text(sql, 0)];

szReg = [NSString stringWithUTF8String:(char*)sqlite3_column_text(sql, 1)];

sqlite3_finalize(sql);

It's got something to do with "sql", but I'm unsure of what. Any suggestions?

Upvotes: 0

Views: 2154

Answers (3)

Chomu
Chomu

Reputation: 224

You can copy your database, click finder and write this address(/Users/administrator/Library/Application Support/iPhone Simulator/6.1/Applications/) in finder click ok. You will get documentary path. Open your project document file and paste your database....

Upvotes: 0

tiguero
tiguero

Reputation: 11537

Your code seems ok - did you copy the db to the ressource folder of your project?

EDIT

Make sure you access your db file with something like that:

    - (void) initializeDB {

        // Get the database from the application bundle
        NSString* path = [[NSBundle mainBundle] pathForResource:@"tblStore" ofType:@"sqlite"];

        if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) 
        {
            NSLog(@"Opening Database"); 
        }
        else
        {
            // Call close to properly clean up
            sqlite3_close(database);

            NSAssert1(0, @"Error: failed to open database: '%s'.", 
                      sqlite3_errmsg(database));
        }

}

Upvotes: 3

pgb
pgb

Reputation: 25001

The database file you add to the project will be embedded in the main NSBundle (see [NSBundle mainBundle]).

In order to do what you want, you need to copy the database from the main bundle to the documents folder before trying to access it. Otherwise, as you are experiencing, you will not be able to find the SQLite DB on the document's folder.

Upvotes: 1

Related Questions