Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sqlite datbase crete empty copy.

jitendrajitendra Posts: 59New Users @
edited April 2013 in iOS SDK Development
I want to fetch data from sqlite database.

create database by using this code but cretae empty copy why this happening.

code is here.



- (void) checkAndCreateDatabase { NSString *databasePath; sqlite3 *database; NSString *databaseName = @"Card.sqlite"; // Name Of DataBase. // Get the path to the documents directory and append the databaseName NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = documentPaths[0]; NSLog(@"Dir : %@ ",documentsDir); databasePath = [documentsDir stringByAppendingPathComponent:databaseName]; // 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]; //[fileManager release]; }


help me out this.
thanks in advance.

Replies

  • jitendrajitendra Posts: 59New Users @
    I solved this issue.
    Solution i am deleting blank copy of Sqlite file genrated in documents directory.
    NSFileManager * manger=[NSFileManager defaultManager]; NSArray * allfiles=[manger contentsOfDirectoryAtPath:documentsDir error:nil]; NSPredicate * fltr=[NSPredicate predicateWithFormat:@"self ENDSWITH'.sqlite'"]; NSArray * sqlitefiles=[allfiles filteredArrayUsingPredicate:fltr]; for(NSString * sqlitefile in sqlitefiles) { [manger removeItemAtPath:sqlitefile error:nil]; }

    And then copying sqlite file from bundle like this.


    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; // NSLog(@"%@",databasePathFromApp); [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
  • obaidjawadobaidjawad Posts: 181Registered Users @ @
    But I think with your solution it will delete every time you run this method checkAndCreateDatabase, Check the below code easily understandable.
    
    -(void)CreateDatabaseFile{
        
    NSString *databaseName;
    NSString *databasePath;
    
    databaseName = @"PDF_DB.sqlite";
    
    
    
    // Get the path to the documents directory and append the databaseName
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    
    
    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];
    
    
    
    }
    
    And this is where you get your filePath method.
    
    -(NSString*)filepath{
        NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDir=[path objectAtIndex:0];
        return [documentsDir stringByAppendingPathComponent:@"PDF_DB.sqlite"];
    }
  • jitendrajitendra Posts: 59New Users @
    @obaidjawad Thank you this code work perfect for me.
Sign In or Register to comment.