Ios 通过UITableView保存到SQLITE3时的奇怪行为
我很难理解为什么sqlite3 insert命令只保存某些index.rows 当前,当用户选择表上的特定行时,以下命令开始Ios 通过UITableView保存到SQLITE3时的奇怪行为,ios,objective-c,uitableview,sqlite,Ios,Objective C,Uitableview,Sqlite,我很难理解为什么sqlite3 insert命令只保存某些index.rows 当前,当用户选择表上的特定行时,以下命令开始 NSMutableString * videoString = [self.filteredVideoArray objectAtIndex:indexPath.row]; NSMutableString * imageString = [self.filteredImageArray objectAtIndex:indexPath.row];
NSMutableString * videoString = [self.filteredVideoArray objectAtIndex:indexPath.row];
NSMutableString * imageString = [self.filteredImageArray objectAtIndex:indexPath.row];
NSMutableString * titleString = [self.filteredTitleArray objectAtIndex:indexPath.row];
NSString * descriptionString = [self.filteredDescriptionArray objectAtIndex:indexPath.row];
NSString *sql = [NSString stringWithFormat:@"INSERT INTO Recent ('name', 'title', 'video', 'image', 'detail' ) VALUES ('%s','%s','%s','%s','%s')", [self.nameString UTF8String],[titleString UTF8String],[videoString UTF8String],[imageString UTF8String] ,[descriptionString UTF8String],NULL];
char *err;
if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) !=SQLITE_OK) {
sqlite3_close(db);
// NSAssert(0, @"could not update table");
}
else{
NSLog(@"table updated");
}
当我登录时
NSLog(@"video = %@",videoString);
NSLog(@"image = %@",imageString);
NSLog(@"detail = %@",descriptionString);
NSLog(@"title = %@",titleString);
NSLog(@"name = %@",self.nameString);
以上所有操作都返回正确的值
我不确定您需要什么其他信息来解决此问题
谢谢
Thomas在您的
插入
字符串中,使用“%@”
的“%s”
实例添加值
NSString *sql = [NSString stringWithFormat:@"INSERT INTO Recent ('name', 'title', 'video', 'image', 'detail' ) VALUES ('%@','%@','%@','%@','%@')", [self.nameString UTF8String],[titleString UTF8String],[videoString UTF8String],[imageString UTF8String] ,[descriptionString UTF8String]];
并从
Insert
字符串的末尾删除NULL
。通常建议不要使用stringWithFormat
构建SQL语句,而是使用?
占位符。如果其中一个值中有撇号,这将保护您。它还可以防止SQL注入攻击。所以你应该
NSString *sql = @"INSERT INTO Recent ('name', 'title', 'video', 'image', 'detail' ) VALUES (?,?,?,?,?)";
sqlite3_stmt *statement;
if (sqlite3_prepare(db, [sql UTF8String], -1, &statement, NULL) != SQLITE_OK)
{
NSLog(@"%s: prepare error: %s", __FUNCTION__, sqlite3_errmsg(database));
return;
}
if (sqlite3_bind_text(statement, 1, [self.nameString UTF8String], -1, NULL) != SQLITE_OK)
{
NSLog(@"%s: bind 1 error: %s", __FUNCTION__, sqlite3_errmsg(database));
}
if (sqlite3_bind_text(statement, 2, [titleString UTF8String], -1, NULL) != SQLITE_OK)
{
NSLog(@"%s: bind 2 error: %s", __FUNCTION__, sqlite3_errmsg(database));
}
// repeat for the other parameters
if (sqlite3_step(statement) != SQLITE_DONE)
{
NSLog(@"%s: step error: %s", __FUNCTION__, sqlite3_errmsg(database));
}
sqlite3_finalize(statement);
注意,除了使用
sqlite3\u bind\u text
函数外,每当我没有收到成功的返回代码时,我都会记录sqlite3\u errmsg
,这样它会准确地告诉我出了什么问题。如果你不看这些错误消息,你就瞎了。谢谢你的评论。我按照你说的做了,但也删除了utf8String,让它工作,但它没有产生任何影响difference@Tom-在NSString中,不考虑“%s”,它接受“%@”,也检查我的Eddid ANSRWRY,但是使用<代码> %@ <代码> > <代码> > [Self.NeMeStudioUTF8St]错误。请严格按照您的说明操作,但无法解决问题issue@MartinR-很抱歉,但我总是按照我所说的那样工作,这是我的答案,也是我的工作:(<代码> SqLeTe3EXECU< <代码>返回>代码> SqLeLeEXOK < /COD> >不是吗?是的,一小部分行将添加数据,但不是全部,顺便说一下,这可能是正确的方式来执行<代码> SqLeTe3xxxx < /代码>调用,绑定值,如果您发现这是太多代码,您可以考虑使用,这是一个瘦Objy-C包装器。SQLite C库,它极大地简化了编写可靠SQLite代码的过程。感谢Rob的信息。现在它给了我以下错误准备错误:内存不足,这可能是它发生的原因。关于我在这种情况下需要做什么的任何想法,我都没有使用上述方法before@Tom-臭名昭著的“内存不足”消息表示您传递的数据库参数尚未打开。(当我第一次发布我的答案时,我将数据库设置保留为nil
;如果您抓住该行,请将其删除;请参阅我的修订答案。)在调用此代码之前,请确保您的数据库已打开,并将其传递给正确的db
指针。您是一个绝对的天才。非常感谢您抽出时间来解决我的问题:)还要感谢iPatel和martin