no such table: tablename in sqlite even after initialization - ios

i am new to ios dev and i am having the following problem:
i initialized the tables i needed, and when i try to execute a query to any of the tables i get the "no such table" error, here is the steps and code:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Function called to create a copy of the database if needed.
[[SQLiteUtilities sharedSQLiteManager] initializationDatabase];
return YES;
}
and here is the implementation:
-(void)initializationDatabase {
NSString *path = [self databaseFilePath];
BOOL isExists = [[NSFileManager defaultManager] fileExistsAtPath:path];
if (isExists) return;
FMDatabase *db = [FMDatabase databaseWithPath:path];
if ([db open] == NO) {
return;
}
NSString *sql =
#"CREATE TABLE albums("
"albumid INTEGER PRIMARY KEY AUTOINCREMENT,"
"directory CHAR(20) NOT NULL,"
"albumname CHAR(32) NOT NULL,"
"count INT NOT NULL,"
"orderid INT NOT NULL"
");"
"CREATE TABLE photos("
"photoid INTEGER PRIMARY KEY AUTOINCREMENT,"
"albumid INTEGER NOT NULL DEFAULT 0,"
"filename CHAR(50) NOT NULL DEFAULT \"\","
"originalname CHAR(50) DEFAULT \"\","
"addtime INTEGER NOT NULL DEFAULT 0,"
"createtime INTEGER NOT NULL DEFAULT 0,"
"filesize INTEGER NOT NULL DEFAULT 0"
");";
//[db executeQuery:sql1];
[db executeQuery:sql];
[db close];
}
and when i try to add anything as follows i get the "no such table:albums" error:
- (AlbumsUtilities *)createAlbumWithName:(NSString *)name {
FMDatabase *db = [FMDatabase databaseWithPath:[self databaseFilePath]];
if (![db open]) return nil;
NSString *directory = createRandomAlbumDirectory();
int maxid = 0;
BOOL success = [db executeUpdate:#"INSERT INTO albums(directory,albumname,count,orderid) VALUES(?,?,0,?)", directory,name,#(maxid)];
AlbumsUtilities *album = nil;
if (success) {
....(not getting here of course)
}
[db close];
return album;
}
any help would be appreciated, thanks.

Try this two function for initialize db .
ADD Blank File .sqlite extension
- (void)createCopyOfDatabaseIfNeeded {
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSLog(#"%#",[self getDBPath]);
success = [fileManager fileExistsAtPath:[self getDBPath]];
if (success){
return;
}
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"db.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:[self getDBPath] error:&error];
if (!success) {
NSAssert1(0, #"Failed to create writable database file with message '%#'.", [error localizedDescription]);
}
}
-(NSString *)getDBPath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *dbPath = [docsPath stringByAppendingPathComponent:#"db.sqlite"];
return dbPath;
}
After Initialize create table
-(void)createTable{
FMDatabase *database = [FMDatabase databaseWithPath:[self getDBPath]];
[database open];
[database executeUpdate:"your query"];
[database close];
}
Calling
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Function called to create a copy of the database if needed.
[[SQLiteUtilities sharedSQLiteManager] createCopyOfDatabaseIfNeeded];
[[SQLiteUtilities sharedSQLiteManager] createTable];
}

#Try this:
Bool isSuccess = YES;
if (sqlite3_open(path, &db) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS contactTable (albumid integer primary key autoincrement ,directory CHAR(20) NOT NULL, albumname CHAR(32) NOT NULL, albumname CHAR(32) NOT NULL, count INT NOT NULL, orderid INT NOT NULL);";
if (sqlite3_exec(db, sql_stmt, NULL, NULL, &errMsg)
!= SQLITE_OK)
{
sqlite3_close(db);
isSuccess = NO;
}
sqlite3_close(db);
return isSuccess;
}
else {
sqlite3_close(db);
isSuccess = NO;
}
}
sqlite3_close(db);
NSLog(#"Albums Table Created");
return isSuccess;

Related

SQlite insert data in iOS App

I'm following some tutorials from youtube for sqlite crud operations. I have textfields with name, contact and address , when i hit save button it show message in console that data is saved but when i look at database it nothing appears there. My database path is correct and no errors in code but 'm confused why it isn't going. My path code is this,
-(void)copyandpaste{
NSArray *arr1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *str1 = [arr1 objectAtIndex:0];
strpath = [str1 stringByAppendingPathComponent:#"personinfo.sqlite"];
if (![[NSFileManager defaultManager]fileExistsAtPath:strpath]) {
NSString *local = [[NSBundle mainBundle]pathForResource:#"personinfo" ofType:#"sqlite"];
[[NSFileManager defaultManager]copyItemAtPath:local toPath:strpath error:nil];
}
NSLog(#"%#",strpath);
}
this method is called in appdelegate.m file,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self copyandpaste];
return YES;
}
I have mad NSObject class for database connection. the connection code is this,
-(id)init{
appdel = (AppDelegate *)[[UIApplication sharedApplication]delegate]; //get all methods in AppDelegate
strmain = appdel.strpath;
return self;
}
-(NSMutableArray *)getalluser:(NSString *)query{
arrdata = [[NSMutableArray alloc]init];
if (sqlite3_open([strmain UTF8String], &(database))==SQLITE_OK) {
sqlite3_stmt *connection;
if (sqlite3_prepare_v2(database, [query UTF8String], -1, &connection, nil)==SQLITE_OK) {
while (sqlite3_step(connection)==SQLITE_ROW) {
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
NSString *str12 = [[NSString alloc]initWithUTF8String:(char *)sqlite3_column_text(connection, 0)]; //name in first column
NSLog(#"Checking");
NSString *str13 = [[NSString alloc]initWithUTF8String:(char *)sqlite3_column_text(connection, 1)]; //contact in second column
NSString *str14 = [[NSString alloc]initWithUTF8String:(char *)sqlite3_column_text(connection, 2)]; //address in third column
[dic setObject:str12 forKey:#"name"];
[dic setObject:str13 forKey:#"contact"];
[dic setObject:str14 forKey:#"address"];
[arrdata addObject:dic];
}
}
sqlite3_finalize(connection);
}
sqlite3_close(database);
return arrdata;
}
My save button code with query is this,
- (IBAction)btnSave:(id)sender {
NSString *saveDATA=[[NSString alloc]initWithFormat:#"insert into stuInfo values('%#','%#','%#')",txtName,txtContact,txtAddress];
dboperations *db = [[dboperations alloc ]init];
BOOL ds = [db getalluser:saveDATA];
if (ds) {
NSLog(#"Data Saved in database");
}else{
NSLog(#"Data is not Saved in database");
}
}
- (NSString*) saveData:(NSString *)name cont:(NSString *)contacts add:(NSString *)address
{
const char *dbpath = [YOUR_DATA_BASE_PATH UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSLog(#"=============saveData open==============");
const char * query="insert into YOUR_TABLE_NAME(name,contacts,address) values(?,?,?);";
sqlite3_stmt *inset_statement;
char *errMsg;
if (sqlite3_open(dbpath, &database)!=SQLITE_OK) {
NSLog(#"Error to Open");
return nil;
}
if (sqlite3_prepare_v2(database, query , -1,&inset_statement, NULL) != SQLITE_OK )
{
NSLog(#"%s Prepare failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(database), sqlite3_errcode(database));
NSLog(#"Error to Prepare");
return nil;
}
//No of data to insert
sqlite3_bind_text(inset_statement,1,[name UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(inset_statement,2,[contacts UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(inset_statement,3,[address UTF8String], -1, SQLITE_TRANSIENT);
if(sqlite3_step(inset_statement)== SQLITE_DONE)
{
long long lastRowId = sqlite3_last_insert_rowid(database);
NSString *rowId = [NSString stringWithFormat:#"%d", (int)lastRowId];
NSLog(#"Row ID of Last Inserted row %#",rowId);
sqlite3_finalize(inset_statement);
sqlite3_close(database);
NSLog(#"=============saveData Close==============");
return rowId;
}
if (sqlite3_exec(database, query, NULL, NULL, &errMsg)
!= SQLITE_OK)
{
NSLog(#"Failed to Insert msg in message table Error = %s",errMsg);
sqlite3_finalize(inset_statement);
sqlite3_close(database);
NSLog(#"=============saveData Close==============");
return nil;
}
else
{
sqlite3_finalize(inset_statement);
}
}
NSLog(#"=============saveData Close==============");
sqlite3_close(database);
return nil;
}
You are not handling if there are any errors in the SQLite statements you are preparing.
The function will always return true because arrdata is never null
You need to add anelse statements to your
if (sqlite3_prepare_v2(database, [query UTF8String], -1, &connection, nil)==SQLITE_OK)
to show any errors...
NSLog(#"%s",sqlite3_errmsg(database));

cannot create database table and insert in iOS sqlite3

I am new in iOS development. I am trying to do a simple todo list app. I am using xocde 8 and objective-C language . I tried several tutorial but could not create the database table. Here is my code.
-(void)createOrOpenDB{
printf("createOrOpenDB: into this function \n");
NSArray *docsDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dirPaths = [docsDir objectAtIndex:0];
dbPathString = [[NSString alloc] initWithString: [dirPaths stringByAppendingPathComponent:#"person.db"]];
NSFileManager *fileManager = [NSFileManager defaultManager];
char *err;
printf("createOrOpenDB: into this function before if statement \n");
if(![fileManager fileExistsAtPath:dbPathString]){
const char *dbPath = [dbPathString UTF8String];
printf("createOrOpenDB: into this functions 1st if statement \n");
//create db here
if(sqlite3_open(dbPath, &personDB) ==SQLITE_OK){
const char *sql_stnt = "CREATE TABLE IF NOT EXISTS PERSONS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, AGE INTEGER)";
if(sqlite3_exec(personDB, sql_stnt, NULL, NULL, &err) !=SQLITE_OK){
printf("Failed to create table\n");
}
sqlite3_close(personDB);
printf("createOrOpenDB: database table created\n");
}
}
}
when i press the add button , its not giving me any error. but its not adding any data. NSLog in not working in xcode 8 . so i did printf instead . and my code breaks before the if statement . can anybody tell me what i am doing wrong?
do like this,
+(DBManager*)getSharedInstance{
if (!sharedInstance) {
sharedInstance = [[super allocWithZone:NULL]init];
[sharedInstance createDB];
}
return sharedInstance;
}
-(BOOL)createDB{
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: #"student.db"]];
BOOL isSuccess = YES;
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == YES)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "create table if not exists studentsDetail (regno integer primary key, name text, department text, year text)";
if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg)
!= SQLITE_OK)
{
isSuccess = NO;
NSLog(#"Failed to create table");
}
else
{
NSLog(#"Table Created Successfully");
}
sqlite3_close(database);
return isSuccess;
}
else {
isSuccess = NO;
NSLog(#"Failed to open/create database");
}
sqlite3_finalize(statement);
}
return isSuccess;
// NSLog(#"database %#", isSuccess);
}
For saving the data in this table,
- (BOOL) saveData:(NSString*)registerNumber name:(NSString*)name
department:(NSString*)department year:(NSString*)year;
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:#"insert into studentsDetail (regno,name, department, year) values (\"%ld\",\"%#\", \"%#\", \"%#\")",(long)[registerNumber integerValue], name, department, year];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
return YES;
}
else {
return NO;
}
sqlite3_reset(statement);
}
return NO;
}
It will create table every time when table does not exist.
Hope t will help you.

sqlite3 prepare statement not working, possibly improper pointers iOS

I have setup a GlobalVars class to hold my sqlite3 database variable.
static sqlite3** database;
const char *dbPath;
#implementation GlobalVars : NSObject
+(GlobalVars*)sharedInstance {
static GlobalVars *myInstance = nil;
if(myInstance == nil) {
myInstance = [[[self class] alloc] init];
}
return myInstance;
}
+(sqlite3*)getGlobalDatabase {
return &database;
}
+(void)setGlobalDatabase:(sqlite3*)_database {
database = &_database;
}
Then in the header file I have
static sqlite3** database;
which is above the interface. This is how I setup my database variable.
I am then trying to access it when i open the database and prepare it. I can open it, because the open call returns true. I can not prepare it properly, because the statement returns false and it doesn't go into the if statement. I am wondering if I have messed up my pointers, because the prepare statement isn't working, and it doesn't prepare it properly.
-(void)setAllValues:(NSMutableArray*)array {
if(sqlite3_open([GlobalVars getGlobalDBPath], [GlobalVars getGlobalDatabase]) == SQLITE_OK) {
sqlite3_stmt *insertStatement;
NSString *sqlInsert = [NSString stringWithFormat:#"insert into my_table ('_id', 'name', 'age', 'weight', 'height', 'description') VALUES (%i, '%#', '%i', '%i', '%#', '%#')", ID, name, age, weight, height, description];
//*********** This is not opening, and SQLITE_OK is equal to false ***********
if(sqlite3_prepare_v2(([GlobalVars getGlobalDatabase]), [sqlInsert UTF8String], -1, &insertStatement, nil) == SQLITE_OK) {
if(sqlite3_step(insertStatement) == SQLITE_DONE) {
NSLog(#"insert stepping done");
}
sqlite3_reset(insertStatement);
}
sqlite3_finalize(insertStatement);
sqlite3_close([GlobalVars getGlobalDatabase]);
}
}
The variables for the database are all filled with their correct data, and it seems to open the database without issues. When it comes to preparing, it does not work properly and returns false. Any ideas why. Thank you for your assistance, any help is appreciated.
**
You must try this one ... Maybe its help you :
**
#import "DBManager.h"
#import "userRegistrationClass.h"
static DBManager *sharedInstance = nil;
static sqlite3 *database = nil;
static sqlite3_stmt *statement = nil;
#implementation DBManager
#pragma mark
#pragma mark Get shared Function
+(DBManager*)getSharedInstance{
if (!sharedInstance) {
sharedInstance = [[super allocWithZone:NULL]init];
[sharedInstance createDB];
}
return sharedInstance;
}
#pragma mark
#pragma mark Create DataBase
-(BOOL)createDB{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
// http://www.mycashkit.com/my-earnings.php
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(#"Dir Path Value is %#",dirPaths);
docsDir = [dirPaths objectAtIndex:0];
NSLog(#"DocsDir Path Value is %#",docsDir);
// Build the path to the database file
databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent: #"UserRegInfo.sqlite"]];
NSLog(#"Data base Work's %#:",databasePath);
BOOL isSuccess = YES;
NSFileManager *filemgr = [NSFileManager defaultManager];
// NSString *currentPath = [filemgr currentDirectoryPath];
NSLog(#"My file managaer value is %#",filemgr);
//NSLog(#"My current drictory path is %#",currentPath);
//the file will not be there when we load the application for the first time
//so this will create the database table
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
NSLog(#"Constan charcter value is %s:-",dbpath);
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "create table if not exists UserInfo(UserID integer primary key, UserName,Gender,UserEmailID,Password,RePassword,DOB, MobileNo,IsUserType)";
const char *sql_stmt2 = "create table if not exists TestInfo(TestID integer primary key, TestName,TestType)";
// NSString *dbPathFromApp=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"UserRegInfo.sqlite"];[filemgr copyItemAtPath:dbPathFromApp toPath:databasePath error:nil];
NSLog(#"Constan charcter value is %s:-",dbpath);
if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg) && (sqlite3_exec(database, sql_stmt2, NULL, NULL, &errMsg)!= SQLITE_OK))
{
isSuccess = NO;
NSLog(#"Failed to create table");
}
NSLog(#"Print Sqlite%d",(sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg)));
sqlite3_close(database);
return isSuccess;
}
else {
isSuccess = NO;
NSLog(#"Failed to open/create database");
}
}
return isSuccess;
}
#pragma mark
#pragma mark Save Data Function
-(BOOL)saveData:(NSString*)insertSQL{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSLog(#" my Sqlite Query is :- %#",insertSQL);
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
return YES;
}
else
{
NSLog(#"Squlite Error Msg is %s",sqlite3_errmsg(database));
return NO;
}
}
return NO;
}

Use and Access Existing SQLite Database on iOS

I have a fully populated database in SQLite that I'd like to use in my new app. It's rather large, so I'd like to avoid changing it to another format if possible. How can I use this database in such a way that it ships with my app?
EDIT: If I just drop the file into my Supported Files directory, for example, how can I access it? How do I reference it?
SQLite database interaction can be made simple and clean by using FMDB Framework. FMDB is an Objective-C wrapper for the SQLite C interface.
Reference worth reading:
FMDB Framework Docs
Sample Project With Storyboard
Initial Setup
Add the SQLite DB like any other file in your application's bundle then copy the database to documents directory using the following code then use the database from the documents directory
First download the FMDB framework
Extract the framework now copy all the file from src/fmdb folder (not the src/sample or src/extra folders).
Click your project in the left column of Xcode.
Click the main target in the middle column.
Click the “Build Phases” tab.
Expand the arrow next to “Link Binary With Libraries”.
Click the “+” button.
Search for libsqlite3.0.dylib and double click it.
Copying your existing database into app's document in didFinishLaunchingWithOptions: and maintain the database path through out the application.
In your AppDelegate add the following code.
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
// Application Start
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Function called to create a copy of the database if needed.
[self createCopyOfDatabaseIfNeeded];
return YES;
}
#pragma mark - Defined Functions
// Function to Create a writable copy of the bundled default database in the application Documents directory.
- (void)createCopyOfDatabaseIfNeeded {
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// Database filename can have extension db/sqlite.
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appDBPath = [documentsDirectory stringByAppendingPathComponent:#"database-name.sqlite"];
success = [fileManager fileExistsAtPath:appDBPath];
if (success) {
return;
}
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"database-name.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:appDBPath error:&error];
NSAssert(success, #"Failed to create writable database file with message '%#'.", [error localizedDescription]);
}
YourViewController.m
Select Query
#import "FMDatabase.h"
- (void)getAllData {
// Getting the database path.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *dbPath = [docsPath stringByAppendingPathComponent:#"database-name.sqlite"];
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];
NSString *sqlSelectQuery = #"SELECT * FROM tablename";
// Query result
FMResultSet *resultsWithNameLocation = [database executeQuery:sqlSelectQuery];
while([resultsWithNameLocation next]) {
NSString *strID = [NSString stringWithFormat:#"%d",[resultsWithNameLocation intForColumn:#"ID"]];
NSString *strName = [NSString stringWithFormat:#"%#",[resultsWithNameLocation stringForColumn:#"Name"]];
NSString *strLoc = [NSString stringWithFormat:#"%#",[resultsWithNameLocation stringForColumn:#"Location"]];
// loading your data into the array, dictionaries.
NSLog(#"ID = %d, Name = %#, Location = %#",strID, strName, strLoc);
}
[database close];
}
Insert Query
#import "FMDatabase.h"
- (void)insertData {
// Getting the database path.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *dbPath = [docsPath stringByAppendingPathComponent:#"database-name.sqlite"];
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];
NSString *insertQuery = [NSString stringWithFormat:#"INSERT INTO user VALUES ('%#', %d)", #"Jobin Kurian", 25];
[database executeUpdate:insertQuery];
[database close];
}
Update Query
- (void)updateDate {
// Getting the database path.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *dbPath = [docsPath stringByAppendingPathComponent:#"fmdb-sample.sqlite"];
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];
NSString *insertQuery = [NSString stringWithFormat:#"UPDATE users SET age = '%#' WHERE username = '%#'", #"23", #"colin" ];
[database executeUpdate:insertQuery];
[database close];
}
Delete Query
#import "FMDatabase.h"
- (void)deleteData {
// Getting the database path.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *dbPath = [docsPath stringByAppendingPathComponent:#"database-name.sqlite"];
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];
NSString *deleteQuery = #"DELETE FROM user WHERE age = 25";
[database executeUpdate:deleteQuery];
[database close];
}
Addition Functionality
Getting the row count
Make sure to include the FMDatabaseAdditions.h file to use intForQuery:.
#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"
- (void)gettingRowCount {
// Getting the database path.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *dbPath = [docsPath stringByAppendingPathComponent:#"database-name.sqlite"];
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];
NSUInteger count = [database intForQuery:#"SELECT COUNT(field_name) FROM table_name"];
[database close];
}
Add the Sqlite DB like any other file in your application bundle
Copy it to documents directory via code and use it .The purpose of this is that updating content in sqlite is possible in Documents directory only
-(void) checkAndCreateDatabase
{
// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:_databasePath];
// If the database already exists then return without doing anything
if(success) return;
// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:_databaseName];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:_databasePath error:nil];
}
- (id)init {
if ((self = [super init]))
{
_databaseName = DB_NAME;
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
_databasePath = [documentsDir stringByAppendingPathComponent:_databaseName];
if (sqlite3_open([[self dbPath] UTF8String], &_database) != SQLITE_OK)
{
[[[UIAlertView alloc]initWithTitle:#"Missing"
message:#"Database file not found"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil]show];
}
}
return self;
}
This following methods will help you to handle database
Method for copy database in document directory if not exist
-(void)copyDatabase
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *insPath = [NSString stringWithFormat:#"Instamontage.sqlite"];
destPath = [documentsDirectory stringByAppendingPathComponent:insPath];
NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:insPath];
// NSLog(#"\n src %# \n dest %#", srcPath, destPath);
if (![[NSFileManager defaultManager] fileExistsAtPath:destPath])
{
NSError *error;
NSLog(#"not exist");
[[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:destPath error:&error];
}
else
{
NSLog(#"exist");
}
}
Method for insert/deleting/updating table
-(BOOL)dataManipulation: (NSString *)query
{
BOOL result=NO;
if (sqlite3_open([destPath UTF8String], &connectDatabase)==SQLITE_OK)
{
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(connectDatabase, [query UTF8String], -1, &stmt, NULL)==SQLITE_OK)
{
sqlite3_step(stmt);
result=YES;
}
sqlite3_finalize(stmt);
}
sqlite3_close(connectDatabase);
return result;
}
Method for getting rows from table
-(NSMutableArray *)getData: (NSString *)query
{
NSMutableArray *arrData=[[NSMutableArray alloc]init];
if (sqlite3_open([destPath UTF8String],&connectDatabase)==SQLITE_OK)
{
sqlite3_stmt *stmt;
const char *query_stmt = [query UTF8String];
if (sqlite3_prepare_v2(connectDatabase,query_stmt, -1, &stmt, NULL)==SQLITE_OK)
{
while (sqlite3_step(stmt)==SQLITE_ROW)
{
NSMutableDictionary *dictResult=[[NSMutableDictionary alloc] init];
for (int i=0;i<sqlite3_column_count(stmt);i++)
{
NSString *str;
if (sqlite3_column_text(stmt,i)!=NULL)
{
str = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt,i)];
}
else
{
str=#"";
}
[dictResult setValue:str forKey:[NSString stringWithUTF8String:(char *)sqlite3_column_name(stmt,i)]];
}
[arrData addObject:dictResult];
}
sqlite3_finalize(stmt);
}
sqlite3_close(connectDatabase);
}
return arrData;
}
Above methods in swift will written as below
Method for copy database in document directory if not exist
func copyDatabaseToDocumentDirectory() {
let directoryList = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
var documentDirectory = directoryList.first
documentDirectory?.append("/DatabasePract1.sqlite")
print(documentDirectory!)
if !FileManager.default.fileExists(atPath: documentDirectory!) {
let databaseBundlePath = Bundle.main.path(forResource: "DatabasePract1", ofType: "sqlite")
do {
try FileManager.default.copyItem(atPath: databaseBundlePath!, toPath: documentDirectory!)
self.databasePath = documentDirectory
} catch {
print("Unable to copy database.")
}
} else {
print("database exist")
self.databasePath = documentDirectory
}
}
Method for insert/deleting/updating table
func dataManipulation(query: String) -> Bool {
var database: OpaquePointer?
var result = false
if (sqlite3_open(databasePath, &database) == SQLITE_OK) {
var queryStatement: OpaquePointer?
if (sqlite3_prepare_v2(database, query, -1, &queryStatement, nil) == SQLITE_OK) {
sqlite3_step(queryStatement)
result = true
} else {
let errmsg = String(cString: sqlite3_errmsg(database)!)
print("error preparing insert: \(errmsg)")
}
sqlite3_finalize(queryStatement)
}
sqlite3_close(database)
return result
}
Method for getting rows from table
func fetchData(_ query: String) -> [[String:Any]] {
var database: OpaquePointer?
var arrData: [[String:Any]] = []
if (sqlite3_open(databasePath, &database) == SQLITE_OK) {
var stmt:OpaquePointer?
if sqlite3_prepare(database, query, -1, &stmt, nil) != SQLITE_OK{
let errmsg = String(cString: sqlite3_errmsg(database)!)
print("error preparing insert: \(errmsg)")
return arrData
}
while(sqlite3_step(stmt) == SQLITE_ROW) {
var dictData: [String: Any] = [:]
for i in 0..<sqlite3_column_count(stmt) {
var strValue = ""
if (sqlite3_column_text(stmt, i) != nil) {
strValue = String(cString: sqlite3_column_text(stmt, i))
}
let keyName = String(cString: sqlite3_column_name(stmt, i), encoding: .utf8)
dictData[keyName!] = strValue
}
arrData.append(dictData)
}
sqlite3_close(database)
}
return arrData
}
Using swift, singleton class and FMDB. you can use below code to achieve it very easily.
Download example
import Foundation
class LocalDatabase: NSObject {
//sharedInstance
static let sharedInstance = LocalDatabase()
func methodToCreateDatabase() -> NSURL? {
let fileManager = NSFileManager.defaultManager()
let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
if let documentDirectory:NSURL = urls.first { // No use of as? NSURL because let urls returns array of NSURL
// exclude cloud backup
do {
try documentDirectory.setResourceValue(true, forKey: NSURLIsExcludedFromBackupKey)
} catch _{
print("Failed to exclude backup")
}
// This is where the database should be in the documents directory
let finalDatabaseURL = documentDirectory.URLByAppendingPathComponent("contact.db")
if finalDatabaseURL.checkResourceIsReachableAndReturnError(nil) {
// The file already exists, so just return the URL
return finalDatabaseURL
} else {
// Copy the initial file from the application bundle to the documents directory
if let bundleURL = NSBundle.mainBundle().URLForResource("contact", withExtension: "db") {
do {
try fileManager.copyItemAtURL(bundleURL, toURL: finalDatabaseURL)
} catch _ {
print("Couldn't copy file to final location!")
}
} else {
print("Couldn't find initial database in the bundle!")
}
}
} else {
print("Couldn't get documents directory!")
}
return nil
}
func methodToInsertUpdateDeleteData(strQuery : String) -> Bool
{
// print("%#",String(methodToCreateDatabase()!.absoluteString))
let contactDB = FMDatabase(path: String(methodToCreateDatabase()!.absoluteString) )
if contactDB.open() {
let insertSQL = strQuery
let result = contactDB.executeUpdate(insertSQL,
withArgumentsInArray: nil)
if !result {
print("Failed to add contact")
print("Error: \(contactDB.lastErrorMessage())")
return false
} else {
print("Contact Added")
return true
}
} else {
print("Error: \(contactDB.lastErrorMessage())")
return false
}
}
func methodToSelectData(strQuery : String) -> NSMutableArray
{
let arryToReturn : NSMutableArray = []
print("%#",String(methodToCreateDatabase()!.absoluteString))
let contactDB = FMDatabase(path: String(methodToCreateDatabase()!.absoluteString) )
if contactDB.open() {
let querySQL = strQuery
let results:FMResultSet? = contactDB.executeQuery(querySQL,
withArgumentsInArray: nil)
while results?.next() == true
{
arryToReturn.addObject(results!.resultDictionary())
}
// NSLog("%#", arryToReturn)
if arryToReturn.count == 0
{
print("Record Not Found")
}
else
{
print("Record Found")
}
contactDB.close()
} else {
print("Error: \(contactDB.lastErrorMessage())")
}
return arryToReturn
}
}
To copy .sqlite file in directory...
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// Database filename can have extension db/sqlite.
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:#"MapView.sqlite"];
success = [fileManager fileExistsAtPath:databasePath];
// if (success){
// return;
// }
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"MapView.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:databasePath error:&error];
if (!success) {
//NSAssert1(0, #"Failed to create writable database file with message '%#'.", [error localizedDescription]);
}
else
{
NSLog(#"Database created successfully");
}
To select Data From Database...
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &mapDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: #"SELECT * FROM maplatlong"];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(mapDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while(sqlite3_step(statement) == SQLITE_ROW)
{
NSString *cityN = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
NSString *lat = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
NSString *longi = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
[cityName addObject:cityN];
[latitude addObject:lat];
[longitude addObject:longi];
}
sqlite3_finalize(statement);
}
sqlite3_close(mapDB);
}

Creating a singleton DB helper class

I am trying to write a Master-Detail application that gets it's data from a sqlite database. As part of this I'm trying to create a helper class that creates a singleton instance of my database. All I want it to do is initialise the database so I can then reference this from the different views of the application.
I followed a tutorial that does this here: http://www.raywenderlich.com/913/sqlite-101-for-iphone-developers-making-our-app
I got the tutorial to work however now I am trying to modify it to fit my application and I can't seem to get it working. I have no errors or warnings but when I run the app on the emulator none of the debug text I have put in executes. So it looks to me like my init function is not executing. I just can't figure out why.
Can anyone spot my problem in the code below?
LoyaltyProgramDatabase.h
#import <Foundation/Foundation.h>
#import <sqlite3.h>
#interface LoyaltyProgramDatabase : NSObject {
sqlite3 *_loyaltyProgDB;
}
#property (strong, nonatomic) NSString *databasePath; //Path file of our database
#property (nonatomic) sqlite3 *loyaltyProgDB; //Reference to the database
+ (LoyaltyProgramDatabase*)loyaltyProgDB;
#end
LoyaltyProgramDatabase.m
#import "LoyaltyProgramDatabase.h"
#implementation LoyaltyProgramDatabase
static LoyaltyProgramDatabase *_loyaltyProgDB;
//Create a singleton instance of loyaltyProgDB
+ (LoyaltyProgramDatabase*)loyaltyProgDB {
if (_loyaltyProgDB == nil) {
_loyaltyProgDB = [[LoyaltyProgramDatabase alloc] init];
}
return _loyaltyProgDB;
}
- (id)init {
NSLog(#"Inside init function");
if ((self = [super init])) {
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:#"loyaltyProg.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: _databasePath ] == NO)
{
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_loyaltyProgDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS scoreCard (ID INTEGER PRIMARY KEY AUTOINCREMENT, campaignID INTEGER, merchantName TEXT)";
if (sqlite3_exec(_loyaltyProgDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
//_status.text = #"Failed to create table";
NSLog(#"Failed to create table");
}
sqlite3_close(_loyaltyProgDB);
} else {
//_status.text = #"Failed to open/create database";
NSLog(#"Failed to open/create database");
}
}
}
return self;
}
- (void)dealloc {
sqlite3_close(_loyaltyProgDB);
}
#end
Try this:
+ (id)allocWithZone:(NSZone *)zone
{
return [self loyaltyProgDB];
}
+ (LoyaltyProgramDatabase*)loyaltyProgDB
{
static BNRImageStore *loyaltyProgDB = nil;
if (!loyaltyProgDB) {
// Create the singleton
loyaltyProgDB = [[super allocWithZone:NULL] init];
}
return loyaltyProgDB;
}
- (id)init {
self = [super init];
if (self) {
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:#"loyaltyProg.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: _databasePath ] == NO)
{
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_loyaltyProgDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS scoreCard (ID INTEGER PRIMARY KEY AUTOINCREMENT, campaignID INTEGER, merchantName TEXT)";
if (sqlite3_exec(_loyaltyProgDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
//_status.text = #"Failed to create table";
NSLog(#"Failed to create table");
}
sqlite3_close(_loyaltyProgDB);
} else {
//_status.text = #"Failed to open/create database";
NSLog(#"Failed to open/create database");
}
}
}
return self;
}
If you have not written a line of code that says [[LoyaltyProgramDatabase alloc] init] or [LoyaltyProgramDatabase loyaltyProgDB], then your init function is never getting called.
The init is not automatically called when you start the app. The only time any init function is called automatically is if you have a UI element in a nib/xib/storyboard file, and the app creates the element utilizing various initialization functions, but never your own init function. But for something like what you have written, to initialize the database object, you need to call the init function yourself.
In your app delegate, under the didFinishLaunching function, put this line:
[LoyaltyProgramDatabase loyaltyProgDB];
This isn't a really up-to-date singleton pattern by the way. This is actually a lazy loader function. If you want a more secure singleton, use this code.
+(id)sharedInstance {
static dispatch_once_t pred = 0;
__strong static id _sharedObject = nil;
dispatch_once(&pred, ^{
_sharedObject = [[self alloc] init];
});
return _sharedObject;
}
This will make sure the database is only ever initialized once and it is thread safe. To create/use the singleton, the code would change from the above to
[LoyaltyProgramDatabase sharedInstance]

Resources