可以将图像上传到 SQLite,但可以再次提取它
我有一个应用程序,可以从库中选择照片并将其设置到我的应用程序中的 UIImageviev 中。 然后我也将它保存到 SQLite 表中,但无法通过名称搜索再次提取它。我知道保存方法是成功的,因为我从终端应用程序手动控制它。我的提取方法有什么问题吗???
//Save the photo which is currently in an UIImage Variable into the SQLITE photos as BLOB
-(IBAction)SaveImage{
sqlite3_stmt *save_statement;
const char *dbpath = [databasePath UTF8String];
NSString *insertSQL = [NSString stringWithFormat: @"insert into photos (name, photo) values(?, ?)"];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_open(dbpath, &photosDB);
sqlite3_prepare(photosDB, insert_stmt, -1, &save_statement, NULL);
sqlite3_bind_text(save_statement, 1, [text.text UTF8String], -1, SQLITE_TRANSIENT);
NSData *binData = UIImagePNGRepresentation(image.image);
if (sqlite3_bind_blob(save_statement, 2, [binData bytes], [binData length], SQLITE_TRANSIENT)==SQLITE_OK) {
status.text = @"Gonderi Kaydedildi";}else{status.text = @"Tanimlanamayan bir sorun var";}
sqlite3_step(save_statement);
sqlite3_finalize(save_statement);
sqlite3_close(photosDB);}
下面我遇到了一些问题...
//Get the photo from database construct it and set it into the UIImageView variable in our application
-(IBAction)ImageFromDatabase{
sqlite3_stmt *call_statement;
const char *dbpath = [databasePath UTF8String];
NSString *findSQL = [NSString stringWithFormat: @"SELECT name, photo FROM photos WHERE name=\"%@\"", [text text]];
const char *sql = [findSQL UTF8String];
sqlite3_open(dbpath, &photosDB);
sqlite3_prepare(photosDB, sql, -1, &call_statement, NULL);
if (sqlite3_step(call_statement) == SQLITE_ROW) {
NSString *imageName = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(call_statement, 0)];
status.text = imageName;
const char *rawData = sqlite3_column_blob(call_statement, 2);
int rawDataLength = sqlite3_column_bytes(call_statement, 2);
NSData *data = [NSData dataWithBytes:rawData length:rawDataLength];
UIImage *image1 = [[UIImage alloc]initWithData:data];
image.image = image1;}
sqlite3_step(call_statement);
sqlite3_finalize(call_statement);
sqlite3_close(photosDB);
}
我觉得问题特别在下面,但是?!?!?!?!
const char *rawData = sqlite3_column_blob(call_statement, 2);
int rawDataLength = sqlite3_column_bytes(call_statement, 2);
NSData *data = [NSData dataWithBytes:rawData length:rawDataLength];
UIImage *image1 = [[UIImage alloc]initWithData:data];
image.image = image1;
你看到了吗???
i have an application where pick photo from library and set it into the UIImageviev in my app.
Then i also save it into SQLite table however can not extract it again by name searching. I know saving method is successful as i control it from Terminal application manually. What is wrong with my extracting method???
//Save the photo which is currently in an UIImage Variable into the SQLITE photos as BLOB
-(IBAction)SaveImage{
sqlite3_stmt *save_statement;
const char *dbpath = [databasePath UTF8String];
NSString *insertSQL = [NSString stringWithFormat: @"insert into photos (name, photo) values(?, ?)"];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_open(dbpath, &photosDB);
sqlite3_prepare(photosDB, insert_stmt, -1, &save_statement, NULL);
sqlite3_bind_text(save_statement, 1, [text.text UTF8String], -1, SQLITE_TRANSIENT);
NSData *binData = UIImagePNGRepresentation(image.image);
if (sqlite3_bind_blob(save_statement, 2, [binData bytes], [binData length], SQLITE_TRANSIENT)==SQLITE_OK) {
status.text = @"Gonderi Kaydedildi";}else{status.text = @"Tanimlanamayan bir sorun var";}
sqlite3_step(save_statement);
sqlite3_finalize(save_statement);
sqlite3_close(photosDB);}
Below i have some problem...
//Get the photo from database construct it and set it into the UIImageView variable in our application
-(IBAction)ImageFromDatabase{
sqlite3_stmt *call_statement;
const char *dbpath = [databasePath UTF8String];
NSString *findSQL = [NSString stringWithFormat: @"SELECT name, photo FROM photos WHERE name=\"%@\"", [text text]];
const char *sql = [findSQL UTF8String];
sqlite3_open(dbpath, &photosDB);
sqlite3_prepare(photosDB, sql, -1, &call_statement, NULL);
if (sqlite3_step(call_statement) == SQLITE_ROW) {
NSString *imageName = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(call_statement, 0)];
status.text = imageName;
const char *rawData = sqlite3_column_blob(call_statement, 2);
int rawDataLength = sqlite3_column_bytes(call_statement, 2);
NSData *data = [NSData dataWithBytes:rawData length:rawDataLength];
UIImage *image1 = [[UIImage alloc]initWithData:data];
image.image = image1;}
sqlite3_step(call_statement);
sqlite3_finalize(call_statement);
sqlite3_close(photosDB);
}
I feel that problem is below esspecilly but ?!?!?!?!
const char *rawData = sqlite3_column_blob(call_statement, 2);
int rawDataLength = sqlite3_column_bytes(call_statement, 2);
NSData *data = [NSData dataWithBytes:rawData length:rawDataLength];
UIImage *image1 = [[UIImage alloc]initWithData:data];
image.image = image1;
You saw it???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在从第 2 列中提取照片。引用 SQLite 参考页面;
您只选择了 2 列,编号为 0 和 1,因此从第 2 列读取将失败。
在 sqlite3_bind_blob 中,索引从 1 开始,这就是插入成功的原因。
You're extracting the photo from column 2. To quote the SQLite reference page;
You're only selecting 2 columns, numbered 0 and 1, so your read from column #2 will fail.
In sqlite3_bind_blob the index starts at 1 though, which is why your insert succeeds.