简体   繁体   English

NSData UIImageJPEGRepresentation不写入SQLite fmdb

[英]NSData UIImageJPEGRepresentation not writing to SQLite fmdb

I am attempting to convert a jpeg to an nsdata object and then store that data in a blob column of an sqlite database using the fmdb wrapper. 我试图将jpeg转换为nsdata对象,然后使用fmdb包装器将该数据存储在sqlite数据库的blob列中。

eg: 例如:

UIImage *img = [UIImage imageNamed:@"house.jpeg" ];
NSData *data = UIImageJPEGRepresentation(img, 1.0);
NSString *query = [NSString stringWithFormat:@"insert into t1 values (null,?)", data ];

Where t1 is created something like: 创建t1的地方类似:

[database executeUpdate:@"create table t1(rowid integer primary key autoincrement, house blob)" ];

Ideally I would then be able to pull the data out and convert it back to a jpeg like so: 理想情况下,我将能够将数据提取出来,然后将其转换回jpeg,如下所示:

NSString *query = @"select * from t1";

FMResultSet *results = [database executeQuery:query];

while ([ results next ]) 
{

    NSData *picData = [ results dataForColumn:@"house" ];
    UIImage *housePhoto = [ UIImage imageWithData:picData ];
}

I have used this type of model for other data types like strings, etc.. with no problem. 我已经将这种类型的模型用于其他数据类型,例如字符串等。没有问题。 In my current attempt I am using this model with a table of string columns and 1 blob column. 在当前尝试中,我将此模型与字符串列和1个blob列的表一起使用。 When I write to the database, all of the strings are stored fine, but when I try to read back the blob column like above, I get null returned for the blob column. 当我写数据库时,所有的字符串都存储良好,但是当我尝试像上面那样读回blob列时,对于blob列返回空值。 Prior to writing to the database I double checked to see that the NSData I am writing with was not null and did contain a jpeg representation and it did. 在写入数据库之前,我仔细检查了一下正在写入的NSData不是null,并且确实包含jpeg表示形式,并且确实如此。 Does a known issue jump out at anyone or am I just doing something wrong externally from this? 某个已知问题会突然发生在任何人身上吗,还是我只是从外部做错了什么?

This line: 这行:

NSString *query = [NSString stringWithFormat:@"insert into t1 values (null,?)", data ];

Is always going to result in this string: 总是会导致以下字符串:

insert into t1 values (null,?)

As -stringWithFormat: doesn't understand that you are trying to bind data to the "?". 如-stringWithFormat:无法理解您正在尝试将数据绑定到“?”。

It looks like FMDatabase has an -executeQuery: method, which takes a variable number of arguments. 看起来FMDatabase具有-executeQuery:方法,该方法采用可变数量的参数。 Try this instead: 尝试以下方法:

[database executeQuery:@"insert into t1 values (null,?)", data];

If you manually open the database using sqlite3, is the original JPEG data there? 如果使用sqlite3手动打开数据库,原始JPEG数据在那里吗?

Warning: I'm accessing an ancient section in my brain, as I haven't done databases for a few years ;) 警告:由于我已经有好几年没有做数据库了,所以我正在访问大脑中的一个古老区域;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM