我是 iphone 编程新手。我使用的是 Mac 10.7.3,因为我无法将数据存储在数据库中在这段代码中
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
databasePath = [docsDir stringByAppendingPathComponent: @"test.db"];
NSLog(@"%@",databasePath);
NSFileManager *fn=[NSFileManager defaultManager];
NSError *error;
BOOL success=[fn fileExistsAtPath:databasePath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.db"];
success = [fn copyItemAtPath:defaultDBPath toPath:databasePath error:&error];
}
//NSLog(@"sdasdassd%@",databasePath);
NSLog(@"hai");
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSLog(@"hai2");
NSString *insertSQL = [NSString stringWithFormat: @"insert into path(imagepath,audiopath) values (\"%@\",\"%@\")",name.text,email.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"details added");
} else {
NSLog(@"details not added");
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
}
谢谢阿斯拉姆