Read data from Sqlite in Objective-C - ios

I am trying to read data from sqlite in Objective-C, but the result is QuestionNumber and TotalRate both is 0, can not read value from sqlite. Please helper how should I change my code?
-(void)readSqlite{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Questiondata.db"];
NSLog(#"filePath,%#",filePath);
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:filePath]){
NSLog(#"database is exist.");
NSMutableArray *retArray = [[NSMutableArray alloc] init];
const char *dbpath = [filePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) {
NSLog(#"THIS IS OK");
const char *insert_stmt = "SELECT QuestionNumber, TotalRate FROM Questions WHERE QuestionNumber = 2";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_contactDB, insert_stmt, -1, &statement, NULL) == SQLITE_OK) {
NSLog(#"SQLITE_OK");
int QuestionNumberx = sqlite3_column_int(statement, 0);
int TotalRate = sqlite3_column_int(statement, 1);
NSLog(#"QuestionNumberx %d",QuestionNumberx );
NSLog(#"TotalRate %d",TotalRate );
[retArray addObject:[NSNumber numberWithInt:QuestionNumberx]];
[retArray addObject:[NSNumber numberWithInt:TotalRate]];
NSLog(#"%s db err '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(_contactDB), sqlite3_errcode(_contactDB));
NSLog(#"retArray %#",retArray );
sqlite3_finalize(statement);
}
}
}else{
NSLog(#"database is not exist.");
}
}

You never actually execute the query with sqlite3_step. You also never close the database.
The following assumes you will only get a single record back from your query:
-(void)readSqlite{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Questiondata.db"];
NSLog(#"filePath,%#",filePath);
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:filePath]){
NSLog(#"database is exist.");
NSMutableArray *retArray = [[NSMutableArray alloc] init];
const char *dbpath = [filePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) {
NSLog(#"THIS IS OK");
const char *insert_stmt = "SELECT QuestionNumber, TotalRate FROM Questions WHERE QuestionNumber = 2";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_contactDB, insert_stmt, -1, &statement, NULL) == SQLITE_OK) {
NSLog(#"SQLITE_OK");
if (sqlite3_step(statement) == SQLITE_ROW) {
int QuestionNumberx = sqlite3_column_int(statement, 0);
int TotalRate = sqlite3_column_int(statement, 1);
NSLog(#"QuestionNumberx %d",QuestionNumberx );
NSLog(#"TotalRate %d",TotalRate );
[retArray addObject:[NSNumber numberWithInt:QuestionNumberx]];
[retArray addObject:[NSNumber numberWithInt:TotalRate]];
NSLog(#"retArray %#",retArray );
}
sqlite3_finalize(statement);
} else {
NSLog(#"Unable to prepare statement: %s db err '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(_contactDB), sqlite3_errcode(_contactDB));
}
sqlite3_close(_contactDB);
} else {
NSLog(#"Unable top open database.");
}
}else{
NSLog(#"database is not exist.");
}
}

Related

Image not retrieving from sql database in objective c

Hello I have been trying to retrieve image from sql database named informationdb.sql but I am not able to,could anyone help with that.Here is my code
- (NSData*) LoadImagesFromSql
{
NSData* data = nil;
directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [directoryPaths objectAtIndex:0];
NSString * databasePath = [[NSString alloc]initWithString:[documentsDirectory stringByAppendingPathComponent:#"informationdb.sql"]];
int openDatabaseResult = sqlite3_open_v2([databasePath UTF8String], &sqlite3DatabaseObject, SQLITE_OPEN_READWRITE , NULL);
if (openDatabaseResult == SQLITE_OK) {
const char* sqlite3Query = "SELECT PHOTO FROM PICTURES";
int sqlite3Prepare = sqlite3_prepare_v2(sqlite3DatabaseObject, sqlite3Query, -1, &sqlite3Statement, NULL);
if( sqlite3Prepare == SQLITE_OK )
{
const char *myData = sqlite3_column_blob(sqlite3Statement, 1);
int myDataSize = sqlite3_column_bytes(sqlite3Statement, 1);
data = [NSData dataWithBytes:myData length:myDataSize];
sqlite3_step(sqlite3Statement);
}
sqlite3_finalize(sqlite3Statement);
}
sqlite3_close(sqlite3DatabaseObject);
return data;
}
Thank you.
first delete database in document directory than change all three method to my code
-(void)database
{
directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [directoryPaths objectAtIndex:0];
NSString * databasePath = [[NSString alloc]initWithString:[documentsDirectory stringByAppendingPathComponent:#"informationdb.sql"]];
NSLog(#"database path=%#",databasePath);
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:databasePath] == NO) {
const char *dbPath = [databasePath UTF8String];
if (sqlite3_open(dbPath, &sqlite3DatabaseObject)== SQLITE_OK) {
char * errorMessage;
const char *sqlite3Query = "CREATE TABLE IF NOT EXISTS PICTURES (PHOTO BLOB,Id integer PRIMARY KEY)";
if (sqlite3_exec(sqlite3DatabaseObject, sqlite3Query, NULL, NULL, &errorMessage)!= SQLITE_OK) {
NSLog(#"failed = %#",sqlite3DatabaseObject);
}
sqlite3_close(sqlite3DatabaseObject);
}else{
NSLog(#"failed to create database");
}
}
}
- (void) SaveImagesToSql: (NSData*) imgData {
directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [directoryPaths objectAtIndex:0];
NSString * databasePath = [[NSString alloc]initWithString:[documentsDirectory stringByAppendingPathComponent:#"informationdb.sql"]];
NSLog(#"database path=%#",databasePath);
const char* sqlite3Query = "INSERT INTO PICTURES (PHOTO) VALUES (?)";
int openDatabaseResult = sqlite3_open_v2([databasePath UTF8String], &sqlite3DatabaseObject, SQLITE_OPEN_READWRITE , NULL);
if (openDatabaseResult == SQLITE_OK) {
int sqlite3Prepare = sqlite3_prepare_v2(sqlite3DatabaseObject, sqlite3Query, -1, &sqlite3Statement, NULL);
if( sqlite3Prepare == SQLITE_OK ) {
sqlite3_bind_blob(sqlite3Statement, 1, [imgData bytes], [imgData length], SQLITE_TRANSIENT);
sqlite3_step(sqlite3Statement);
}
else
{
NSLog(#"Error is: %s", sqlite3_errmsg(sqlite3DatabaseObject));
}
sqlite3_finalize(sqlite3Statement);
}
else NSLog( #"Error is: %s", sqlite3_errmsg(sqlite3DatabaseObject) );
sqlite3_close(sqlite3DatabaseObject);
}
- (NSData*)LoadImagesFromSql
{
NSData* data = nil;
directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [directoryPaths objectAtIndex:0];
NSString * databasePath = [[NSString alloc]initWithString:[documentsDirectory stringByAppendingPathComponent:#"informationdb.sql"]];
sqlite3_stmt *compiledStmt;
sqlite3 *db;
int i = 1;
if(sqlite3_open([databasePath UTF8String], &db)==SQLITE_OK){
NSString *insertSQL = [NSString stringWithFormat:#"SELECT PHOTO FROM PICTURES WHERE Id = %d",i];
if(sqlite3_prepare_v2(db,[insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &compiledStmt, NULL) == SQLITE_OK) {
while(sqlite3_step(compiledStmt) == SQLITE_ROW) {
int length = sqlite3_column_bytes(compiledStmt, 0);
data = [NSData dataWithBytes:sqlite3_column_blob(compiledStmt, 0) length:length];
NSLog(#"Length : %lu", (unsigned long)[data length]);
// if(imageData == nil)
// NSLog(#"No image found.");
// else
// imgView.image = [UIImage imageWithData:imageData];
}
}
sqlite3_finalize(compiledStmt);
}
sqlite3_close(db);
return data;
}
solved it by this:
- (NSData*) LoadImagesFromSql
{
directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [directoryPaths objectAtIndex:0];
NSString * databasePath = [[NSString alloc]initWithString:[documentsDirectory stringByAppendingPathComponent:#"informationdb.sql"]];
NSData* data = nil;
// int i=0;
NSString* sqlite3Query = [NSString stringWithFormat:#"SELECT PHOTO FROM PICTURES "];
int openDatabaseResult = sqlite3_open_v2([databasePath UTF8String], &sqlite3DatabaseObject, SQLITE_OPEN_READWRITE , NULL);
if (openDatabaseResult == SQLITE_OK)
{
int sqlite3Prepare = sqlite3_prepare_v2(sqlite3DatabaseObject, [sqlite3Query UTF8String], -1, &sqlite3Statement, NULL);
if( sqlite3Prepare == SQLITE_OK )
{
while (sqlite3_step(sqlite3Statement) == SQLITE_ROW) {
const char *myData = sqlite3_column_blob(sqlite3Statement, 0);
int myDataSize = sqlite3_column_bytes(sqlite3Statement, 0);
data = [NSData dataWithBytes:myData length:myDataSize];
}
sqlite3_finalize(sqlite3Statement);
}
}
sqlite3_close(sqlite3DatabaseObject);
return data;
}
and updated viewWillAppear as :
-(void)viewWillAppear:(BOOL)animated{
dbmClass = [[DBManager alloc]init];
if (imgPic.image != nil) {
imgPic.image = nil;
}
imgPic.image = [UIImage imageWithData:[dbmClass LoadImagesFromSql]];
NSLog(#"%#",[dbmClass LoadImagesFromSql]);
}

Adding record to table in SQLIte file won't work

I'm trying to add record to SQLite file but nothing happens. I get no error and in console I get that record is inserted in table #"----> RECORD INSERTED". Below you can see the code that I'm using. Please, can someone tell me how to fix this. Thanks for your time and help.
- (void)addRecordToSQLiteDatabase
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appDBPath = [documentsDirectory stringByAppendingPathComponent:#"ProductiivData.sqlite"];
success = [fileManager fileExistsAtPath:appDBPath];
if (success)
{
const char *databaseCharPath = [appDBPath UTF8String];
if (sqlite3_open(databaseCharPath, &(database)) == SQLITE_OK)
{
char *errorInsert;
const char *insertRecordInPROJECT = "insert into PROJECT values (139, 'TEST1888799')";
if (sqlite3_exec(database, insertRecordInPROJECT, NULL, NULL, &errorInsert))
{
NSLog(#"----> RECORD INSERTED");
}
else
{
NSLog(#"----> RECORD NOT INSERTED -- Error: %s", errorInsert);
}
char *errorSelectAll;
sqlite3_stmt *statement = NULL;
const char *allRecordsFromPROJECT = "select * from PROJECT";
if (sqlite3_prepare_v2(database, allRecordsFromPROJECT, 1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *projectName = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(statement, 2)];
}
}
else
{
NSLog(#"----> RECORDS NOT SELECTED -- Error: %s", errorSelectAll);
}
}
}
}
Separate insert and select logic, Try following code
Get Database path
+ (NSString *)getDBPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:#"dbName.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success)
{
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"dbName.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, #"Failed to create writable database file with message '%#'.", [error localizedDescription]);
}
return dbPath;
}
Insert Data
+ (void)insertData
{
const char *dbpath = [[self getDBPath] UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *strSQL = [NSString stringWithFormat:#"INSERT INTO tableName………………."];
const char *insertQuery = [strSQL UTF8String];
sqlite3_stmt *insert_stmt;
if (sqlite3_prepare_v2(database, insertQuery, -1, &insert_stmt, NULL) == SQLITE_OK)
{
if (sqlite3_step(insert_stmt) == SQLITE_DONE)
{
// Code
}
else
{
NSLog(#"error");
}
}
sqlite3_finalize(insert_stmt);
sqlite3_close(database);
}
else
{
NSLog(#"Error in opening database.");
}
}
Get data from table
+ (NSArray *)getDataList
{
NSMutableArray *arrProducts = [[NSMutableArray alloc] init];
const char *dbpath = [[self getDBPath] UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *strSQL = [NSString stringWithFormat:#"SELECT * FROM tableName"];
const char *selectQuery = [strSQL UTF8String];
sqlite3_stmt *select_stmt;
if(sqlite3_prepare_v2(database, selectQuery, -1, &select_stmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(select_stmt) == SQLITE_ROW)
{
NSInteger productID = sqlite3_column_int(select_stmt, 0);
NSString productNo = [NSString stringWithUTF8String:(char *)sqlite3_column_text(select_stmt, 1)];
// Store above data into dictionary or object and then add this to array
[arrProducts addObject:dict];
}
}
}
else
{
NSLog(#"Error in opening database.");
}
return arrProducts;
}

select data record in table sqlite ios return with only one object

i have an problem when i select usernames the return is only one object and the table contain all of usernames how to solve this i try to select by database manager and i find that query sql statment is right
- (NSMutableArray*) FindAccounts
{
///database
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: #"TRIAL.sqlite"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:#"SELECT USERNAME,TYPE FROM CONTNEW"];
const char *query_stmt = [querySQL UTF8String];
NSMutableArray *resultArray = [[NSMutableArray alloc]init];
if (sqlite3_prepare_v2(contactDB,query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *USERS = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
[resultArray addObject:USERS];
return resultArray;
}
else{
NSLog(#"Not found");
return nil;
}
sqlite3_reset(statement);
}
}
return nil;
}
OK, try this (note that there is not much error reporting):
- (NSMutableArray*) FindAccounts
{
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *databasePath = [docsDir stringByAppendingPathComponent:#"TRIAL.sqlite"];
sqlite3_stmt *statement;
// I assume contactDB is an instance variable?
if (!contactDB &&
sqlite3_open([databasePath UTF8String], &contactDB) != SQLITE_OK) {
NSLog(#"Failed to open database");
return nil;
}
NSString *querySQL = #"SELECT USERNAME,TYPE FROM CONTNEW ORDER BY USERNAME";
NSMutableArray *resultArray = [NSMutableArray new];
if (sqlite3_prepare_v2(contactDB, [querySQL UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *username = #(sqlite3_column_text(statement, 0));
[resultArray addObject:username];
}
sqlite3_reset(statement);
}
return resultArray;
}

sqlite_prepare_v2 not working and not return sqlite_ok

NSArray *documentpath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* path = [documentpath objectAtIndex:0];
NSString *dbpath = [path stringByAppendingString:#"check1.sqlite"];
if (sqlite3_open([dbpath UTF8String], &db) == SQLITE_OK) {
NSLog(#"get database");
const char *sql = "select name from login where ID = 1";
sqlite3_stmt *statement;
int sqlresult = sqlite3_prepare_v2(db, sql, -1, &statement, NULL);
if ( sqlresult == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1) ];
NSLog(#"%#",name);
}
}
}

Create Sqlite database in iOS

I am unable to create sqlite database in my documents directory.
Here is the code:
NSString *fileDir;
NSArray *dirPaths;
//Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDemoApplicationDirectory, NSUserDomainMask, YES);
fileDir = [dirPaths objectAtIndex:0];
// Build the database path
databasePath = [[NSString alloc]initWithString:[fileDir stringByAppendingPathComponent:#"student.sql"]];
NSFileManager *fileMgr = [NSFileManager defaultManager];
if([fileMgr fileExistsAtPath:databasePath] == NO)
{
const char *dbPath = [databasePath UTF8String];
if (sqlite3_open(dbPath, &database) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS(ID INTEGER PRIMARY KEY , NAME TEXT, ADDRESS TEXT, MOBILE INTEGER)";
if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK) {
_status.text = #"Failed to create table";
}
sqlite3_close(database);
}
else
{
_status.text = #"Failed to open/create database";
}
}
I have debug the code and found that the compiler is not going under this condition.
sqlite3_open(dbPath, &database) == SQLITE_OK
I don't know what i am doing wrong.
Any help will be appreciated...
Thanks,
Check you code to get dirPath, you are getting right path or not, I used following way in my code and its working for me :
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// dirPaths = NSSearchPathForDirectoriesInDomains(NSDemoApplicationDirectory, NSUserDomainMask,YES);
fileDir = dirPaths[0];
// Build the database path
databasePath = [[NSString alloc]initWithString:[fileDir stringByAppendingPathComponent:#"student.sqlite"]];
this is how i managed it.
DataBaseAccess.m
static sqlite3 *database=nil;
-(id)init
{
if(self=[super init])
{
self.user_data=#"user_data.db";
}
return self;
}
-(void)createUserDataDatabase
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:user_data];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
// construct database from external ud.sql
NSString *filePath=[[NSBundle mainBundle]pathForResource:#"ud" ofType:#"sql"];
NSString *sqlStatement=[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if(sqlite3_open([writableDBPath UTF8String], &database)==SQLITE_OK)
{
sqlite3_exec(database, [sqlStatement UTF8String], NULL, NULL, NULL);
sqlite3_close(database);
}
}
Your external sql file must contain the sql queries:
CREATE TABLE quantityInSubCountries (
refID INT,
quantity INT
);
CREATE TABLE quantityInSubRegions (
refID INT,
quantity INT
); ....
Hope it will help.
This is common methods used for Database
-(void)updateTable:(NSString *)tableName setname:(NSString *)Name setImagePath:(NSString *)imagePath whereID:(NSInteger)rid{
NSString *sqlString=[NSString stringWithFormat:#"update %# set name='%#' where id=%ld",tableName,Name,rid];
char *error;
if (sqlite3_exec(db, [sqlString UTF8String], NULL, NULL, &error)!=SQLITE_OK) {
[self closeDatabase];
NSLog(#"Faield to update");
}
else{
NSLog(#"update successfully");
}
}
-(void)deleteFrom:(NSString *)tablename whereName:(NSInteger )rid {
NSString *sqlString=[NSString stringWithFormat:#"delete from %# where id=%ld",tablename,(long)rid];
char *error;
if (sqlite3_exec(db, [sqlString UTF8String], NULL, NULL, &error)!=SQLITE_OK) {
[self closeDatabase];
NSLog(#"faield to Delete");
}
else{
NSLog(#"Deleted successfully");
}
}
-(void)insertInTable:(NSString *)tableName withName:(NSString *)name withImagePath:(NSString *)imagePath
{
NSString *sqlString=[NSString stringWithFormat:#"insert into %#(name,path)values('%#','%#')",tableName,name,imagePath];
char *error;
if (sqlite3_exec(db, [sqlString UTF8String], NULL, NULL, &error)!=SQLITE_OK) {
[self closeDatabase];
NSLog(#"Failed to insert");
}
else{
NSLog(#"Inserted succesfully");
}
}
-(NSString *)path
{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir=[paths objectAtIndex:0];
return [documentDir stringByAppendingPathComponent:#"Storage.db"];
}
-(void)open{
if(sqlite3_open([[self path] UTF8String], &db)!=SQLITE_OK)
{
sqlite3_close(db);
NSLog(#"your database table has been crash");
}
else{
NSLog(#"Database open successfully");
}
}
-(void)closeDatabase
{
sqlite3_close(db);
NSLog(#"Database closed");
}
-(void)copyFileToDocumentPath:(NSString *)fileName withExtension:(NSString *)ext{
NSString *filePath=[self path];
NSFileManager *fileManager=[NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:filePath]) {
NSString *pathToFileInBundle=[[NSBundle mainBundle] pathForResource:fileName ofType:ext];
NSError *err=nil;
BOOL suc=[fileManager copyItemAtPath:pathToFileInBundle toPath:filePath error:&err];
if (suc) {
NSLog(#"file copied successfully");
}
else
{
NSLog(#"faield to copied");
}
}
else
{
NSLog(#"File allready present");
}
}
-(NSMutableArray *)AllRowFromTableName:(NSString *)tableName{
NSMutableArray *array=[[NSMutableArray alloc] init];
NSString *sqlString=[NSString stringWithFormat:#"select *from %#",tableName];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(db, [sqlString UTF8String], -1, &statement, nil)==SQLITE_OK) {
while (sqlite3_step(statement)==SQLITE_ROW) {
Database *tempDatabase=[[Database alloc] init];
tempDatabase.Hid=sqlite3_column_int(statement, 0);
tempDatabase.Hname =[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
tempDatabase.Hpath=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
[array addObject:tempDatabase];
}
}
return array;
}
-(void)test
{
//Pet photos
NSString *sqlString=[NSString stringWithFormat:#"create table if not exists StorageTable(id integer primary key autoincrement,name text,path text)"];
char *error;
if (sqlite3_exec(db, [sqlString UTF8String], NULL, NULL, &error)!=SQLITE_OK) {
[self closeDatabase];
NSLog(#"Faield to blanck 1 %s",error);
}
else{
NSLog(#"Test On StorageTable Database successfully");
}
}

Resources