Database 在数据库中插入时发生SQlite错误
我花了几天的时间试图修复这个代码,我放弃了!! 我所做的是从用户那个里获取输入,并在他按下“保存”按钮时将其插入数据库。 当我运行此代码并按下“保存”按钮后。 1-我无法从视图中退出(转到其他选项卡)。 2-插入未应用Database 在数据库中插入时发生SQlite错误,database,xcode,xcode4,sqlite,Database,Xcode,Xcode4,Sqlite,我花了几天的时间试图修复这个代码,我放弃了!! 我所做的是从用户那个里获取输入,并在他按下“保存”按钮时将其插入数据库。 当我运行此代码并按下“保存”按钮后。 1-我无法从视图中退出(转到其他选项卡)。 2-插入未应用 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [paths ob
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*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
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");
}
}
我错过什么了!!有什么问题 对于您的#2问题:
1.您有一个额外的sqlite\u步骤调用。您的顺序应该是:
sqlite3_prepare_v2()
sqlite_bind_....
sqlite_bind_....
for as many bind's as you need, and then
sqlite_step()