sqlite3 insert and read BLOB data in database - select

I need to read and write BLOB data to a database. Here is my structure table
#define CREATE_TABLE_USERS_SQL "CREATE TABLE IF NOT EXISTS %# ( \
UserID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, \
Name VARCHAR(50), \
Image BLOB);"
How can I insert it into database, and then retrieve from it?
Environment: iPhone SDK 4.1 SQLite3 database.
This code fails:
NSData * buf2 = [[NSData alloc] init];
sqlite3_column_blob(statement, 5);
buf2 = sqlite3_column_bytes(statement, 5);
user.image = [UIImage imageWithData: buf2];

Thanx all!! With yours help I'v solved problem and want to share results for future beginners like I am.)
-(void) addToDB
{
NSLog(#"\nCreating db");
NSString *str = #"CREATE TABLE IF NOT EXISTS Images (image1 BLOB);";
int res = SQLITE_ERROR;
res = sqlite3_open([#"aa.sql" UTF8String], &database);
res = sqlite3_exec(database, [str UTF8String], NULL, NULL, NULL);
sqlite3_stmt *updStmt =nil;
const char *sql = "INSERT INTO Images (image1) VALUES (?);";
res = sqlite3_prepare_v2(database, sql, -1, &updStmt, NULL);
if(res!= SQLITE_OK)
{
NSLog(#"Error while creating update statement:%s", sqlite3_errmsg(database));
}
UIImage *img = [UIImage imageNamed: #"flower.png"];
NSData *imageData = [NSData dataWithData: UIImagePNGRepresentation(img)];
res = sqlite3_bind_blob(updStmt, 1, [imageData bytes], [imageData length] , SQLITE_TRANSIENT);
if((res = sqlite3_step(updStmt)) != SQLITE_DONE)
{
NSLog(#"Error while updating: %#", sqlite3_errmsg(database));
sqlite3_reset(updStmt);
}
res = sqlite3_reset(updStmt);
res = sqlite3_close(database);
}
-(void) readFromDB
{
NSLog(#"\nReading from db");
NSString *query = #"SELECT image1 from Images";
int res = SQLITE_ERROR;
int len = 0;
res = sqlite3_open([#"aa.sql" UTF8String], &database);
sqlite3_stmt *statement;
res = sqlite3_prepare_v2 (database, [query UTF8String], -1, &statement, nil);
if (res == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
len = sqlite3_column_bytes(statement, 0);
NSData *imgData = [[NSData alloc] initWithBytes: sqlite3_column_blob(statement, 0) length: len];
UIImage *img = [[UIImage alloc] initWithData:imgData];
self.view1.image = img;
}
}
sqlite3_finalize(statement);
res = sqlite3_close(database);
}

Apart from choosing your client or programming interface, usage does not differ from any other string field.
[Update] I haven't had the luck to develop for the iPhone yet, but I believe it's the same as any other SELECT or INSERT query. Make sure to encode the BLOB and enclose it with single apostrophes (') like strings.

why not use fmdb by Gus Mueller et al, of Flying Meat? http://flyingmeat.com/
https://github.com/ccgus/fmdb
If you find fmdb useful, go and buy one of the excellents apps of Flying Meat. VoodooPad or Acron.

//-----insert IMAGE
- (void)setImage:(UIImage *)theImage
{
if(sqlite3_open([databasePath UTF8String],&myDatabase)==SQLITE_OK)
{
NSString *insertProgrammeSql = #"INSERT INTO imageTable (imageName) VALUES (?)";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(myDatabase, [insertProgrammeSql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK) {
NSData *imageData = UIImagePNGRepresentation(theImage);
sqlite3_bind_blob(statement, 1, [imageData bytes], [imageData length], SQLITE_TRANSIENT);
sqlite3_step(statement);
}
sqlite3_close(myDatabase);
// return YES;
}
}
//--------get image
-(UIImage *)getImage
{
UIImage *image = nil;
if(sqlite3_open([databasePath UTF8String],&myDatabase)==SQLITE_OK)
{
NSString *selectSql = #"SELECT image FROM imageTable";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(myDatabase, [selectSql UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
int length = sqlite3_column_bytes(statement, 0);
NSData *imageData = [NSData dataWithBytes:sqlite3_column_blob(statement, 0 ) length:length];
image = [UIImage imageWithData:imageData];
sqlite3_finalize(statement);
}
sqlite3_close(myDatabase);
}
return image;
}

This code fails:
NSData * buf2 = [[NSData alloc] init];
sqlite3_column_blob(statement, 5);
buf2 = sqlite3_column_bytes(statement, 5);
user.image = [UIImage imageWithData: buf2];
You are calling the SQLite functions in the correct order. According to the SQLite docs:
The safest and easiest to remember policy is to invoke these routines
in one of the following ways:
sqlite3_column_text() followed by sqlite3_column_bytes()
sqlite3_column_blob() followed by sqlite3_column_bytes()
sqlite3_column_text16() followed by sqlite3_column_bytes16()
In other words, you should call sqlite3_column_text(),
sqlite3_column_blob(), or sqlite3_column_text16() first to force the
result into the desired format, then invoke sqlite3_column_bytes() or
sqlite3_column_bytes16() to find the size of the result. Do not mix
calls to sqlite3_column_text() or sqlite3_column_blob() with calls to
sqlite3_column_bytes16(), and do not mix calls to
sqlite3_column_text16() with calls to sqlite3_column_bytes().
However, buff2 is an integer, not a data pointer. sqlite3_column_bytes tells you how many bytes are in the blob.

Related

How can i insert encrypted data to sqlite. i'm getting an error while inserting encrypted data

How can i insert encrypted data to sqlite. i'm getting an error while inserting encrypted data. bcos the encrypted so many single quotes and double quotes so while am creating my sql the string is breaking. is there any other way to insert data without data lose.
also i'm afraid to use add slashes method bsoc it may alter my actual encrypted data. Can anyone give me a suggestion.. Also please find my insert query function below
-(BOOL) insertItemData:(NSString *)encryptedData folderId:(NSString *)folderId
{
bool giveBackValue = 0;
database = [[[DBConnection alloc] init] autorelease];
if(sqlite3_open([[database filePath] UTF8String], &db) == SQLITE_OK)
{
NSString *sql = [[[NSString alloc] initWithFormat:#"INSERT INTO tbl_content (FolderId, Content) VALUES ('%#', '%#');", folderId, encryptedData] autorelease];
NSLog(#"%#",sql);
char *sqlError;
if(sqlite3_exec(db, [sql UTF8String], nil, nil, &sqlError) == SQLITE_OK)
{
giveBackValue = 1;
}
else
{
//Query exec failed
}
}
else
{
//DB Open failed
}
return giveBackValue;
}
My select query function
-(void)getFirstJsonListInFolder:(NSString *)folderId listCarrier:(NSMutableArray **)listCarrier
{
database = [[[DBConnection alloc] init] autorelease];
NSMutableArray *dummyListCarrier = [[[NSMutableArray alloc] init] autorelease];
if (sqlite3_open([[database filePath] UTF8String], &db) ==SQLITE_OK)
{
NSString *sql = [[NSString alloc] initWithFormat:#"SELECT Content from tbl_content WHERE FolderId = '%#'", folderId];
sqlite3_stmt *result;
if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &result, nil) == SQLITE_OK)
{
while (sqlite3_step(result) == SQLITE_ROW)
{
char *contentList = (char *)sqlite3_column_text(result, 0);
NSString *contentListString = [[NSString alloc] initWithUTF8String:contentList];
[dummyListCarrier addObject:contentListString];
[contentListString release];
}
}
else
{
//Query exec failed
}
}
else
{
//DB Open failed
}
*listCarrier = dummyListCarrier;
}
You should avoid directly assigning values to the columns in sql statements. Instead you should use prepared statement and bind values to it.
NSString *sql = #"INSERT INTO tbl_content (FolderId, Content) VALUES ('?', '?');";
char *sql = (char *) [sql UTF8String];
sqlite3_bind_text(stmt, 1, [Content UTF8String], -1, SQLITE_TRANSIENT);
*This is not complete code

To retrieve BLOB image from sqlite

The code for saving image:
NSData *imageData=UIImagePNGRepresentation(animalImage);
NSString *insertSQL=[NSString stringWithFormat:#"insert into AnimalsTable (name,propertyID,animalID,breed,mainBreed,dateofbirth,sex,notes,imageData) values(\"%#\",\"%#\",\"%#\",\"%#\",\"%#\",\"%#\",\"%#\",\"%#\",\"%#\")",nameString,propertyString,animalidString,breedString,mainBreedString,dateString,sexString,notesString,imageData];
sqlite3_stmt *addStatement;
NSLog(#"%#",appDelegate.sqlFile);
const char *insert_stmt=[insertSQL UTF8String];
if (sqlite3_open([appDelegate.sqlFile UTF8String],&database)==SQLITE_OK) {
sqlite3_prepare_v2(database,insert_stmt,-1,&addStatement,NULL);
if (sqlite3_step(addStatement)==SQLITE_DONE) {
sqlite3_bind_blob(addStatement, 1, [imageData bytes], [imageData length], SQLITE_TRANSIENT);
NSLog(#"Data saved");
}
else{
//NSAssert1(0, #"Error while updating. '%s'", sqlite3_errmsg(database));
NSLog(#"Some Error occured");
}
sqlite3_close(database);
For retrieving image:
NSString *select_sql=[NSString stringWithFormat:#"select name,animalID,imageData from AnimalsTable where mainBreed='%#' AND breed='%#'",mainString,subString];
const char *sql = [select_sql UTF8String];
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSString *animalName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
NSString *animalid=[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
NSData *dataForCachedImage = [[NSData alloc] initWithBytes:sqlite3_column_blob(selectstmt, 2) length: sqlite3_column_bytes(selectstmt, 2)];
[animalNamesArray addObject:animalName];
[animalIDArray addObject:animalid];
[imageDataArray addObject:dataForCachedImage];
}
}
}
else
sqlite3_close(database);
In UITableView CellForIndexPath:
NSData *dataForCachedImage=[imageDataArray objectAtIndex:indexPath.row];
UIImage *dataImage=[[UIImage alloc] init];
dataImage=[UIImage imageWithData:dataForCachedImage];
cell.imageView.image=dataImage;
When I debug the code:
I get bytes for NSData=226381 bytes And for dataImage 0x0.
Please help me.
You need to properly bind your values to the query for it to work. When you set the query on the first line, the description of the data would be inserted and not the correct data. If you were to log the query you would likely see something like <0FFAEC32 ... 23F0E8D1>.
You then attempted to bind the blob properly later but since your query was not written correctly it had no effect.
//Although you try to bind a blob here it does not get bound
//to anything since you do not include a parameter template.
sqlite3_bind_blob(addStatement, 1, [imageData bytes], [imageData length], SQLITE_TRANSIENT);
To fix this here is a minimal example:
//query does not need to be an NSString* but just a const char *
//replace all %# with ?
const char *sql = "INSERT INTO AnimalsTable(name, ..., imageData) values(?, ..., ?)";
if (sqlite3_open([appDelegate.sqlFile UTF8String],&database)==SQLITE_OK) {
sqlite3_prepare_v2(database,insert_stmt,-1,&addStatement,NULL);
//Bind all of the values here before you execute the statement
sqlite3_bind_text(addStatement, 1, [nameString UTF8String], -1, NULL);
...
sqlite3_bind_blob(addStatement, 9, [imageData bytes], [imageData length], SQLITE_TRANSIENT);
if (sqlite3_step(addStatement)==SQLITE_DONE) {
NSLog(#"Data saved");
}
...
//You must finalize your statement before closing the database
sqlite3_finalize(addStatement);
sqlite3_close(database);
Using binded values helps prevent inserting invalid data which could be a SQL injection attack and improves query performance for queries that are run often.
Note*: If you are going to be doing a lot of SQL consider using FMDB or even Core Data.

Sqlite3 Fails Unless I Compile Without Optimization But Only On iOS 3.1.3

I have a very odd problem. My sqlite use in my project which has been working for a while has now stopped working on Xcode 4 compiled code, but only on devices with iOS3.1.3. It fails to return records, but if I turn off code optimization it works perfectly.
Does anyone know what may be the cause of this or has experienced similar issues.
Thanks
Rael
// get word from database
NSString *query = [[NSString alloc] initWithFormat:#"select kana_word, kanji_word, word_type_name, word_subtype_name, custom_category_name, kana_index, dictionary_id from dictionary join word_type on word_type.word_type_id=dictionary.word_type_id join word_subtype on word_subtype.word_subtype_id=dictionary.word_subtype_id join custom_category on custom_category.custom_category_id=dictionary.custom_category_id where kana_index=%d", inKanaIndex];
sqlite3_stmt *statement;
NSLog(#"%#", query);
const char *cQuery = [query UTF8String];
if (sqlite3_prepare_v2(jmwDb, cQuery, -1, &statement, nil) != SQLITE_OK) {
char *errMessage;
errMessage = (char*)sqlite3_errmsg(jmwDb);
sqlite3_finalize(statement);
[query release];
return nil;
}
// build a word object
int kanji_key;
if (sqlite3_step(statement) == SQLITE_ROW) {
// create new word
self.kana = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)];
self.kanji = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)];
self.wordType = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 2)];
self.wordSubType = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 3)];
self.customCategory = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 4)];
self.kanaIndex = sqlite3_column_int(statement, 5);
kanji_key = sqlite3_column_int(statement, 6);
self.dictionaryId = kanji_key;
}
else {
sqlite3_finalize(statement);
[query release];
return nil;
}

iphone store image in application which are chosen from gallery

I searched a lot in google but not able to get the answer required. I am choosing photos from the image gallery and I want to store it in my application. If I have to store it in database(As BLOB), then I have to enter the name of the file(which I am not getting as I have chosen from the gallery). Can I store it anywhere else? please help. I am stuck in this from a long time. I need to extract the image and show it in map calloutview.
I am using the below code to choose photo from gallery and insert into the database
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)dictionary {
[picker dismissModalViewControllerAnimated:YES];
tempImg.image = image;
NSData *imgData = UIImagePNGRepresentation(tempImg.image);
NSLog(#"the img data %#",imgData);
sql = [NSString stringWithFormat:#"update latlng set image = '%#' where placeName = '%#'",imgData,pName.text];
The below code excuting the sql statement mentioned above
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement;
sqlStatement = [sql UTF8String];;
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
if (sqlite3_step(compiledStatement))
{
NSLog(#"YES");
}
}
else
{
printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database));
}
sqlite3_finalize(compiledStatement);
}sqlite3_close(database);
The below code to extract from database
NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 5) length:sqlite3_column_bytes(compiledStatement, 5)];
the below code to display the image in callout
UIImageView *mapImg = [[UIImageView alloc] initWithImage [UIImageimageWithData:imgData]];
pinView.leftCalloutAccessoryView = mapImg;
Convert the image obtained for photo gallery to NSData and then save it in database as a Blob,So you can easily retrieve the nsdata from database when ever you needed.
NSData *image1Data = UIImagePNGRepresentation(image1.image);
sqlite3_bind_blob(init_statement, 1, [image1Data bytes], [image1Data length], NULL);
image1 is the ImageView where i am displaying the image from the photo gallery.
All the best.
You can use this to retrieve the path to a directory writable by your application:
NSString *directory = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
There are various ways to access that directory, including NSData's writeToFile: methods, the NSFileManager class, the NSFileHandle class, and even C stdio functions.
In my case i searching lots of google and then finaly got solution that below code its work's for me i hope its work for you
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(#"info: %#",info);
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirec = [paths objectAtIndex:0];
NSString *imgePath = [documentsDirec stringByAppendingPathComponent:#"note.sqlite"];
if(sqlite3_open([imgePath UTF8String], &database) == SQLITE_OK){
const char *sql = "insert into images (images) values (?)";
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) == SQLITE_OK){
UIImage *edtedImae = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *dataForImage = UIImagePNGRepresentation(edtedImae);
sqlite3_bind_blob(addStmt, 1, [dataForImage bytes], [dataForImage length], SQLITE_TRANSIENT);
}
if(sqlite3_step(addStmt) != SQLITE_DONE){
NSLog(#"error %s",sqlite3_errmsg(database));
}
else {
NSLog(#"Insert row Id = %d",sqlite3_last_insert_rowid(database));
}
sqlite3_finalize(addStmt);
}
sqlite3_close(database);
[picker dismissModalViewControllerAnimated:YES];
}
thank you

problem in reading image from data base in iphone

hi i am able to store the image into database by using the following code.......
-(void)insertimages: (NSData *)image
{
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
NSUInteger len = [image length];
NSLog(#"data size is %d", len);
sqlStatement="insert into messages values(10,?)";
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
//sqlite3_bind_int(compiledStatement, 1, -1);
//sqlite3_bind_blob(updateStmt, 3, [imgData bytes], [imgData length], NULL);
sqlite3_bind_blob(compiledStatement, 1, [image bytes], [image length], SQLITE_TRANSIENT);
//sqlite3_bind_text(compiledStatement, 1, [SMSBody UTF8String],-1,SQLITE_STATIC);
if(sqlite3_step(compiledStatement) == SQLITE_DONE)
{
sqlite3_finalize(compiledStatement);
}
}
sqlite3_close(database);
}
}
but now my problem is i am not able to retrieve the image from the database and to display it in uiimageview...
con any one please help me how to do that.
Use following method to read the image data from database and stored the images in array or in dictionary
-(void) readImageDataFromDatabase {
// Setup the database object
sqlite3 *database;
// Init the img Array
imageArray = [[NSMutableArray alloc] init];
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "select * from messages";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSURL *url = [NSURL URLWithString:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]];
];
NSData *imgData = [NSData dataWithContentsOfURL: url];
// Create a new image object with the data from the database
iconImage.image=[UIImage imageWithData:imgData];
// Add the img object to the img Array
[imageArray addObject:iconImage];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
I was trying to do this a year ago, I wrote code to save the images into a sqlite3 db using osx sdk (of which i was able to write and read, i could even store the images in the db with ruby and read them on with macosx sdk). But when i moved this exact code over to an iOS app it would pull out bad data. If you do work this out i'd like to know the answer.