1
我正在使用sqlite,我試圖插入一些數據到sqlite數據庫。它不工作。我有一個表,它的名字是LabUpdate但我有一些象這樣的錯誤:Sqlite插入錯誤的目標c
2012-04-02 08:49:58.158 SqliteDeneme[361:207] Failed from sqlite3_prepare_v2. Error is: no such column: deneme
2012-04-02 08:49:58.159 SqliteDeneme[361:207] Compiled Statement has error code:1:INSERT INTO LabUpdate (IsSuccess, ProducerId, Latitude, Longitude, Altitude, Slope, SampleDate, PackageNo, Status, Description) VALUES(1,1,0.100000,0.100000,0.100000,0.100000,deneme,1,1,deneme)
2012-04-02 08:49:58.160 SqliteDeneme[361:207] ExecuteNonQuery has error
2012-04-02 08:49:58.160 SqliteDeneme[361:207] Failed from sqlite3_step. Error is: library routine called out of sequence
我這樣的代碼:
- (IBAction)buttonClick:(id)sender {
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"SqliteTestDb.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &cruddb) == SQLITE_OK))
{
NSLog(@"An error has occured.");
}
if(sqlite3_open([dbPath UTF8String], &cruddb) ==SQLITE_OK){
int str1 =1;
int str2 =1;
float str3 =0.1;
float str4 =0.1;
float str5 =0.1;
float str6 =0.1;
NSString *str7 [email protected]"deneme";
int str8 =1;
int str9 =1;
NSString *[email protected]"deneme";
NSString* SQL = [NSString stringWithFormat:@"INSERT INTO LabUpdate (IsSuccess, ProducerId, Latitude, Longitude, Altitude, Slope, SampleDate, PackageNo, Status, Description) VALUES(%i,%i,%f,%f,%f,%f,%@,%i,%i,%@)",str1,str2,str3,str4,str5,str6,str7,str8,str9,str10];
stmt = [self prepare:SQL];
sqlite3_step(stmt);
sqlite3_finalize(stmt);
sqlite3_close(cruddb);
if (sqlite3_step(stmt) != SQLITE_DONE)
{
NSLog(@"ExecuteNonQuery has error");
NSLog(@"Failed from sqlite3_step. Error is: %s", sqlite3_errmsg(cruddb));
}
else
{
int rowsaffected = sqlite3_changes(cruddb);
NSLog(@" rowsaffected %i",rowsaffected);
}
}
}
-(sqlite3_stmt*)prepare:(NSString*)query
{
sqlite3_stmt *queryHandle;
const char *sqlStatement = (const char *) [query UTF8String];
if(sqlite3_prepare_v2(cruddb, sqlStatement, -1, &queryHandle, NULL) != SQLITE_OK)
{
int error = sqlite3_prepare_v2(cruddb, sqlStatement, -1, &queryHandle, NULL);
NSLog(@"Failed from sqlite3_prepare_v2. Error is: %s", sqlite3_errmsg(cruddb));
NSLog(@"Compiled Statement has error code:%i:%@",error,query);
}
return queryHandle;
}
是什麼問題?我該如何解決這個問題?感謝您的回覆。
這並不是說它真的可以解決您的問題,但我完全推薦使用像FMDB或PLDatabase這樣的Objective-C sqlite包裝器 - 它會讓您的生活變得更加輕鬆。 – 2012-04-02 06:32:16
你可以發佈NSLog(@「SQL:%@」,SQL)嗎?但你應該引用你的字符串!例如:NSString * SQL = [NSString stringWithFormat:@「INSERT INTO LabUpdate(IsSuccess,ProducerId,Latitude,Longitude,Altitude,Slope,SampleDate,PackageNo,Status,Description)VALUES(%i,%i,%f,%f,%女,%F, '%@',%I,%I, '%@')」,STR1,STR2,STR3,STR4,STR5,STR6,STR7,STR8,STR9,str10]; – CarlJ 2012-04-02 07:43:54
出於某種原因,您正在執行'sqlite3_prepare_v2()',如果失敗,您再次執行它**以獲取錯誤代碼。這是無稽之談;一步完成。 – trojanfoe 2012-04-02 09:18:48