tags:

views:

177

answers:

1

Hello everyone

I have a project in which I stored sqlite database file "data.sqlite3" to 'Group'&files'-'resource'

Below are my viewcontroller source codes

//-myviewcontroller.h
#import "sqlite3.h"
#define kFilename @"data.sqlite3"

//myviewcontroller.m

-(NSString *)dataFilePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(
        NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:kFilename];
}

-(void)f
{
    if (sqlite3_open([[self dataFilePath] UTF8String],&database)!=SQLITE_OK)
    //dataFilePath returns
    ///Users/interdev/Library/Application Support/iPhone Simulator/User/Applications/095C6E05-4EAE-4817-883E-A72E39D439E0/Documents/data.sqlite3 
    {
        sqlite3_close(database);
        NSAssert(0,@"Failed to open database");//no problem
    }    

    NSString *query = @"SELECT * FROM table1 ORDER BY ROW";//table1 is table name
    sqlite3_stmt *statement;
    NSInteger v=sqlite3_prepare_v2( database, [query UTF8String],
                                   -1, &statement, nil);
    NSString *zs= [NSString stringWithFormat:@"%d",v];
    NSLog(@" The buttontitile is %@ ",zs);
    if ( v == SQLITE_OK) { // ...
}

I checked value of v in log, it always is 1

#define SQLITE_ERROR        1   /* SQL error or missing database */

I do not know why this happened.

A: 

It looks like your code is looking for your database in the 'Documents' folder of your application.

NSArray *paths = NSSearchPathForDirectoriesInDomains(
    NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

Thus, if the data.sqlite3 file is in your Resources folder, the applicaiton is not going to find the database. It's probably best to create and store the sqlitedatabase in the 'Documents' folder of the applicaiton. E.g /Users/interdev/Library/Application Support/iPhone Simulator/User/Applications/095C6E05-4EAE-4817-883E-A72E39D439E0/Documents/data.sqlite3

Lenny
I changed -(NSString *)dataFilePath{ NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; return [documentsDirectory stringByAppendingPathComponent:kFilename];}to-(NSString *)dataFilePath{ return @"data.sqlite3";}same errorit look likes the problem is atNSString *query = @"SELECT * FROM table1 ORDER BY ROW";