gpt4 book ai didi

xcode - sqlite 解析查询时出错

转载 作者:行者123 更新时间:2023-12-03 17:13:35 26 4
gpt4 key购买 nike

我在使用 sqlite 查询数据库时收到一个奇怪的错误:SQLlite 错误:“SE”附近:语法错误我真的不明白为什么他不喜欢我发送给它的查询字符串。这是我的代码:

-(Wallet*)loadDataFromSQL{
sqlite3 *database;
NSLog(@"opening..");
if (sqlite3_open([[NSString stringWithFormat:@"mywallet.sqlite3"] UTF8String], &database) == SQLITE_OK) {
NSLog(@"opened..");
const char *query = [[NSString stringWithFormat:@"SELECT * FROM 'Transaction';"] UTF8String]; // "insert into \"Transaction\" values (\"2013-01-01\",\"tipo\",\"cat\",1)";
sqlite3_stmt *selectstmt;
NSLog(@"preparing stmnt..");
if(sqlite3_prepare_v2(database, query, SQLITE_OPEN_READWRITE, &selectstmt, nil) == SQLITE_OK) {
NSLog(@"Prepared..");
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSLog(@"row..");
NSString *data = [NSString stringWithUTF8String: (char*)sqlite3_column_text(selectstmt, 0)];
NSString *type = [NSString stringWithUTF8String: (char*)sqlite3_column_text(selectstmt, 1)];
NSString *category = [NSString stringWithUTF8String: (char*)sqlite3_column_text(selectstmt, 2)];
float amount = (float)sqlite3_column_double(selectstmt, 0);

Transaction *t = [[Transaction alloc]init:data transactionType:type transactionCategory:category transactionAmount:[NSString stringWithFormat:@"%f",amount]];
NSLog(@"%@",t);
}
}else{
NSLog([NSString stringWithFormat:@"SQLlite error: %s\n", sqlite3_errmsg(database)]);
}
}
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
return nil;
}

没什么太花哨的..有什么问题吗?谢谢!

最佳答案

sqlite3_prepare_v2的第三个参数必须是查询字符串的长度,或者只是 -1

sqlite3_prepare_v2(database, query, SQLITE_OPEN_READWRITE, &selectstmt, nil)

随着 SQLite 提示“SE”,我猜测 SQLITE_OPEN_READWRITE 的值是 2:-)

关于xcode - sqlite 解析查询时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13593222/

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