Skip to content

Instantly share code, notes, and snippets.

@k4ml
Created September 29, 2012 20:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save k4ml/3805152 to your computer and use it in GitHub Desktop.
Save k4ml/3805152 to your computer and use it in GitHub Desktop.
Using existing sqlite database in android with phonegap
// 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();
}
}
@kidino
Copy link

kidino commented Apr 8, 2013

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment