PruitIgoe
PruitIgoe

Reputation: 6384

iOS SQLite insert and select not working?

I am trying to read/write to a SQLite db but I keep getting errors. I've verified that the db exists in ...iPhone Simulator/Applications/5.1/app#/Documents/kipSQLDB.db

I've done INSERT and SELECT to it from Terminal.

But from the app it just errors out. (Additional question - is there a way with sqlite to get informative error messages like mysql_error in mysql? When I error in the app I get nothing but some symbols.

Here's my code:

.h

#import <Foundation/Foundation.h>
#import <sqlite3.h>

@interface SQLiteController : NSObject { 

//file management
NSFileManager *fileManager;
NSString *documentsDirectory;

//sqlite data
sqlite3* databaseHandle;

}

- (void) initSQLiteDB; 

- (void) insertData : (NSString* ) fName : (NSString* ) lName : (NSString* ) companyName; 

- (NSArray* ) getData;

@end

.m

- (void) insertData : (NSString* ) fName : (NSString* ) lName : (NSString* ) companyName { 

NSString* insertStatement = [NSString stringWithFormat:@"INSERT INTO nameList (userFName, userLName, userCompany) VALUES (\"%@\", \"%@\", \"%@\")", fName, lName, companyName];
NSLog(@"%@", insertStatement);

char *error;
if ( sqlite3_exec(databaseHandle, [insertStatement UTF8String], NULL, NULL, &error) == SQLITE_OK) {

    int recordID = sqlite3_last_insert_rowid(databaseHandle);
    NSLog(@"A record was inserted into the database %@ with the id of %i", databaseHandle, recordID);
} else { 
    NSLog(@"Error: %s", error);
}
}

- (NSArray* ) getData {

NSMutableArray* dataArray = [[NSMutableArray alloc] init];

NSString* getStatement = @"SELECT * FROM nameList";

sqlite3_stmt *statement;
if (sqlite3_prepare_v2(databaseHandle, [getStatement UTF8String], -1, &statement, NULL) == SQLITE_OK){

    // Iterate over all returned rows
    while (sqlite3_step(statement) == SQLITE_ROW) {

        int recordID = sqlite3_column_int(statement, 0);
        NSString* fNameFromDB = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)];
        NSLog(@"%@", fNameFromDB);
    }
} else { 
    NSLog(@"No soup for you!");
}

return 0;

}
@end

Upvotes: 1

Views: 1036

Answers (3)

Aaron Bratcher
Aaron Bratcher

Reputation: 6451

SQLite can be a little tricky. All access to it must be from the same thread. For every prepare statement there must be a finalize statement.

There is an example project for using SQLite here you can refer to: https://github.com/AaronBratcher/ABSQLite

It has classes for accessing SQLite in a more traditional database way that I feel makes it easier.

Upvotes: -1

Ratnakar
Ratnakar

Reputation: 443

Try this,i think it will help you some what

 - (void) insertData : (NSString* ) fName : (NSString* ) lName : (NSString* ) companyName { 

    if(insertStatement == nil)
    {

    NSString* insertStatement = [NSString stringWithFormat:@"INSERT INTO nameList (userFName, userLName, userCompany) VALUES (\"%@\", \"%@\", \"%@\")", fName, lName, companyName];
    NSLog(@"%@", insertStatement);
    if(sqlite3_prepare_v2(database, insertSql, -1, &insertStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating insert statement. '%s'", sqlite3_errmsg(database));
    }


    sqlite3_bind_text(insertStatement, 1, [Gunameq UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(insertStatement, 2, [Gpassq UTF8String], -1, SQLITE_TRANSIENT);
    if(SQLITE_DONE != sqlite3_step(insertStmt))
        NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
    else
        NSLog("Inserted");
    //Reset the add statement.
    sqlite3_reset(insertStatement);
    insertStatement= nil;      

    - (NSArray* ) getData {

    NSMutableArray* dataArray = [[NSMutableArray alloc] init];

    NSString* getStatement = @"SELECT * FROM nameList";

    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2(databaseHandle, [getStatement UTF8String], -1, &statement, NULL) == SQLITE_OK){

        // Iterate over all returned rows
        while (sqlite3_step(statement) == SQLITE_ROW) {

            int recordID = sqlite3_column_int(statement, 0);
            NSString* fNameFromDB = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)];
            NSLog(@"%@", fNameFromDB);
        }
    } else { 
        NSLog(@"No soup for you!");
    }

    return 0;

    }
    @end

Upvotes: 1

Rish
Rish

Reputation: 457

Use Try catch format and catch the error type of sql

Upvotes: 1

Related Questions