gpt4 book ai didi

objective-c - Sqlite在 objective-c 中插入错误

转载 作者:行者123 更新时间:2023-11-29 11:14:43 26 4
gpt4 key购买 nike

我正在使用 sqlite,我尝试将一些数据插入 sqlite 数据库。它不起作用。我有一个名为 LabUpdate 的表,但我有一些这样的错误:

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 =@"deneme";
int str8 =1;
int str9 =1;
NSString *str10=@"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;
}

问题是什么?我该如何解决这个问题?感谢您的回复。

最佳答案

当您尝试运行 SQL 语句时,所有非数字的内容都需要引用,例如

INSERT INTO foo (column1, column2) VALUES ('bar' , 'baz');
// ^^^^^ ^^^^^ string values quoted.

但是,我强烈建议使用 sqlite3_bind而不是直接将值格式化到您的 SQL 语句中。

关于objective-c - Sqlite在 objective-c 中插入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9971535/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com