gpt4 book ai didi

iphone - SQLite "SQLITE MISUSE"错误

转载 作者:行者123 更新时间:2023-12-03 19:55:14 25 4
gpt4 key购买 nike

我在使用 SQLite 时遇到问题。当我尝试插入条目时出现错误。我发现错误是“SQLITE MISUSE”,错误代码为 21,使用

NSLog(@"ERROR:  Failed to add food!  (code: %d)",sqlite3_step(statement));

我的代码中的 insertSQL 字符串在需要时正确创建。另外,我看到使用 iFunBox 创建的表。

这是我的插入方法:

-(void)saveDataWithCategoryNumber:(int)categoryNumber foodNumber:(int)foodNumber foodName:(NSString *)foodName definiton:(NSString *)definiton ingredients:(NSString *)ingredients calorie:(int)calorie price:(int)price image1:(NSString *)image1 image2:(NSString *)image2 image3:(NSString *)image3 image4:(NSString *)image4 {
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &foodDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO foodDB (categoryNumber, foodNumber, foodName, definiton, ingredients, calorie, price, image1, image2, image3, image4) VALUES (%i, %i, \"%@\", \"%@\", \"%@\", %i, %i, \"%@\", \"%@\", \"%@\", \"%@\")", categoryNumber, foodNumber, foodName, definiton, ingredients, calorie, price, image1, image2, image3, image4];

const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(foodDB, insert_stmt, -1, &statement, NULL);
//char *error;
//sqlite3_exec(foodDB, insert_stmt, NULL, NULL, &error);

NSLog(@"insertSQL: %@",insertSQL);

if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"Food added.");
} else {
NSLog(@"ERROR: Failed to add food! (code: %d)",sqlite3_step(statement));

}

sqlite3_finalize(statement);
sqlite3_close(foodDB);
}}
<小时/>

也许创建方法会有用:

-(void)createDatabase{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

docsDir = [dirPaths objectAtIndex:0];

// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"foodDB.db"]];

NSFileManager *filemgr = [NSFileManager defaultManager];

if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &foodDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS foodDB (ID INTEGER PRIMARY KEY AUTOINCREMENT, categoryNumber INT, foodNumber INT, foodName TEXT, definition TEXT, ingredients TEXT, calorie INT, price INT, image1 TEXT, image2 TEXT, image3 TEXT, image4 TEXT)";

if (sqlite3_exec(foodDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(@"ERROR: Failed to create database!");
}

sqlite3_close(foodDB);

} else {
NSLog(@"ERROR: Failed to open/create database!");
}
}

}

最佳答案

您的日志记录逻辑正在执行两个 sqlite_step() 操作,这充其量只是一种误导。

相反,捕获第一个 sqlite_step() 调用的返回代码并报告值:

int rc = sqlite3_step(statement);
if (rc == SQLITE_OK)
{
NSLog(@"Food added.");
} else {
NSLog(@"ERROR: Failed to add food!: %d", rc);
}

您需要将此逻辑扩展到代码中的所有 sqlite_xxx() 调用。

关于iphone - SQLite "SQLITE MISUSE"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11312584/

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