When I test my app on the simulator everything works. But when I test the app on my iPhone, it doesn't load from the db.sqlite3 data base.
I tried every solution from the internet. I cleaned the app. I added the database to the build phrases - copy bundle resources etc.
Can you help me? Is there any fails in my coding?
-(void)openDataBase{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
dbPath = [documentsDirectory stringByAppendingString:#"db.sqlite3"];
//load it
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *pathInRessource = [[NSBundle mainBundle] pathForResource:#"db" ofType:#"sqlite3"];
//exist it?
if (![fileManager fileExistsAtPath:dbPath]) {
[fileManager copyItemAtPath:pathInRessource toPath:dbPath error:nil];
}
//open database
int result = sqlite3_open([dbPath UTF8String], &db);
if (result != SQLITE_OK) {
sqlite3_close(db);
return;
}
//if ok
NSLog(#"Data base opened");
}
At the time of opening database you can call this with its else case to get the issue.
if (sqlite3_open([getDBPath UTF8String], &sqlhandle) == SQLITE_OK)
{
NSLog(#"Database Open!");
}
else
{
NSLog(#"Select err: %s",sqlite3_errmsg(sqlhandle));
}
You will be able to get proper error message using this
I am wondring where you getting stuck, I am doing same way and its working fine to me.
BTW I have updated your code, how I am handling, please check let me know if find and trouble in this.
//My DB variable
static sqlite3 *database;
-(void)openDataBase{
if (database == NULL)
sqlite3 *mDatabse;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
dbPath = [documentsDirectory stringByAppendingString:#"db.sqlite3"];
//load it
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *pathInRessource = [[NSBundle mainBundle] pathForResource:#"db" ofType:#"sqlite3"];
//exist it?
if (![fileManager fileExistsAtPath:dbPath]) {
[fileManager copyItemAtPath:pathInRessource toPath:dbPath error:nil];
}
if (sqlite3_open([path UTF8String], &mDatabse) == SQLITE_OK) {
NSLog(#"Database Successfully Opened");
database = mDatabse;
} else {
NSLog(#"Error in opening database");
mDatabse = NULL;
}
}
Related
I want to update my table.This code work on simulator but does not work on device. I found that I can't write to any files in my app bundle but I do not how change it to work. Please help me.
-(NSString *)filePath
{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir=[paths objectAtIndex:0];
NSError *error;
bool success;
NSString *writablePath = [documentsDir stringByAppendingPathComponent:#"Test.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:writablePath];
if(!success)
{
NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Test.sqlite"];
success = [fileManager copyItemAtPath:defaultPath toPath:writablePath error:&error];
}
NSLog(#"%#",writablePath);
return writablePath;
}
And Then:
- (IBAction)addToFavorite:(id)sender
{
[self filePath];
sqlite3_stmt *updateStatement=nil;
NSString *sql=[[NSString alloc]initWithFormat:#"UPDATE A SET favorite=? WHERE x='%#' ",mystring];
if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &updateStatement, nil)==SQLITE_OK)
{
sqlite3_bind_text(updateStatement, 1, [#"1" UTF8String], -1, SQLITE_TRANSIENT);
}
if (sqlite3_step(updateStatement)!=SQLITE_DONE)
{
NSAssert(0,#"Error While Creating Update Statement. '%s'",sqlite3_errmsg(db));
}
sqlite3_reset(updateStatement);
}
I thing you have made changes in simulator, but on device new database have not been updated.
Just delete your app from device and re-run it i.e.
CLEAN INSTALL
Am making an iOS apps with an embedded SQLite database. So I make my DB in a SQLite administrator and drag it to my Xcode project, like in the tutorial says.
When I try open my DataBase, I get this error: "out of memory". Dont know if it is a SQLite bug or something, but my file is really small to get a memory problem.
This is my code to init my DataBase:
- (id)initWithPath:(NSString *)path {
if (self = [super init]) {
BOOL success;
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:#"SoundLib_DB.sqlite"];
if ([fileManager fileExistsAtPath:dbPath] == NO) {
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:#"SoundLib_DB" ofType:#"sqlite"];
[fileManager copyItemAtPath:resourcePath toPath:dbPath error:&error];
}
success = [fileManager fileExistsAtPath:dbPath];
if(!success)
{
NSLog(#"Cannot locate database file '%#'.", dbPath);
}
sqlite3 *dbConnection;
//Here is when I get the error, at trying to open the DB
if (sqlite3_open_v2("SoundLib", &dbConnection, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) {
NSLog(#"[SQLITE] Unable to open database!");
NSLog(#"%s Prepare failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(database), sqlite3_errcode(database));
return nil;
}
database = dbConnection;
}
return self;
}
Error may be because you need to pass databasepath as UTF8String. I see you are passing "SoundLib". You can not directly pass like that.
if (sqlite3_open([dbPath UTF8String], &databaseHandle) == SQLITE_OK)
{
//dbPath is your full database path you getting above
}
P.S. Also have dbpath as const char
const char *dbpath
Change your opening process so that it looks like this... It works for me ;)
I guess your main mistake is that you forgot the UTF8String....
sqlite3 *database;
int result = sqlite3_open([dbPath UTF8String], &database);
if (result != SQLITE_OK) {
sqlite3_close(database);
NSLog(#"Error opening databse");
return;
}
I m using below code to for sqlite DB, but I can not insert values with Execute query can any one tell me where i m wrong ?
- (void)createEditableCopyOfDatabaseIfNeeded {
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DATABASE_NAME];
success = [fileManager fileExistsAtPath:writableDBPath];
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];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, #"Failed to create writable database file with message '%#'.", [error localizedDescription]);
}
}
Initialize Database
- (void)initializeDatabase {
// The database is stored in the application bundle.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:DATABASE_NAME];
// Open the database. The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{
//TRUE
}
else
{
// Even though the open failed, call close to properly clean up resources.
sqlite3_close(database);
NSAssert1(0, #"Failed to open database with message '%s'.", sqlite3_errmsg(database));
// Additional error handling, as appropriate...
}
}
Execute Sql Method
-(void)ExecuteSQL:(NSString*)SQL
{
#synchronized(self)
{
sqlite3_stmt *select_statement=nil;
if (sqlite3_prepare_v2(database, [SQL UTF8String], -1, &select_statement, NULL) != SQLITE_OK) {
NSString *errString = [NSString stringWithFormat:#"%#",
[NSLocalizedString(#"statementPrepareFailedKey",#"")
stringByReplacingOccurrencesOfString:#"#" withString:[NSString
stringWithCString:sqlite3_errmsg(database) encoding:NSUTF8StringEncoding] ]];
NSAssert1(0, #"%#", errString);
}
//int columncount=sqlite3_column_count(select_statement);
//NSMutableDictionary* dic;
if(sqlite3_step(select_statement) == SQLITE_DONE)
{
NSLog(#"Execute sql");
}
sqlite3_finalize(select_statement);
}
}
Thanks.
How can i replace the database (with the one i am working on) in my app on each simulator run? (Still adding data and want to check that it looks good)
I tried this but it doesn't seem to be working, still using the old database:
- (void)viewDidLoad
{
dbname = #"animaldatabase.sql";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
dbpath = [documentsDir stringByAppendingPathComponent:dbname];
[self checkAndCreateDatabase];
[self readAnimalsFromDatabase];
[super viewDidLoad];
// Do any additional setup after loading the view.
mylabel.text = [NSString stringWithFormat:#"%d", [animals count]];
}
- (void) checkAndCreateDatabase{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:dbpath];
if (success) {
//return;
[fileManager removeItemAtPath:dbpath error:nil];
}
NSString *dbPathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbname];
[fileManager copyItemAtPath:dbPathFromApp toPath:dbpath error:nil];
}
Also where is the location for the iphone database? When my app copies the database into the iphone, where does it end up?
UPDATE
Added the loading code as well.
Senthilkumar:
I fail to see how this should solve the problem? Doesn't this just skip the copying or my database if it already exists?
What i want is to delete the database in my iphone app. Import the new database and then run it.
use the following code instead of that.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
databaseName=#"yourfileName.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
[self checkAndCreateDatabase];
}
-(void) checkAndCreateDatabase {
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if(success) return;
else
printf("NO File found");
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
To help debug file manager operations, it helps to try and use the error parameter, like so:
NSError* error;
[fileManager copyItemAtPath:dbPathFromApp toPath:dbpath error: &error];
if (error != nil) {
NSLog(#"copyItemAtPath generated error=%#", [error localizedDescription]);
}
If you do this, the error message will get written to your debugger console / standard output window.
I am using fmdb but i am not knowing where to add the db file.
I found this old answer that i think it doesn't fit for xcode 4.2 (since we dont have 'Resources' folder anymore).
I created a database in 'Lita', added the extension .sqlite and added the db file as follows :
then tried to use the following
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *path = [docsPath stringByAppendingPathComponent:#"db.sqlite"];
FMDatabase *database = [FMDatabase databaseWithPath:path];
[database open];
FMResultSet *results = [database executeQuery:#"select * from tbl"];
printf("hi");
while([results next]) {
printf("hi2");
NSString *name = [results stringForColumn:#"clm1"];
NSString *text = [results stringForColumn:#"clm2"];
NSLog(#"test: %# - %#",name, text);
}
printf("done");
i am getting the 'hi' 'done' but never 'hi2'...
anyone can help?
You want to add the (compiled) database to the project as a resource, and then copy it into the documents folder of the app on first start-up.
Here is an example method that does the work, call it from the applicationDidFinishLaunching and use the resultant FMDatabase * for all you queries.
- (FMDatabase *)openDatabase
{
NSFileManager *fm = [NSFileManager defaultManager];
NSString *documents_dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *db_path = [documents_dir stringByAppendingPathComponent:[NSString stringWithFormat:#"database.s3db"]];
NSString *template_path = [[NSBundle mainBundle] pathForResource:#"template_db" ofType:#"s3db"];
if (![fm fileExistsAtPath:db_path])
[fm copyItemAtPath:template_path toPath:db_path error:nil];
FMDatabase *db = [FMDatabase databaseWithPath:db_path];
if (![db open])
NSLog(#"Failed to open database!");
return db;
}
I am working with the same issues right now. But to answer your question: the documents folder is the folder that is to be used in the device.
Put the database in the the supporting files folder.
Here is what works for me:
//hide status bar for full screen view
[[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
//set reference to database here
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
self.databasePath = [documentDir stringByAppendingPathComponent:#"RPRA.sqlite"];
[self createAndCheckDatabase];
// Override point for customization after application launch.
return YES;
}
//method used to create database and check if it exists
-(void) createAndCheckDatabase
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:self.databasePath];
if(success) return;
//else move database into documents folder
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"RPRA.db"];
[fileManager copyItemAtPath:databasePathFromApp toPath:self.databasePath error:nil];
}
- (void)ensureDBFile
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dbFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"retails.sqlite"]];
NSLog(#"Database Filepath Delgate = %#", dbFilePath);
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL dbFileExists = [fileManager fileExistsAtPath:dbFilePath];
if(!dbFileExists){
//Copy the db file if it didn't exist
NSString *bundledDbPath = [[NSBundle mainBundle] pathForResource:#"retails" ofType:#"sqlite"];
if (bundledDbPath != nil) {
BOOL copiedDbFile = [fileManager copyItemAtPath:bundledDbPath toPath:dbFilePath error:nil];
if (!copiedDbFile) {
NSLog(#"Error: Couldn't copy the db file from the bundle");
}
}
else {
NSLog(#"Error: Couldn't find the db file in the bundle");
}
}
self.db = [FMDatabase databaseWithPath:dbFilePath];
[self.db setShouldCacheStatements:NO];
[self.db open];
if([self.db hadError]) {
NSLog(#"Database Error %d: %#", [self.db lastErrorCode], [self.db lastErrorMessage]);
}
}