我不能在目标C插入数据到SQLite数据库
可能重复:
Assertion failure error in objective c我不能在目标C插入数据到SQLite数据库
我想尝试到SQLite数据库表中插入一些数据,但我得到了一个错误像这样:
***Assertion failure in -[ViewController buttonClick:],/Users/ds/Desktop/SqliteDeneme/SqliteDeneme/ViewController.m:57
我代码在这里:
- (IBAction)buttonClick:(id)sender {
NSString *str1 [email protected]"1";
NSString *str2 [email protected]"1";
NSString *str3 [email protected]"0.1";
NSString *str4 [email protected]"0.1";
NSString *str5 [email protected]"0.1";
NSString *str6 [email protected]"0.1";
NSString *str7 [email protected]"deneme";
NSString *str8 [email protected]"1";
NSString *str9 [email protected]"1";
NSString *[email protected]"deneme";
NSArray *pathsArray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];
destinationPath=[doumentDirectoryPath stringByAppendingPathComponent:@"SqliteTestDb.sqlite"];
NSLog(@"database path %@",destinationPath);
if (sqlite3_open([destinationPath UTF8String], &cruddb)==SQLITE_OK)
{
NSLog(@"dataBaseOpen");
// leak happens here, do stuff then call sqlite3_close(database), or move it out of the if/else block.
if(stmt == nil) {
const char *sql = "INSERT INTO LabUpdate (IsSuccess, ProducerId, Latitude, Longitude, Altitude, Slope, SampleDate, PackageNo, Status, Description) VALUES (?,?,?,?,?,?,?,?,?,?)";
if(sqlite3_prepare_v2(cruddb, sql, -1, &stmt, NULL) == SQLITE_OK){
//sqlite3_prepare_v2(cruddb, sql, 1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, [str1 integerValue]);
sqlite3_bind_int(stmt, 2, [str2 integerValue]);
sqlite3_bind_double(stmt, 3, [str3 floatValue]);
sqlite3_bind_double(stmt, 4, [str4 floatValue]);
sqlite3_bind_double(stmt, 5, [str5 floatValue]);
sqlite3_bind_double(stmt, 6, [str6 floatValue]);
sqlite3_bind_text(stmt, 7, [str7 UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 8, [str8 integerValue]);
sqlite3_bind_int(stmt, 9, [str9 integerValue]);
sqlite3_bind_text(stmt, 10, [str10 UTF8String], -1, SQLITE_TRANSIENT);
}
else
NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(cruddb));
}
if(SQLITE_DONE != sqlite3_step(stmt))
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(cruddb));
else
//SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
recordID = sqlite3_last_insert_rowid(cruddb);
//Reset the add statement.
sqlite3_reset(stmt);
}
else {
sqlite3_close(cruddb);
NSLog(@"dataBaseNotOpen");
NSAssert1(0, @"Error while opening database '%s'", sqlite3_errmsg(cruddb));
}
}
我该如何解决这个问题?我把一个断点,我看到了这条线后,没有进入:
if(sqlite3_prepare_v2(cruddb, sql, -1, &stmt, NULL) == SQLITE_OK){
这是我的数据库表和colums:
可能是它的工作原理
if(sqlite3_open([destinationPath UTF8String], &cruddb) ==SQLITE_OK) {
sqlite3_prepare_v2(cruddb, "BEGIN TRANSACTION", -1, &compiledStmt, NULL);
sqlite3_step(compiledStmt);
sqlite3_finalize(compiledStmt);
const char *sql = "INSERT INTO LabUpdate (IsSuccess, ProducerId, Latitude, Longitude, Altitude, Slope, SampleDate, PackageNo, Status, Description) VALUES (?,?,?,?,?,?,?,?,?,?)";
if(sqlite3_prepare_v2(cruddb, sql, -1, &stmt, NULL) == SQLITE_OK){
sqlite3_bind_int(stmt, 1, [str1 integerValue]);
sqlite3_bind_int(stmt, 2, [str2 integerValue]);
sqlite3_bind_double(stmt, 3, [str3 floatValue]);
sqlite3_bind_double(stmt, 4, [str4 floatValue]);
sqlite3_bind_double(stmt, 5, [str5 floatValue]);
sqlite3_bind_double(stmt, 6, [str6 floatValue]);
sqlite3_bind_text(stmt, 7, [str7 UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 8, [str8 integerValue]);
sqlite3_bind_int(stmt, 9, [str9 integerValue]);
sqlite3_bind_text(stmt, 10, [str10 UTF8String], -1, SQLITE_TRANSIENT);
NSUInteger err = sqlite3_step(compiledStmt);
if (err != SQLITE_DONE){
NSLog(@"error while binding %d %s",err, sqlite3_errmsg(database));
}
sqlite3_reset(compiledStmt);
sqlite3_finalize(compiledStmt);
} else {
NSLog(@"Invalid Query");
}
sqlite3_prepare_v2(cruddb, "END TRANSACTION", -1, &compiledStmt, NULL);
sqlite3_step(compiledStmt);
sqlite3_finalize(compiledStmt);
sqlite3_close(cruddb);
我得到thie消息:2012-03-30 13:57:22688 SqliteDeneme [1167:207]无效的查询。我该如何解决查询问题? – 2012-03-30 10:58:04
我错误地设置了其他查询现在检查答案 – Hiren 2012-03-30 11:05:15
它给仍然无效的查询错误:S – 2012-03-30 11:07:55
我可以建议你使用绑定列的不同技术:
NSString* SQL = [NSString stringWithFormat:@"INSERT INTO table1(col1,col2,col3) VALUES(%i,'%@',%i)",number,Surname,age];
其中数字和年龄是int的,姓是NSSt ring *(注意格式中字符串的引号)。
你可以像这样的代码执行以下命令:
sqlite3_stmt *queryHandle = [self prepare:SQL];
if (sqlite3_step(queryHandle) != SQLITE_DONE)
{
NSLog(@"ExecuteNonQuery has error");
NSLog(@"Failed from sqlite3_step. Error is: %s", sqlite3_errmsg(database));
}
else
{
int rowsaffected = sqlite3_changes(database);
}
一般准备功能应该是这样的:
-(sqlite3_stmt*)prepare:(NSString*)query
{
sqlite3_stmt *queryHandle;
const char *sqlStatement = (const char *) [query UTF8String];
if(sqlite3_prepare_v2(database, sqlStatement, -1, &queryHandle, NULL) != SQLITE_OK)
{
int error = sqlite3_prepare_v2(database, sqlStatement, -1, &queryHandle, NULL);
NSLog(@"Failed from sqlite3_prepare_v2. Error is: %s", sqlite3_errmsg(database));
NSLog(@"Compiled Statement has error code:%i:%@",error,query);
}
return queryHandle;
}
这是我使用的方式,是快速,可以推广到子功能,如准备。
希望这会有所帮助。
但代码是不同的! – 2012-03-30 10:04:59
所以你应该编辑你的原始问题,而不是发布一个新的问题。 – borrrden 2012-03-30 10:09:48
sqlite3_prepare_v2不等于SQLITE_OK!请帮助我如何解决? – 2012-03-30 10:13:42