Creating database on sdcard ...

Put your problem here if it does not fit any of the other categories.

Creating database on sdcard ...

Postby preetam_pict » Wed Sep 02, 2009 8:16 am

hi ...

well how_to_use_single_database_for_multiple_application talks about creating database on sd card ... (you need such thing to do when your database size is huge!)

as discussed in above topic we need to use
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. SQLiteDatabase.openDatabase("/sdcard/demo.db3", null, SQLiteDatabase.OPEN_READWRITE + SQLiteDatabase.CREATE_IF_NECESSARY);
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


but in such cases we cant use
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. SQLiteOpenHelper
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


so decided to write a sample util class which would so the similar thing ...

code as below ...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import android.database.Cursor;
  3.  
  4. import android.database.sqlite.SQLiteDatabase;
  5.  
  6.  
  7.  
  8. /**
  9.  
  10.  * @author preetam.palwe
  11.  
  12.  *
  13.  
  14.  */
  15.  
  16. public class DBUtil
  17.  
  18. {
  19.  
  20.     private static final String TAG = "DBUtil";
  21.  
  22.  
  23.  
  24.     public static void safeCloseCursor(Cursor cursor)
  25.  
  26.     {
  27.  
  28.         if (cursor != null)
  29.  
  30.         {
  31.  
  32.             cursor.close();
  33.  
  34.         }
  35.  
  36.     }
  37.  
  38.  
  39.  
  40.     public static void safeCloseDataBase(SQLiteDatabase database)
  41.  
  42.     {
  43.  
  44.         if (database != null)
  45.  
  46.         {
  47.  
  48.             database.close();
  49.  
  50.         }
  51.  
  52.     }
  53.  
  54. }
  55.  
  56.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

and
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import java.io.File;
  3.  
  4.  
  5.  
  6. import android.database.Cursor;
  7.  
  8. import android.database.sqlite.SQLiteDatabase;
  9.  
  10. import android.util.Log;
  11.  
  12.  
  13.  
  14. public class DatabaseHelper
  15.  
  16. {
  17.  
  18.     private static final String TAG                  = "DatabaseHelper";
  19.  
  20.  
  21.  
  22.     public static final String  DATABASE_FILE_PATH      = "/sdcard";
  23.  
  24.     public static final String  DATABASE_NAME      = "mydb";
  25.  
  26.     public static final String  TRACKS_TABLE        = "tracks";
  27.  
  28.     public static final String  TRACK_INFO_TABLE        = "track_info";
  29.  
  30.  
  31.  
  32.     private static final String TRACKS_TABLE_CREATE     = "create table "
  33.  
  34.                                                                 + TRACKS_TABLE
  35.  
  36.                                                                 + " (_id integer primary key autoincrement, title text not null, description text null, created_at date not null);";
  37.  
  38.     private static final String TRACK_INFO_TABLE_CREATE = "create table "
  39.  
  40.                                                                 + TRACK_INFO_TABLE
  41.  
  42.                                                                 + " (_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);";
  43.  
  44.  
  45.  
  46.     private SQLiteDatabase      database;
  47.  
  48.  
  49.  
  50.     public DatabaseHelper()
  51.  
  52.     {  
  53.  
  54.         try
  55.  
  56.         {
  57.  
  58.             database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
  59.  
  60.                 + File.separator + DATABASE_NAME, null,SQLiteDatabase.OPEN_READWRITE);
  61.  
  62.         }
  63.  
  64.         catch (SQLiteException ex)
  65.  
  66.         {
  67.  
  68.             Log.e(TAG, "error -- " + ex.getMessage(), ex);
  69.  
  70.             // error means tables does not exits
  71.  
  72.             createTables();
  73.  
  74.         }
  75.  
  76.         finally
  77.  
  78.         {
  79.  
  80.             DBUtil.safeCloseDataBase(database);
  81.  
  82.         }
  83.  
  84.     }
  85.  
  86.  
  87.  
  88.     private void createTables()
  89.  
  90.     {
  91.  
  92.         database.execSQL(TRACKS_TABLE_CREATE);
  93.  
  94.         database.execSQL(TRACK_INFO_TABLE_CREATE);
  95.  
  96.     }
  97.  
  98.  
  99.  
  100.     public void close()
  101.  
  102.     {
  103.  
  104.         DBUtil.safeCloseDataBase(database);
  105.  
  106.     }
  107.  
  108.  
  109.  
  110.     public SQLiteDatabase getReadableDatabase()
  111.  
  112.     {
  113.  
  114.         database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
  115.  
  116.                 + File.separator + DATABASE_NAME, null,
  117.  
  118.                 SQLiteDatabase.OPEN_READONLY);
  119.  
  120.         return database;
  121.  
  122.     }
  123.  
  124.  
  125.  
  126.     public SQLiteDatabase getWritableDatabase()
  127.  
  128.     {
  129.  
  130.         database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
  131.  
  132.                 + File.separator + DATABASE_NAME, null,
  133.  
  134.                 SQLiteDatabase.OPEN_READWRITE);
  135.  
  136.         return database;
  137.  
  138.     }
  139.  
  140.  
  141.  
  142. }
