我一直在努力合併一個sqlite數據庫在我的iPhone應用程序。數據庫已創建,並且我已成功將元素插入到數據庫中。現在,當我嘗試從db中選擇時,我的prepare語句不符合SQLITE_OK的要求。下面是代碼,我創建數據庫:sqlite聲明不行......爲什麼? - iphone
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語句一直沒有出於某種原因?
任何想法?
使用'的NSLog(@ 「錯誤:%s」,sqlite3_errmsg(expeseDB));'打印錯誤控制檯(在你的else語句)。這個錯誤通常說明問題是什麼。 –