我知道当我使用'sqlite3_open'时会打开一个新数据库而不是我的sqlite文件。 所以我用'sqlite3_open_v2'修复了它。 但是现在,我仍然无法从我的文件中获取数据。谁能告诉我如何结果呢? 非常感谢!
(void)viewDidLoad
{
[super viewDidLoad];
databaseName=@"freeway.sqlite";
[databasePath stringByExpandingTildeInPath];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:@"freeway.sqlite"];
[self checkAndCreateDatabase];
[self readDataFromDatabase];
}
-
(void)checkAndCreateDatabase
{
BOOL success;
NSFileManager *fileManager=[NSFileManager defaultManager];
success=[fileManager fileExistsAtPath:databasePath];
if (success)
{ NSLog(@"success");
return;
}
else {
NSLog(@"fail");
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
}
-
(void)readDataFromDatabase
sqlite3 *database;
fee = [[NSMutableArray alloc]init];
//if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
if (sqlite3_open_v2([databasePath UTF8String], &database, SQLITE_OPEN_READWRITE, NULL) == SQLITE_OK)
sqlite3_stmt *stmt;
char *sql = "SELECT * FROM fee";
if (sqlite3_prepare_v2(database,sql,-1,&stmt,NULL) == SQLITE_OK){
while (sqlite3_step(stmt) == SQLITE_ROW) {
NSInteger pID = sqlite3_column_int(stmt, 0);
NSString *pName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 1)];
NSString *pName2 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 2)];
Fee_Station *temp = [[Fee_Station alloc] initwithFCID:pID F_Name:pName Fmaileage:pName2];
[fee addObject:temp];
}
}
else {
NSLog(@"%s Prepare failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(database), sqlite3_errcode(database));
}
sqlite3_finalize(stmt);
}
sqlite3_close(database);
}
答案 0 :(得分:0)
你的checkAndCreateDatabase数据库函数调用Application didFinishLaunchingWithOptions方法,
之后
-(void) checkAndCreateDatabase
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if(success) return;
else
printf("NO File found");
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
//[fileManager release];
}
-(void)readDataFromDatabase
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"freeway.sqlite"];
sqlite3_stmt *compiledStatement;
sqlite3 *database;
if(sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement;
sqlStatement = "SELECT * FROM fee";
sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL);
printf("\nError===%s",sqlite3_errmsg(database));
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW )
{
NSString *str_field1=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
NSString *str_field2=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *str_field3=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSString *str_field4=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
// add str_field into array
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}