我一直在努力在我的iPhone应用程序中加入一个sqlite数据库。数据库已创建,我已成功将元素插入数据库。现在,当我尝试从db中进行选择时,我的prepare语句不符合SQLITE_OK的要求。这是我创建数据库的代码:
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)";
这是我用来提取数据的代码:
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] initWithUTF8String:(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");
}
第二个if语句因某种原因而失败?
有什么想法吗?
答案 0 :(得分:1)
printf("the prepare statement is not ok\n");
根本没有用处。尝试像
NSLog(@"prepare failed (%s)", sqlite3_errmsg(expenseDB));