0

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!

CodeWarrior
  • 5,026
  • 6
  • 30
  • 46
iBeris
  • 5
  • 2

3 Answers3

2

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.

turtle
  • 1,619
  • 1
  • 14
  • 30
  • but i can't able to update db in sdcard. it says it is readonly – GK_ Oct 13 '14 at 05:40
  • what are u exactly doing? means how are u modifying the db in sdcard, manually? – turtle Oct 13 '14 at 07:18
  • i reading the db tables in the sdcard inside app, when i try to update the row. i got error . http://stackoverflow.com/questions/26333738/attempting-to-write-in-a-readonly-database-titanium-android – GK_ Oct 13 '14 at 09:09
0

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...

Community
  • 1
  • 1
David N
  • 509
  • 4
  • 12
0

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());
      } 
Anil Jadhav
  • 2,128
  • 1
  • 17
  • 31