下面是我在sqlite数据库中存储图像的代码。当我用它来存储它的值时,它正在尝试将图像存储在sqlite数据库中。我不知道我做错了什么。我已经搜索过,我无法得到我需要的答案。任何人都帮我解决他的代码。
sqlite3 *database;
dbName=@"dataTable.sqlite";
NSArray *documentpath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentdir=[documentpath objectAtIndex:0];
dbPath=[documentdir stringByAppendingPathComponent:dbName];
sqlite3_stmt *compiledStmt;
if(sqlite3_open([dbPath UTF8String], &database)==SQLITE_OK){
NSLog(@"Name:%@,Company:%@,URL:%@",model.personName,model.companyName,model.imgurl);
const char *insertSQL="insert into Persons(PersonName,CompanyName,ImgUrl,PersonImage)values(?,?,?,?)";
if(sqlite3_prepare_v2(database,insertSQL, -1, &compiledStmt, NULL)==SQLITE_OK){
sqlite3_bind_text(compiledStmt,1,[model.personName UTF8String],-1,SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStmt,2,[model.companyName UTF8String],-1,SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStmt,3,[model.imgurl UTF8String],-1,SQLITE_TRANSIENT);
NSData *imageData=UIImagePNGRepresentation(imageView.image);
sqlite3_bind_blob(compiledStmt, 4, [imageData bytes], [imageData length], NULL);
NSLog(@"Prepare");
sqlite3_step(compiledStmt);
}sqlite3_finalize(compiledStmt);
}
更新 感谢所有人..我通过在此处提出另一个问题来解决此问题。store and retrieve image into sqlite database for iphone这可能有助于其他人。
答案 0 :(得分:2)
const char *insertSQL="insert into Persons(PersonName,CompanyName,ImgUrl,PersonImage)values(?,?)"
您有4个值可插入到表格中参数只有2个占位符。纠正它们。
哎呀我不是iOS开发人员
答案 1 :(得分:1)
你只需将libSqlite3.dylib添加到Linked FrameWork和Lilbraries并在.h文件中声明数据库变量
//Database Variables
@property (strong, nonatomic) NSString *databasePath;
@property (nonatomic)sqlite3 *contactDB;
@property (strong, nonatomic) IBOutlet UIButton *backbtn;
@property (strong, nonatomic) IBOutlet UIButton *forwardbtn;
拖放UIImageView并将其命名为...我声明为imgView。 转到.m文件,你只需复制并粘贴该代码
int i=1;
long long temp=0;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
_databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"images.db"]];
//docsDir NSPathStore2 * @"/Users/gayathiridevi/Library/Application Support/iPhone Simulator/7.0.3/Applications/B5D4D2AF-C613-45F1-B414-829F38344C2A/Documents" 0x0895e160
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: _databasePath ] == NO)
{
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS IMAGETB (ID INTEGER PRIMARY KEY AUTOINCREMENT,URL TEXT, CHECKSUM TEXT,IMAGE BLOB)";
if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog( @"User table Not Created Error: %s", errMsg);
}
else
{
NSLog( @"User table Created: ");
}
sqlite3_close(_contactDB);
}
else {
NSLog( @"DB Not Created");
}
}
[self saveImage];
[self showImage];
}
- (void)saveImage
{
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{
NSString *insertSQL=@"INSERT INTO IMAGETB(URL,image) VALUES(?,?)";
if(sqlite3_prepare_v2(_contactDB, [insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL)== SQLITE_OK)
{
//NSString *url =@"https://lh6.googleusercontent.com/-vJBBGUtpXxk/AAAAAAAAAAI/AAAAAAAAADQ/nfgVPX1n-Q8/photo.jpg";
//NSString *url =@"http://upload.wikimedia.org/wikipedia/commons/2/2a/Junonia_lemonias_DSF_upper_by_Kadavoor.JPG";
// NSString *url =@"http://upload.wikimedia.org/wikipedia/commons/8/84/Tibia_insulaechorab.jpg";
NSString *url =@"http://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/PNG_transparency_demonstration_2.png/280px-PNG_transparency_demonstration_2.png";
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
NSData *imageData=UIImagePNGRepresentation(image);
sqlite3_bind_text(statement,1, [url UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_blob(statement, 2, [imageData bytes], [imageData length], SQLITE_TRANSIENT);
NSLog(@"Length from internet : %lu", (unsigned long)[imageData length]);
}
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog( @"Insert into row id %lld",(sqlite3_last_insert_rowid(_contactDB)));
temp =(sqlite3_last_insert_rowid(_contactDB));
}
else {
NSLog( @"Error IN INSERT" );
}
sqlite3_finalize(statement);
sqlite3_close(_contactDB);
}
}
- (void)showImage
{
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if(sqlite3_open(dbpath,&_contactDB)==SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:@"Select IMAGE FROM IMAGETB WHERE ID = %d",i];
if(sqlite3_prepare_v2(_contactDB,[insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK) {
while(sqlite3_step(statement) == SQLITE_ROW) {
int length = sqlite3_column_bytes(statement, 0);
NSData *imageData = [NSData dataWithBytes:sqlite3_column_blob(statement, 0) length:length];
NSLog(@"Length from db : %lu", (unsigned long)[imageData length]);
if(imageData == nil)
NSLog(@"No image found.");
else
_imgView.image = [UIImage imageWithData:imageData];
NSLog(@"image found.");
}
}
sqlite3_finalize(statement);
}
sqlite3_close(_contactDB);
}
-(IBAction)backBtn:(id)sender {
if (i<=1) {}
else{
i=i-1;
[self showImage];
}
}
-(IBAction)forwardBtn:(id)sender {
if(i==temp){}
else{
i=i+1;
[self showImage];
}
}
答案 2 :(得分:0)
我用这个答案回答了一个类似的问题:如果你使用CoreData,它会更好。使用CoreDate而不是SQL将更容易。 CoreData几乎是一个很好的包装器中的SQL数据库。
如果使用iOS 5,您可以轻松地将图像添加到数据库,而不必担心它们是BLOBS(二进制大对象),方法是选中&#34;允许外部存储&#34;。
答案 3 :(得分:0)
您应该检查bind_text和bind_blob以及步骤调用的返回值,在失败时打印错误消息。