【发布时间】:2021-11-21 01:30:06
【问题描述】:
我在 Objective-c 中有一个项目,我试图找到一种方法将属性文本从 UITextView 保存到 SQLite3 表。 我的项目目标操作系统是 12.1。 我正在使用一个名为“MMItem”的对象和一个名为“notesAttributed”的 NSData 属性。 在我的 viewController 类中,我使用 NSKeyedArchiver 将 AttributedText 编码为 NSdata 格式,然后复制到对象属性。
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.itemNotes.attributedText requiringSecureCoding:YES error:Nil];
self.item.notesAttributed = data;
然后我在我的模型中调用一个方法来保存对象
NSString *resultStr = [self.meetingModel saveAttributedItemNote:item];
在模型中,我尝试将属性字符串保存到项目表设置中的字段中,类型为“blob”
- (NSString *)saveAttributedItemNote:(MMItem *)item{
NSString *errorMessage;
NSString *sql;
NSInteger result = 0;
if (item) {
//create blob encoded data for Attributed notes
NSInteger itemID = item.itemID;
sql = [NSString stringWithFormat:
@"UPDATE Item set noteAttributed = %@ WHERE itemID = %ld",item.notesAttributed, (long)itemID];
char *err;
// open DB and save
if ([self openDB]){
//NSLog(@"%@", NSStringFromSelector(_cmd));
result = sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err);
sqlite3_close(db);
}
if (result != SQLITE_OK)
{
errorMessage = @"Error";
}
else
{
//NSLog(@"Table updated");
errorMessage = @"OK";
[self saveAttributedItemNote:item];
}
}
item = nil;
return errorMessage;
}
SQL Execute 语句失败并出现错误 1。
我怀疑我打算使用“绑定”而不是 Execute 语句将 blob 插入表中,但我只是找到了如何使用 Objective-c 将所有这些放在一起。 任何帮助都会非常感谢。
【问题讨论】:
-
一个叫 'MMItem' 的人是从哪里来的?
-
SQLite和
NSKeyedArchiver有什么业务? -
开放数据库?它来自哪里?
-
MMItem 是一个 NSobject,它包含一个 'Item' 的所有属性,例如 itemID、topicID、itemDescription 等
-
openDB 是我的模型中打开 SQLite DB 的一种方法
标签: objective-c sqlite save blob nsattributedstring