2012-01-04 184 views
1

下面是我的代碼存儲在SQLite數據庫圖像。當我用它來存儲它的價值,現在我試圖在sqlite數據庫中存儲圖像。我不知道我做錯了什麼。我已經搜查過了,我無法得到我需要的答案。任何人都可以用他的代碼幫助我將圖像存儲到SQLite數據庫

sqlite3 *database; 
    [email protected]"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); 
} 

UPDATE: 感謝大家。我清除這個問題通過問另一個問題從這裏.. store and retrieve image into sqlite database for iphone這可以幫助別人。

+0

IM有代碼來讀取我的appdelegate數據庫,查看應用程序的同時在我的項目啓動的文件.... – 2012-01-04 11:29:04

+0

請一次只問一個* *問題。 – Sathya 2012-01-04 11:33:00

+0

@Sathya OKK ......你能幫助我與我的第一question..i將編輯.. – 2012-01-04 11:34:48

回答

0

我回答這個答案一個similair問題:它的更好,如果你使用CoreData代替。使用CoreDate而不是SQL可以輕鬆得多。 CoreData幾乎是一個漂亮的包裝器中的SQL數據庫。

如果您使用的iOS 5,你可以圖像數據庫輕鬆添加,而不必擔心他們的檢查是BLOBS(二進制大對象)「允許外部存儲」。

+2

我已經知道that..I必須開發應用程序的ios4..i必須使用sqlite3的..即時通訊只使用2張圖片...所以我需要檢查出正確的代碼..不管怎麼說,謝謝.. – 2012-01-04 11:57:56

2
const char *insertSQL="insert into Persons(PersonName,CompanyName,ImgUrl,PersonImage)values(?,?)" 

你有4個值插入到表&只有2佔位符的參數。糾正他們。

哎呀我不是一個iOS開發者

+0

感謝您的答案...但它仍然不工作.. – 2012-01-04 11:38:05

+0

「不能正常工作」是一個壞評論。我們不介意讀者在這裏弄清楚你的屏幕上發生了什麼。添加一些錯誤日誌,至少自己做一些*基本的*調試。 @ DineshRaja.MaG – Sathya 2012-01-04 11:40:40

+0

既然你有無形的佔位符,也傳遞參數,以便.. – nawfal 2012-01-04 11:42:48

0

你應該檢查bind_text和bind_blob和腳步調用的返回值,如果他們無法打印錯誤消息。

+0

兄弟...我的代碼沒有執行後sqlite_prepare_v2聲明..我不知道y?...任何東西即時通訊做錯了? – 2012-01-04 12:04:23

+1

設置if的其他部分if(sqlite3_prepare_v2(database,...)'像'else NSLog(@「準備失敗(%s)」,sqlite3_errmsg(數據庫));'。這會告訴你更多的信息。 – 2012-01-04 13:32:24

1

你只需要添加libSqlite3.dylib到聯骨架和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 *[email protected]"INSERT INTO IMAGETB(URL,image) VALUES(?,?)"; 

    if(sqlite3_prepare_v2(_contactDB, [insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL)== SQLITE_OK) 
    { 
     //NSString *url [email protected]"https://lh6.googleusercontent.com/-vJBBGUtpXxk/AAAAAAAAAAI/AAAAAAAAADQ/nfgVPX1n-Q8/photo.jpg"; 
     //NSString *url [email protected]"http://upload.wikimedia.org/wikipedia/commons/2/2a/Junonia_lemonias_DSF_upper_by_Kadavoor.JPG"; 
     // NSString *url [email protected]"http://upload.wikimedia.org/wikipedia/commons/8/84/Tibia_insulaechorab.jpg"; 


     NSString *url [email protected]"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];   
} 
} 
+0

我發佈我的代碼。我相信它會幫助你... – 2014-09-11 10:39:52

相關問題