Created
September 29, 2012 20:46
-
-
Save k4ml/3805152 to your computer and use it in GitHub Desktop.
Using existing sqlite database in android with phonegap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://www.corporatezen.com/shipping_prepopulated_database_with_phonegap | |
package net.smach.halal; | |
import java.io.*; | |
import android.os.Bundle; | |
import android.app.Activity; | |
import android.view.Menu; | |
import org.apache.cordova.*; | |
public class MainActivity extends DroidGap { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
try | |
{ | |
String pName = this.getClass().getPackage().getName(); | |
this.copy("Databases.db","/data/data/"+pName+"/databases/"); | |
this.copy("0000000000000001.db","/data/data/"+pName+"/databases/file__0/"); | |
} | |
catch (IOException e) | |
{ | |
e.printStackTrace(); | |
} | |
super.loadUrl("file:///android_asset/www/index.html"); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.activity_main, menu); | |
return true; | |
} | |
void copy(String file, String folder) throws IOException { | |
File CheckDirectory; | |
CheckDirectory = new File(folder); | |
if (!CheckDirectory.exists()) | |
{ | |
CheckDirectory.mkdir(); | |
} | |
InputStream in = getApplicationContext().getAssets().open(file); | |
OutputStream out = new FileOutputStream(folder+file); | |
// Transfer bytes from in to out | |
byte[] buf = new byte[1024]; | |
int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); | |
in.close(); out.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If I read this right (haven't test it yet), the Databases.db and the 0000000000000001.db file will be overwritten everytime the app loads. Is that right?