Skip to content Skip to sidebar Skip to footer

Phonegap(3.4.0) Android App With Sqlite, Its Working Fine In Emulator But Not In Device

I have created in PhoneGap(3.4.0) android app with sqlite, its working fine in emulator but not working in device. I am using populated database with lots of records. I have google

Solution 1:

I solved my issue using this, If you are working for emulator then copy your databas using command prompt, I have added myDB.sqlite in asset folder then run

adb push myDB.sqlite /data/data/com.my_app.my_app/databases/myDB.sqlite

For device, just put them in to bundle of app that is assets folder in case of Android . you need myDB.sqlite and file__0/0000000000000001.sqlite files. More....

publicvoidonCreate(Bundle savedInstanceState)
{
   super.onCreate(savedInstanceState);
   super.init();
   try
   {
        StringpName=this.getClass().getPackage().getName();
        this.copy("myDB.sqlite","/data/data/"+pName+"/databases/");
        this.copy("0000000000000001.sqlite","/data/data/"+pName+"/app_database/file__0/");
   }
   catch (IOException e)
   {
       e.printStackTrace();
   }

  super.loadUrl(Config.getStartUrl());    
}

voidcopy(String file, String folder)throws IOException 
{

   File CheckDirectory;
   CheckDirectory = newFile(folder);
   if (!CheckDirectory.exists())
   { 
     CheckDirectory.mkdir();
   }

   InputStreamin= getApplicationContext().getAssets().open(file);
   OutputStreamout=newFileOutputStream(folder+file);
   // Transfer bytes from in to outbyte[] buf = newbyte[1024];
   int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len);
   in.close(); out.close();

}

For iOS you can use following steps:

Step 1 : Remove the .sqlite extension from the sqlite database. Ex. If we have the database called myDB.sqlite make it only myDB.

Step 2 : Then drag the myDB file to Resource directory in Xcode by choosing the option “Create groups for any added folder” and check the “Copy items into destination group’s folder (if needed)” option while adding reference.

Step 3 : Now use the following code to copy the database into the Document directory inside our application.

-(void) copyDatabase{

   NSFileManager *fileManager = [NSFileManager defaultManager];
   NSError *error;

   NSString *dbPath = [self getDBPath];
   BOOL success = [fileManager fileExistsAtPath:dbPath];

   if(!success) {

     NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myDB"];
     success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath 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 *documentsDir = [paths objectAtIndex:0];
   return [documentsDir stringByAppendingPathComponent:@"myDB"];
}

Step 4 : Use this code [self copyDatabase]; and call this in the didFinishLaunchingWithOptions(AppDelegate.m ) method like this

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
   [self copyDatabase];
}

Now open the database using following code.

functionopenDatabase() {
      try{
           if(!!window.openDatabase){
            var openDB = window.sqlitePlugin.openDatabase({name : "myDB"});
           }else{
              navigator.notification.alert("Local database is not supported by your device.");
           }
      }
      catch(error){
         console.log("ERROR:" + error.message);
 }

More detail...

Post a Comment for "Phonegap(3.4.0) Android App With Sqlite, Its Working Fine In Emulator But Not In Device"