2013-08-29 21 views
-2

我想使用此查詢存儲數據與圖像,一切正常更新,但圖像列總是null.i不知道這個query.please什麼是錯誤的任何人幫助我修復它。我也是iOS和sqlite的新手。如何更新使用ios的sqlite中的圖像

UIImage *uiimg = img.image; 
    NSData *data = UIImagePNGRepresentation(uiimg); 

    NSString *cmd = [NSString stringWithFormat:@"update APPUSERS set name = '%@', u_name =   '%@', contact_no = '%@', address = '%@', dob = '%@', pswrd = '%@', confirm_pswrd = '%@', img = '%d' where u_name ='%@'",name.text, uname.text, pnoneno.text, address.text, dob.text, password.text, confirmpassword.text, data, tempstore] ; 

    const char * sql = [cmd UTF8String]; 


    if (sqlite3_step(update_statement) == SQLITE_DONE) 
    { 
      NSLog(@"success"); 
    } 
     else 
    { 
      NSLog(@"failed"); 

    } 
+0

while sqlite3_bind_text while all the fields not get update.that爲什麼我使用這種方法來保存數據和圖像。 – Anand3777

+0

我得到的問題,而更新,而不是在插入.. – Anand3777

+0

@ user2315316沒關係。無論是'INSERT','UPDATE'還是隻是'SELECT'語句中的WHERE子句,您都應該執行'sqlite3_bind_xxx'。如果之前沒有使用'sqlite3_bind_xxx',那麼您可能不會查看'sqlite3_errmsg'來確定問題的根源並對其進行補救。請注意,如果這些值中的任何一個值可能爲「nil」,那麼您還需要檢查該情況,並在該情況下使用sqlite3_bind_null。 – Rob

回答

3

找到如何使用sqlite3_bind_blobNSData存儲在數據庫中的例子見https://stackoverflow.com/a/17994313/1271826

此外,您絕對應該避免使用stringWithFormat來構建SQL語句。如果地址是123 O'Brian WayMartha's Vineyard?該撇號會過早地終止您插入的字符串。雙引號不會更好,當你去插入Jimmy "The Greek" SnyderDwayne "The Rock" Johnson。更糟糕的是,惡意用戶可能會對SQL注入造成嚴重破壞。

您應該使用?佔位符和sqlite3_bind_blob函數爲NSData。對於你的琴絃,請使用sqlite3_bind_text

這樣:

UIImage *uiimg = img.image; 
NSData *data = UIImagePNGRepresentation(uiimg); 

const char *sql = "update APPUSERS set name = ?, u_name = ?, contact_no = ?, address = ?, dob = ?, pswrd = ?, confirm_pswrd = ?, img = ? where u_name =?"; 

if (sqlite3_prepare_v2(database, sql, -1, &update_statement, NULL) != SQLITE_OK) 
    NSLog(@"prepare failed: %s", sqlite3_errmsg(database)); 

if (sqlite3_bind_text(update_statement, 1, [name.text UTF8String], -1, NULL) != SQLITE_OK) 
    NSLog(@"bind 1 failed: %s", sqlite3_errmsg(database)); 

if (sqlite3_bind_text(update_statement, 2, [uname.text UTF8String], -1, NULL) != SQLITE_OK) 
    NSLog(@"bind 2 failed: %s", sqlite3_errmsg(database)); 

if (sqlite3_bind_text(update_statement, 3, [pnoneno.text UTF8String], -1, NULL) != SQLITE_OK) 
    NSLog(@"bind 3 failed: %s", sqlite3_errmsg(database)); 

if (sqlite3_bind_text(update_statement, 4, [address.text UTF8String], -1, NULL) != SQLITE_OK) 
    NSLog(@"bind 4 failed: %s", sqlite3_errmsg(database)); 

if (sqlite3_bind_text(update_statement, 5, [dob.text UTF8String], -1, NULL) != SQLITE_OK) 
    NSLog(@"bind 5 failed: %s", sqlite3_errmsg(database)); 

if (sqlite3_bind_text(update_statement, 6, [password.text UTF8String], -1, NULL) != SQLITE_OK) 
    NSLog(@"bind 6 failed: %s", sqlite3_errmsg(database)); 

if (sqlite3_bind_text(update_statement, 7, [confirmpassword.text UTF8String], -1, NULL) != SQLITE_OK) 
    NSLog(@"bind 7 failed: %s", sqlite3_errmsg(database)); 

// note, use blob here 

if (sqlite3_bind_blob(update_statement, 8, [data bytes], [data length], SQLITE_TRANSIENT) != SQLITE_OK) 
    NSLog(@"bind 8 failed: %s", sqlite3_errmsg(database)); 

if (sqlite3_bind_text(update_statement, 9, [tempstore UTF8String], -1, NULL) != SQLITE_OK) 
    NSLog(@"bind 9 failed: %s", sqlite3_errmsg(database)); 

if (sqlite3_step(update_statement) == SQLITE_DONE) 
{ 
    NSLog(@"success"); 
} 
else 
{ 
    NSLog(@"failed: %s", sqlite3_errmsg(database)); 
} 

sqlite3_finalize(update_statement); 

注意,除了做sqlite3_bind_xxx,我也建議(一)不要忘記做sqlite3_prepare_v2第一; (二)任何錯誤,請看sqlite3_errmsg; (c)記得在最後做sqlite3_finalize以釋放你的記憶。

請注意,儘管如此,SQLite在存儲大型blob方面效率不高。如果圖像是小縮略圖,這不是問題,但對於大圖像,您應該將圖像保存到Documents文件夾,然後只保存數據庫中的文件路徑。

最後,請注意,如果您有加載到UIImage的圖像,然後通過UIImagePNGRepresentation重新檢索,則可能導致數據丟失或文件可能變得更大。它取決於圖像的原始來源。人們經常會假設他們是否確實獲得了相同的圖像,雖然它看起來幾乎完全相同,但可以在過程中進行更改(並非總是;視情況而定)。如果您有權訪問原始資產/文件/ NSData,您曾經設置UIImage,那麼您應該回頭參考一下。


作爲一個不相干的人,你真的不應該在數據庫中以明文存儲密碼。您應該加密它們或使用keychain來存儲密碼。

+0

不錯的答案......! – preetam

+0

是啊..它的工作就像一個魅力...謝謝.. – Anand3777

1

試圖通過NSFileManager.These創建NSDocument在文檔文件夾中的數據庫和圖像保存圖像名稱是單instances.You可以節省這是關係到applciation的文件手機內存文件夾中的圖像

例如,你可以在這條道路Read/write file in Documents directory problem