找到如何使用sqlite3_bind_blob
您NSData
存儲在數據庫中的例子見https://stackoverflow.com/a/17994313/1271826。
此外,您絕對應該避免使用stringWithFormat
來構建SQL語句。如果地址是123 O'Brian Way
或Martha's Vineyard
?該撇號會過早地終止您插入的字符串。雙引號不會更好,當你去插入Jimmy "The Greek" Snyder
或Dwayne "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來存儲密碼。
while sqlite3_bind_text while all the fields not get update.that爲什麼我使用這種方法來保存數據和圖像。 – Anand3777
我得到的問題,而更新,而不是在插入.. – Anand3777
@ user2315316沒關係。無論是'INSERT','UPDATE'還是隻是'SELECT'語句中的WHERE子句,您都應該執行'sqlite3_bind_xxx'。如果之前沒有使用'sqlite3_bind_xxx',那麼您可能不會查看'sqlite3_errmsg'來確定問題的根源並對其進行補救。請注意,如果這些值中的任何一個值可能爲「nil」,那麼您還需要檢查該情況,並在該情況下使用sqlite3_bind_null。 – Rob