我正在尝试读取/写入 SQLite 数据库,但我不断收到错误。我已验证数据库存在于 ...iPhone Simulator/Applications/5.1/app#/Documents/kipSQLDB.db
我已经从终端完成了 INSERT 和 SELECT 。
但是从应用程序中它只是出错了。(附加问题 - sqlite 有没有办法在 mysql 中获取信息性错误消息,如 mysql_error?当我在应用程序中出错时,我只得到一些符号。
这是我的代码:
。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