How to store sqlite database directly on sdcard
I created my DB with
public DatabaseHelper(final Context context) {
super(context, Environment.getExternalStorageDirectory()
+ File.separator + FILE_DIR
+ File.separator + DATABASE_NAME, null, DATABASE_VERSION);
}
and had no problems further on. I guess your call to super() should reference the sdcard as well.
android SQLite database in sdcard
You can store your database directly in SD card like this:
static class DatabaseClass extends SQLiteOpenHelper {
DatabaseClass(final Context context) {
super(context, Environment.getExternalStorageDirectory()
+ File.separator + "/DataBase/" + File.separator
+ DATABASE_NAME, null, DATABASE_VERSION);
}
If you store ur databse in internal memory then you can also copy the database from internal memory to SD card :)
Here's an example how to copy data to SD card:
InputStream myInput = new FileInputStream("/data/data/pack_name/databases/databasename.db");
File directory = new File("/sdcard/some_folder");
if (!directory.exists()) {
directory.mkdirs();
}
OutputStream myOutput = new FileOutputStream(directory.getPath() + "/AppName.backup");
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
How to create a sqlite db on the sdcard
Actually, the db is created in /data/data/your.app.name/databases/your.db,
but you can specify the path to your sd card
In short:
Set this permission in your Manifest file:
<!-- To write and read to and from SDCard -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />get the path to your sd card
public static final String DATABASE_FILE_PATH = Environment.getExternalStorageDirectory();
When opening your db (getWitableDatabase, getReadableDatabase), specify you path:
database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
+ File.separator + DATABASE_NAME, null,SQLiteDatabase.OPEN_READWRITE);or
database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
+ File.separator + DATABASE_NAME, null,SQLiteDatabase.OPEN_READONLY);respectively
[EDIT]
Note that WRITE_EXTERNAL_STORAGE includes READ_EXTERNAL_STORAGE, so, no need to specify this one too.
Android - SQLite database on SD card
he is a proposed solution which i found in stackoverflow
File dbfile = new File("/sdcard/android/com.myapp/databases/mydatabase.db" );
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
System.out.println("Its open? " + db.isOpen());
here is the link.
UPDATE
i am not sure you can use this along with SQLiteOpenHelper, but you sure can query the database object.
db.getVersion();
db.execSQL(sql);
db.beginTransaction();
db.endTransaction();
db.setTransactionSuccessful();
db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
you can do all the things which you expect with a database. SQLiteOpenHelper in only a wrapper class which helps you the extra which we always can do on our own.
EDIT
as for your file size limit i found this link. Is there a file size limit on Android Honeycomb?
SQLiteOpenHelper - creating database on SD card
First you have to specify the path of the sdcard. You can do that by creating a string like this:
public static final String DATABASE_FILE_PATH = "/sdcard";
But for you should call
Environment.getExternalStorageDirectory()
to get the root path to the SD card and use that to create the database. After that you create the database as you want. Here is an example
public class DatabaseHelper
{
private static final String TAG = "DatabaseHelper";
public static final String DATABASE_FILE_PATH = Environment.getExternalStorageDirectory();
public static final String DATABASE_NAME = "mydb";
public static final String TRACKS_TABLE = "tracks";
public static final String TRACK_INFO_TABLE = "track_info";
private static final String TRACKS_TABLE_CREATE = "create table "
+ TRACKS_TABLE
+ " (_id integer primary key autoincrement, title text not null, description text null, created_at date not null);";
private static final String TRACK_INFO_TABLE_CREATE = "create table "
+ TRACK_INFO_TABLE
+ " (_id integer primary key autoincrement, track_id integer not null, latitude real not null, longitude real not null, altitude real not null, created_at date not null);";
private SQLiteDatabase database;
public DatabaseHelper()
{
try
{
database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
+ File.separator + DATABASE_NAME, null,SQLiteDatabase.OPEN_READWRITE);
}
catch (SQLiteException ex)
{
Log.e(TAG, "error -- " + ex.getMessage(), ex);
// error means tables does not exits
createTables();
}
finally
{
DBUtil.safeCloseDataBase(database);
}
}
private void createTables()
{
database.execSQL(TRACKS_TABLE_CREATE);
database.execSQL(TRACK_INFO_TABLE_CREATE);
}
public void close()
{
DBUtil.safeCloseDataBase(database);
}
public SQLiteDatabase getReadableDatabase()
{
database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
+ File.separator + DATABASE_NAME, null,
SQLiteDatabase.OPEN_READONLY);
return database;
}
public SQLiteDatabase getWritableDatabase()
{
database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
+ File.separator + DATABASE_NAME, null,
SQLiteDatabase.OPEN_READWRITE);
return database;
}
And in the end you have to set permission in manifest like this: android.permission.WRITE_EXTERNAL_STORAGE
Good luck :)
Arkde
Related Topics
Http Request in Android with Kotlin
Cannot Resolve Method Setlatesteventinfo
How to Hide System Navigation Bar in Tablets
How to Get Activity's Content View
How to Format Datetime in Flutter
Spannablestringbuilder to Create String with Multiple Fonts/Text Sizes etc Example
Redirecting Audio/Creating Alternate Sound Paths in Android
Listselector Applies to the Entire List
How to Check Whether the Sim Card Is Available in an Android Device
List of Files in Assets Folder and Its Subfolders
Android: How to Make an Activity Return Results to the Activity Which Calls It
Overriding Referenced Style Attributes
How to Add Action Bar from Support Library into Preferenceactivity