I've been working on incorporating a sqlite db in my iPhone app. The database has been created, and I have successfully inserted elements into the database. Now, when I try to select from the db, my prepare statement is not meeting the requirement of SQLITE_OK. Here is the code where I create the database:
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS EXPENSES (id integer primary key autoincrement, unix_date integer, date_time text, purpose text, start_mile text, end_mile text, distance text, fees text, party_id integer)";
and here is the code I am using to extract the data:
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
printf("in getExpense\n");
//expenses = [[NSMutableArray alloc] init];
if (sqlite3_open(dbpath, &expenseDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM EXPENSES"];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(expenseDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
printf("right before the while loop\n");
while (sqlite3_step(statement) == SQLITE_ROW)
{
printf("in the while loop\n");
VehicleExpense *expense = [[VehicleExpense alloc] init];
NSInteger *newTimeStamp = sqlite3_column_int(statement, 1);
NSString *newDate = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
NSString *newPurpose = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];
NSString *newStart = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 4)];
NSString *newEnd = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 5)];
NSString *newDistance = [[NSString alloc] initWit开发者_如何学ChUTF8String:(const char *) sqlite3_column_text(statement, 6)];
NSString *newFees = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 7)];
NSInteger *newPartyId = sqlite3_column_int(statement, 8);
expense.unix_date = newTimeStamp;
expense.date = newDate;
expense.purpose = newPurpose;
expense.start_mile = newStart;
expense.end_mile = newEnd;
expense.distance = newDistance;
expense.fees = newFees;
expense.party_id = newPartyId;
[expenses addObject: expense];
printf("expense add");
[newDate release];
[newPurpose release];
[newStart release];
[newEnd release];
[newDistance release];
[newFees release];
[expense release];
expense_count++;
}
sqlite3_finalize(statement);
}else{
printf("the prepare statement is not ok\n");
}
sqlite3_close(expenseDB);
}else{
printf("couldn't open the db\n");
}
The second if-statement keeps failing for some reason?
Any ideas?
printf("the prepare statement is not ok\n");
is not really helpful at all. Try something like
NSLog(@"prepare failed (%s)", sqlite3_errmsg(expenseDB));
精彩评论