gpt4 book ai didi

ios - 在 iOS 中使用 SQLite3 插入语句时出错

转载 作者:行者123 更新时间:2023-11-29 03:59:55 24 4
gpt4 key购买 nike

我正在尝试在 iOS 应用程序的 SQLite 3 数据库中插入一些数据。但我不断收到对我来说毫无用处的错误。

这是我尝试插入的代码:

 @try {
NSString *dbPath = [self.GetDocumentDirectory stringByAppendingPathComponent:@"knhb.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if((sqlite3_open_v2([dbPath UTF8String], &db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK))
{
NSLog(@"An error has occured.");
}


NSString *sqlString = [NSString stringWithFormat:@"INSERT INTO Profile (name, password) VALUES ('%@','%@')", name, password];

const char *sql = [sqlString UTF8String];

sqlite3_stmt *sqlStatement;

if(sqlite3_prepare_v2(db, sql, 1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"%s SQLITE_ERROR '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(db), sqlite3_errcode(db));
}

sqlite3_step(sqlStatement);
sqlite3_finalize(sqlStatement);

int lastInsert = sqlite3_last_insert_rowid(db);
NSLog(@"lastinsertid:%d", lastInsert);

sqlite3_close(db);
}
@catch (NSException *exception) {
NSLog(@"An exception occured: %@", [exception reason]);
}

我的数据库表 Profile 有三列 idProfile、名称和密码。但由于 idProfile 是一个 int 和主键,我不必包含它,因为它是自动递增的。

我不断收到的错误是:SQLITE_ERROR 'near“I”:语法错误' (1)

我正在写入“文档”文件夹。

非常感谢任何帮助。

大安

最佳答案

我不太清楚为什么,但我通过更改以下代码行修复了它:

if(sqlite3_prepare_v2(db, sql, 1, &sqlStatement, NULL) != SQLITE_OK)

我把它变成了:

if(sqlite3_prepare_v2(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)

不确定 1 或 -1 是什么意思。也许有人可以详细说明一下?

干杯!

关于ios - 在 iOS 中使用 SQLite3 插入语句时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16001988/

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