Skip to content

Instantly share code, notes, and snippets.

@daniele777
Last active August 29, 2015 14:04
Show Gist options
  • Save daniele777/a69c36dc68bd468ba68f to your computer and use it in GitHub Desktop.
Save daniele777/a69c36dc68bd468ba68f to your computer and use it in GitHub Desktop.
Hi great tuttorial ...but in eclipse don't work i recieving error...
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.view.View.OnClickListener;
public class AssetDatabaseOpenHelper {
private static final String DB_NAME = "danitest.db";
private Context context;
public AssetDatabaseOpenHelper(OnClickListener onClickListener) {
this.context = (Context) onClickListener;
}
public SQLiteDatabase openDatabase() {
File dbFile = context.getDatabasePath(DB_NAME);
if (!dbFile.exists()) {
try {
copyDatabase(dbFile);
} catch (IOException e) {
throw new RuntimeException("Error creating source database", e);
}
}
return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
}
private void copyDatabase(File dbFile) throws IOException {
InputStream is = context.getAssets().open(DB_NAME);
OutputStream os = new FileOutputStream(dbFile);
byte[] buffer = new byte[1024];
while (is.read(buffer) > 0) {
os.write(buffer);
}
os.flush();
os.close();
is.close();
}
}
For Run
public class javapage1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newinterface);
Button btnHome=(Button)findViewById(R.id.button1);
btnHome.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
AssetDatabaseOpenHelper adb = new AssetDatabaseOpenHelper(this);
SQLiteDatabase db = adb.openDatabase();
Cursor c = db.rawQuery("SELECT * FROM xxx;", null);
Log.d("MyApp", "cnt: "+c.getCount());
}
});
}
}
database in root/assets/danitest.db
08-03 07:21:01.222: D/AndroidRuntime(2900): Shutting down VM
08-03 07:21:01.222: W/dalvikvm(2900): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-03 07:21:01.242: E/AndroidRuntime(2900): FATAL EXCEPTION: main
08-03 07:21:01.242: E/AndroidRuntime(2900): java.lang.ClassCastException: example.javapage1$1
08-03 07:21:01.242: E/AndroidRuntime(2900): at example.AssetDatabaseOpenHelper.<init>(AssetDatabaseOpenHelper.java:20)
08-03 07:21:01.242: E/AndroidRuntime(2900): at example.javapage1$1.onClick(javapage1.java:22)
08-03 07:21:01.242: E/AndroidRuntime(2900): at android.view.View.performClick(View.java:2408)
08-03 07:21:01.242: E/AndroidRuntime(2900): at android.view.View$PerformClick.run(View.java:8816)
08-03 07:21:01.242: E/AndroidRuntime(2900): at android.os.Handler.handleCallback(Handler.java:587)
08-03 07:21:01.242: E/AndroidRuntime(2900): at android.os.Handler.dispatchMessage(Handler.java:92)
08-03 07:21:01.242: E/AndroidRuntime(2900): at android.os.Looper.loop(Looper.java:123)
08-03 07:21:01.242: E/AndroidRuntime(2900): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-03 07:21:01.242: E/AndroidRuntime(2900): at java.lang.reflect.Method.invokeNative(Native Method)
08-03 07:21:01.242: E/AndroidRuntime(2900): at java.lang.reflect.Method.invoke(Method.java:521)
08-03 07:21:01.242: E/AndroidRuntime(2900): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-03 07:21:01.242: E/AndroidRuntime(2900): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-03 07:21:01.242: E/AndroidRuntime(2900): at dalvik.system.NativeStart.main(Native Method)
08-03 07:21:05.302: I/Process(2900): Sending signal. PID: 2900 SIG: 9
Helppp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment