我花了几天时间试图修复这段代码,但我放弃了!!如果他按下“保存”按钮,我正在做的是从用户那里获取输入并将其插入数据库。当我运行此代码并按下“保存”按钮后。1-我无法退出视图(到另一个选项卡)。2-插入未应用!
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"CAMAClientDB2.sqlite"];
sqlite3 *database;
//Copy the database from the package to the usrrs filesystem
if (sqlite3_open([dbPath UTF8String], &database)== SQLITE_OK) {//start if
NSLog(@"dataBaseOpen");
const char *sqlStatement = "insert into Location (Latitude,Longitude,LocationName,Tag) VALUES (?,?,?,?)";
//
sqlite3_stmt *compileStatement;
if (sqlite3_prepare_v2(database, sqlStatement, -1, &compileStatement, NULL) == SQLITE_OK) { // start if 2
if(SQLITE_DONE!=sqlite3_step(compileStatement))
{
sqlite3_bind_double(compileStatement, 1, [latitudeTextField.text doubleValue]);
sqlite3_bind_double(compileStatement, 2, [longitudeTextField.text doubleValue]);
sqlite3_bind_text( compileStatement, 3, [lName.text UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compileStatement, 4, [myText.text UTF8String] ,-1, SQLITE_TRANSIENT);
NSLog( @" successfully done" );
if(sqlite3_step(compileStatement) != SQLITE_DONE ) {
NSLog( @" not done" );
}
}
}
}
我在文件管理器中放入了 appDelegate 代码来应对:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"CAMAClientDB2.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if (success) {
NSLog(@"we have the database");
} else {
NSLog(@"we have no database");
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"CAMAClientDB2.sqlite"];
BOOL moved = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:nil];
if (moved) {
NSLog(@"database copied");
}
}
我错过了什么 !!问题是什么 !!