sqlite3在数据库中插入和读取BLOB数据
我需要读取 BLOB 数据并将其写入数据库。这是我的结构表
#define CREATE_TABLE_USERS_SQL "CREATE TABLE IF NOT EXISTS %@ ( \
UserID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, \
Name VARCHAR(50), \
Image BLOB);"
如何将其插入数据库,然后从中检索?
环境:iPhone SDK 4.1 SQLite3数据库。
此代码失败:
NSData * buf2 = [[NSData alloc] init];
sqlite3_column_blob(statement, 5);
buf2 = sqlite3_column_bytes(statement, 5);
user.image = [UIImage imageWithData: buf2];
I need to read and write BLOB data to a database. Here is my structure table
#define CREATE_TABLE_USERS_SQL "CREATE TABLE IF NOT EXISTS %@ ( \
UserID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, \
Name VARCHAR(50), \
Image BLOB);"
How can I insert it into database, and then retrieve from it?
Environment: iPhone SDK 4.1 SQLite3 database.
This code fails:
NSData * buf2 = [[NSData alloc] init];
sqlite3_column_blob(statement, 5);
buf2 = sqlite3_column_bytes(statement, 5);
user.image = [UIImage imageWithData: buf2];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
谢谢大家!!在您的帮助下,我解决了问题,并希望与像我一样的未来初学者分享结果。)
Thanx all!! With yours help I'v solved problem and want to share results for future beginners like I am.)
除了选择客户端或编程接口之外,用法与任何其他字符串字段没有什么不同。
[更新] 我还没有运气为 iPhone 进行开发,但我相信它与任何其他 SELECT 或 INSERT 查询相同。确保对 BLOB 进行编码,并用单撇号 (
'
) 将其括起来,就像字符串一样。Apart from choosing your client or programming interface, usage does not differ from any other string field.
[Update] I haven't had the luck to develop for the iPhone yet, but I believe it's the same as any other SELECT or INSERT query. Make sure to encode the BLOB and enclose it with single apostrophes (
'
) like strings.为什么不使用 Flying Meat 的 Gus Mueller 等人的
fmdb
呢? http://flyingmeat.com/https://github.com/ccgus/fmdb
如果您发现
fmdb
有用,请去购买一款 Flying Meat 的优秀应用程序。 VoodooPad 或 Acron。why not use
fmdb
by Gus Mueller et al, of Flying Meat? http://flyingmeat.com/https://github.com/ccgus/fmdb
If you find
fmdb
useful, go and buy one of the excellents apps of Flying Meat. VoodooPad or Acron.//-----插入图像
//--------获取图像
//-----insert IMAGE
//--------get image
您正在以正确的顺序调用 SQLite 函数。根据 SQLite 文档:
但是,buff2 是一个整数,而不是数据指针。
sqlite3_column_bytes
告诉您 blob 中有多少字节。You are calling the SQLite functions in the correct order. According to the SQLite docs:
However,
buff2
is an integer, not a data pointer.sqlite3_column_bytes
tells you how many bytes are in the blob.