Skip to content

Instantly share code, notes, and snippets.

@thiagoeliasr
Created October 5, 2017 18:36
Show Gist options
  • Save thiagoeliasr/b9cf143a530a372f07495c8b692cdf32 to your computer and use it in GitHub Desktop.
Save thiagoeliasr/b9cf143a530a372f07495c8b692cdf32 to your computer and use it in GitHub Desktop.
Opening sqlite databases using external storage in Android.
/*
Write permission is necessary in your AndroidManifest.xml.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
*/
try {
//Defining your external storage path.
String extStore = Environment.getExternalStorageDirectory().getPath();
//Defining the file to be opened.
File dbfile = new File(extStore + "/yourdatabase.db");
//stablishing the connection
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
//working with query and results.
Cursor cursor = db.query("table", new String[]{"field1","field2"}, null, null, null, null, null, "100");
while (cursor.moveToNext()) {
String field1 = cursor.getString(cursor.getColumnIndex("field1"));
String field2 = cursor.getString(cursor.getColumnIndex("field2"));
Log.i(TAG, field1 + " " + field2);
}
cursor.close();
} catch (SQLiteCantOpenDatabaseException e) {
Log.d(TAG, "Error opening sqlite database: " + e.getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment