Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
557 views
in Technique[技术] by (71.8m points)

objective c - Retrieve nsdata from sqlite stored in Blob data type for iPhone App

In iPhone App how to store UIImage converted into NSData to sqlite table in BLOB datatype? Is there any kind of binding needed(NSData ->Blob)?

or

While retrieving image stored in NSData form in sqlite Blob datatype, should I need to perform any task to convert that stored database in blob datatype to get back into NSData form?

Please help and suggest.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I used something like this:

// prepare sqlite3 statement before this line
int res = sqlite3_step(stmt);
if (res == SQLITE_ROW) {
    const void *ptr = sqlite3_column_blob(stmt, 0);
    int size = sqlite3_column_bytes(stmt, 0);
    data = [[NSData alloc] initWithBytes:ptr length:size];
    sqlite3_reset(stmt);

    return [data autorelease];
} 
sqlite3_reset(stmt);

For saving as blob you can extract pointer and size of NSData using [data bytes] and [data length]. And then bind to your BLOB row

sqlite3_bind_blob (stmt, 2, [data bytes], [data length], SQLITE_TRANSIENT);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...