如何保存&通过FMDB将NSdata检索到sqlite [英] How to save & retrieve NSdata into sqlite through FMDB
问题描述
如何将 NSData
保存到sqlite中,我使用FMDB包装器来保存数据。
How can I save NSData
into sqlite, I am using FMDB wrapper for saving data.
下面是到目前为止我尝试过的代码
用于保存
Below is the code which I have tried so far
For saving
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model.expertArray];;
NSString *query = [NSString stringWithFormat:@"insert into save_article values ('%@','%@','%@','%@','%@','%@')",
model.Id, model.title, model.earliestKnownDate, data, model.excerpt,model.image_url];
用于Retriving
while([results next]) {
NSData *data = [results dataForColumn:@"experts"];
NSMutableArray *photoArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
我已尝试过两种数据类型blob&文字,但到目前为止没有运气,有人可以指导我怎么做吗?
I have tried both datatype blob & text but no luck so far, can anybody guide me how to do it?
推荐答案
以下是所有可能面临的人的片段使用FMDB将 NSData
插入Sqlite时出现同样的问题。
Below is the Snippet for all who may face the same issue while inserting NSData
to Sqlite using FMDB.
在我的案例中我想存储 NSArray
在 Sqlite
所以我首先将 NSArray
转换为 NSData
&然后将其存储在 Sqlite
。
In my Case I wanted to store NSArray
in Sqlite
So i first convert the NSArray
into NSData
& then store it in Sqlite
.
将NSArray转换为NSData
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:YourNSarray];;
将NSData保存到Sqlite
[database executeQuery:@"insert into save_article values (?,?,?,?,?,?)", model.Id, model.title, model.earliestKnownDate, data, model.excerpt,model.image_url];
注意: - 不要使用<$ c $构建查询c> stringWithFormat [下面是你不应该使用的代码]:感谢@rmaddy& @hotlicks指我这个:)
Note:- Don't build a query using stringWithFormat
[below is the code which you should not use]:. Thanks to @rmaddy & @hotlicks for pointing me to this :)
NSString *query = [NSString stringWithFormat:@"insert into user values ('%@', %d)",
@"brandontreb", 25];
[database executeUpdate:query];
现在是最后一步,即将 NSData检索回NSArray
and now the last step i.e retrieving NSData back to NSArray
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:[database dataForColumn:@"yourcololumname"]];
希望这能帮助有需要的人和朋友。初学者:)
Hope this will help the needy & beginner :)
这篇关于如何保存&通过FMDB将NSdata检索到sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!