Parsed in 0.056 seconds, using GeSHi 1.0.8.4



thanks ... any comments / feedback is always welcome :)
preetam_pict
Freshman
Freshman
 
Posts: 5
Joined: Tue Aug 25, 2009 2:52 pm
Location: India

Top

Re: Creating database on sdcard ...

Postby AfzalivE » Sat Nov 13, 2010 10:57 am

I'm getting the error that it's unable to open the database file, and then it makes the file but crashes after giving that error (which it shouldn't because the code to openorcreate the table is right in the next line!)

Then when I start the app again and it's unable to find the table

I'm completely new to the TryCatchFinally statement so I don't even know what it's doing.

When I shifted the createTables(); line inside the try block, it was able to make the whole database but it kept doing that (and kept telling me that the table already exists) when intents created a new instance of the DBHelper.

Any help would be greatly appreciated.

edit: Okay I modified the

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. db = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH + File.separator + DATABASE_NAME, null,SQLiteDatabase.OPEN_READWRITE);
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


to

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. db = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH + File.separator + DATABASE_NAME, null,SQLiteDatabase.OPEN_READWRITE + SQLiteDatabase.CREATE_IF_NECESSARY);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4



and now it only tells me that the table doesn't exist, which is because the createTables method is executed in the catch block.



edit2: So I found out how this TryCatch works, I shifted the createTables(db) to Try block, but still I'm getting one line telling me that it exists. Is there some other way to approach the same thing so that it checks if the db file exists, if it does, it doesn't execute the createTables method, otherwise it does? Because TryCatch executes it first and then if there's a problem it executes the other thing.
AfzalivE
Freshman
Freshman
 
Posts: 5
Joined: Sat Nov 13, 2010 10:53 am

Re: Creating database on sdcard ...

Postby AfzalivE » Sun Nov 14, 2010 2:09 am

So I was able to modify the code to my liking:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public static class DatabaseHelper {
  2.                 private SQLiteDatabase db;
  3.                
  4.                 public void open() {
  5.                         File dbDir = null;
  6.                         File dbFile = null;
  7.                        
  8.                         if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
  9.                                 dbDir = Environment.getExternalStorageDirectory();
  10.                                 dbFile = new File(dbDir, DATABASE_NAME);
  11.                         } else {
  12.                                 // code for showing error coz there's no sdcard
  13.                         }
  14.                        
  15.                         if (dbFile.exists()) {
  16.                                 Log.i("SQLiteHelper", "Opening database at " + dbFile);
  17.                                 db = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
  18.                                 if (DATABASE_VERSION > db.getVersion())
  19.                                         upgrade();
  20.                         } else {
  21.                                 Log.i("SQLiteHelper", "Creating database at " + dbFile);
  22.                                 db = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
  23.                                 Log.i("SQLiteHelper", "Opened database at " + dbFile);
  24.                                 create();
  25.                         }
  26.                 }
  27.  
  28.                 // rest of the db methods
  29. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
AfzalivE
Freshman
Freshman
 
Posts: 5
Joined: Sat Nov 13, 2010 10:53 am

Re: Creating database on sdcard ...

Postby 1ka » Sat Mar 05, 2011 1:25 pm

The problem with creating a DB on the SD card is that the Android libraries, specifically the Context class, does not, in some OS versions, allow path separators in the DB name.

You can override this behaviour and use the standard SQLiteOpenHelper by creating your own context class and using this.
Code: Select all
   class MyContext extends ContextWrapper {

      public MyContext(Context base) {
         super(base);
      }

      /**
       * We override the default open/create as it doesn't work on OS 1.6 and
       * causes issues on other devices too.
       */
      @Override
      public SQLiteDatabase openOrCreateDatabase(String name, int mode,
            CursorFactory factory) {
         File f = new File(name);
         SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(f, factory);
         return db;
      }

   }



You can use this with SQLiteOpenHelper e.g.:
Code: Select all
      
               boolean useSD = true; // put your own logic here

               Context mCtx = getApplicationContext(); // or wherever you get the context from

                mDbHelper = new SQLiteOpenHelper(useSD ? new MyContext(mCtx) : mCtx,
            useSD ? sdRoot + DATABASE_NAME_SD : DATABASE_NAME);
   
1ka
Freshman
Freshman
 
Posts: 2
Joined: Sat Mar 05, 2011 1:16 pm

Re: Creating database on sdcard ...

Postby markq9x » Sun Mar 24, 2013 11:54 am

public void close()
{
DBUtil.safeCloseDataBase(database);
}



I can not find out DBUtil class anywhere or any other workspace. Where can I find it?
markq9x
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Mar 24, 2013 11:47 am

Re: Creating database on sdcard ...

Postby 1ka » Sun Mar 24, 2013 12:15 pm

markq9x wrote:
public void close()
{
DBUtil.safeCloseDataBase(database);
}



I can not find out DBUtil class anywhere or any other workspace. Where can I find it?



The class is defined in the third code panel in the first post above.
1ka
Freshman
Freshman
 
Posts: 2
Joined: Sat Mar 05, 2011 1:16 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 20 guests