0

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.

3
  • @user262325: I reformatted your question for better readability. Commented Feb 18, 2010 at 11:58
  • What's the point of [query UTF8String]? Use a C string! Commented Feb 18, 2010 at 12:03
  • [query UTF8String] returns a C string Commented Feb 18, 2010 at 12:14

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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 error it look likes the problem is at NSString *query = @"SELECT * FROM table1 ORDER BY ROW";

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.