iBeris
iBeris

Reputation: 5

Titanium how to move DB to SD card

Im programming in Titanium for Android. I have 6 sqlite databases and i don't want to store them on the device internal memory because DB amount size is too large.

So, how can i move the sqlite files to SD card programmatically? Or how to install Dbs directly on the SD card when users installs my App from PlayStore?

PD: I try adding "PreferExternal" but this didn't fix my problem.

<manifest android:installLocation="preferExternal</manifest>

thanks in advance!

Upvotes: 0

Views: 344

Answers (3)

turtle
turtle

Reputation: 1619

From Titanium docs of Ti.Database.open.

Open a Database on External Storage (Android) A database, with a filename of mydb2Installed and located at the absolute path provided, is opened.

if (Ti.Platform.name === 'android' && Ti.Filesystem.isExternalStoragePresent()) {
  var db2 = Ti.Database.open(Ti.Filesystem.externalStorageDirectory + 'path' + Ti.Filesystem.separator + 'to' + Ti.Filesystem.separator + 'mydb2Installed');
}

Hopefully this will do the trick.

Upvotes: 2

David N
David N

Reputation: 519

According to this question : Android: use SQLite database on SD Card (not using internal Android Data Store at all)

You can use :

 SQLiteDatabase.openOrCreateDatabase(String, SQLiteDatabase.CursorFactory) 

Put your path as the first parameter and null as the second. To get the path of your sdcard do :

 Environment.getExternalStorageDirectory();

But note that everyone with a physical access to the device can access to the database file...

Upvotes: 0

Anil Jadhav
Anil Jadhav

Reputation: 2158

I have no idea about titanium. But in android you can move DB by below logic:

  1. copy your Database.db file in your projects assets folder.
  2. In manifest file define permission shown below:

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
  3. now using code copy database file from /asset to device's external storage

For copy file use below code,

    try {
     // Open your local db as the input stream
     InputStream myInput = myContext.getAssets().open("your database file name");

     // Path to the just created empty db
     String outFileName = "path of external storage/<database_file_name>";

     OutputStream myOutput = new FileOutputStream(outFileName);

     // transfer bytes from the inputfile to the outputfile
     byte[] buffer = new byte[1024];
     int length;
     while ((length = myInput.read(buffer)) > 0) 
     {
       myOutput.write(buffer, 0, length);
     }

     // Close the streams
      myOutput.flush();
      myOutput.close();
      myInput.close();
     } 
     catch (Exception e) 
      {
       Log.e("error", e.toString());
      } 

Upvotes: 0

Related Questions