Populating Content Provider w/Initial Values From a File

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Populating Content Provider w/Initial Values From a File

Postby rmanromga » Thu Oct 02, 2008 5:40 am

I am trying to use a content provider using an SQLiteDatabase and I have an sql file as a resource/asset that I want to use to populate the database once the database is created....My problem is that I have a static SQLiterOpenHelper class nested inside my DatabaseManager class (which extends the ContentProvider class)..because of this, the application context is not visible inside static SQLiteHelper class and so I cannot open my sql file in the DatabaseHelper class. I have to open it there because the values form my sql file have to be loaded into the database in the onCreate() function of the SQLiteHelper class right after the tables are loaded... because of the fact this is a static nested class, I have no access to the application context am I doing this wrong or is there a better way to do this

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class DatabaseManager extends ContentProvider {
  2.  
  3.         private static final String TAG = "DatabaseManager";
  4.  
  5.  
  6.  
  7.         private static final String DATABASE_NAME = "fitroid.db";
  8.  
  9.         private static final int DATABASE_VERSION = 1;
  10.  
  11.         private static final String DATABASE_STARTER_FILE = "input_" + DATABASE_VERSION + ".sql";
  12.  
  13.        
  14.  
  15.        
  16.  
  17.        ...
  18.  
  19.        ...
  20.  
  21.  
  22.  
  23.         private static final UriMatcher sUriMatcher;
  24.  
  25.  
  26.  
  27.         /*
  28.  
  29.          * This class helps open, create, and upgrade the database file
  30.  
  31.          */
  32.  
  33.         private static class DatabaseHelper extends SQLiteOpenHelper {
  34.  
  35.  
  36.  
  37.                 DatabaseHelper(Context context) {
  38.  
  39.                         super(context, DATABASE_NAME, null, DATABASE_VERSION);
  40.  
  41.                 }
  42.  
  43.  
  44.  
  45.                 @Override
  46.  
  47.                 public void onCreate(SQLiteDatabase db) {
  48.  
  49.                        
  50.  
  51.                        
  52.  
  53.                        
  54.  
  55.                         db.execSQL(createYourFirstTableString());
  56.  
  57.                        db.execSQL(createYourSecondTableString());
  58.  
  59.                        db.execSQL(createYourThirdTableString);
  60.  
  61.  
  62.  
  63.              /****   Test File Openning here ***************
  64.  
  65.                         //final String TESTSTRING = new String("Hello Android for Invigor");
  66.  
  67.                         //FileOutputStream fOut = openFileOutput("samplefile.txt", MODE_WORLD_READABLE);
  68.  
  69.                        
  70.  
  71.                        
  72.  
  73.                        
  74.  
  75.                          How can I place such a call here !!
  76.  
  77.                        
  78.  
  79.  
  80.  
  81.                  getContext().getAssets().open(DATABASE_STARTER_FILE)    // DOES NOT WORK - due to static class
  82.  
  83.  
  84.  
  85.                   I need to load values from my sql file to the database here !!
  86.  
  87.                        
  88.  
  89.                         ******* End Testinf file openning   *****************/
  90.  
  91.                        
  92.  
  93.                        
  94.  
  95.                 }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
Last edited by rmanromga on Sat Oct 04, 2008 9:06 pm, edited 1 time in total.
rmanromga
Developer
Developer
 
Posts: 43
Joined: Thu Dec 20, 2007 6:21 am
Location: Baltimore, MD USA

Top

Postby ninor » Thu Oct 02, 2008 6:25 am

One idea:

Create a static variable to hold a "pointer" to the app's context.
Assign the context to the var in the onCreate of the app.
Then you can try to access the static variable inside the static class.

Hope this helps.
Image AndDev: Your Android Development Community / Tutorials | Here's my Basic ToolKit
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Postby rmanromga » Sat Oct 04, 2008 9:02 pm

Thanks ninor, your suggestion worked... Here is how I did it folks, for any of you who may have a similar problem.. I have also shown the code of how to open the sql file. You will have to create a text file with the sql INSERT statements and put this text file in the /res/raw folder of your project. This creates a resource that you can then use context.getResources().openRawResource(R.raw.your_sql_file) to open up an InputStream to the file which you can then read from.....

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class DatabaseManager extends ContentProvider {
  2.  
  3.     private static final String TAG = "DATABASEMANAGER_TAG";
  4.  
  5.  
  6.  
  7.     private static final String DATABASE_NAME = "myDB.db";
  8.  
  9.     private static final int DATABASE_VERSION = 2;
  10.  
  11.     private static final String DATABASE_STARTER_FILE = "insert_" + DATABASE_VERSION + ".sql";
  12.  
  13.     private SQLiteDatabase mDB;
  14.  
  15.  
  16.  
  17.     public static Context contextPointer; //This is the pointer that will hold a reference to the context for us
  18.  
  19.  
  20.  
  21.     private static final UriMatcher sUriMatcher;
  22.  
  23.  
  24.  
  25.     private static class DatabaseHelper extends SQLiteOpenHelper {
  26.  
  27.  
  28.  
  29.         DatabaseHelper(Context context) {
  30.  
  31.  
  32.  
  33.             super(context, DATABASE_NAME, null, DATABASE_VERSION);
  34.  
  35.         }
  36.  
  37.  
  38.  
  39.         @Override
  40.  
  41.         public void onCreate(SQLiteDatabase db) {
  42.  
  43.             Log.i(TAG, "DEBUG: calling onCreate Function in SQLiteHelper function");
  44.  
  45.             db.execSQL(createYourFirstTableString());
  46.  
  47.             db.execSQL(createYourSecondTableString());
  48.  
  49.             db.execSQL(createYourThirdTableString);
  50.  
  51.             insertValues(db);            //Function to initally populate the database
  52.  
  53.         }
  54.  
  55.  
  56.  
  57.          .......
  58.  
  59.          .......
  60.  
  61.          .......
  62.  
  63.          
  64.  
  65.          //Function that initially populates the database
  66.  
  67.  
  68.  
  69.         public void insertValues(SQLiteDatabase db) {
  70.  
  71.             String CurLine = "";
  72.  
  73.             try {
  74.  
  75.  
  76.  
  77.                 //make use of context pointer to get the file
  78.  
  79.                 InputStream inStream = contextPointer.getResources().openRawResource(R.raw.insert_2);
  80.  
  81.                 DataInputStream din = new DataInputStream(inStream);
  82.  
  83.  
  84.  
  85.                 // read each line of the sql file and insert it into the db
  86.  
  87.                 while ((CurLine = din.readLine()) != null) {
  88.  
  89.                     db.execSQL(CurLine);
  90.  
  91.                 }
  92.  
  93.             } catch (IOException e) {
  94.  
  95.                 Log.e(TAG, "error reading insert values");
  96.  
  97.             }
  98.  
  99.        }
  100.  
  101.    }// END SQL OPEN HELPER
  102.  
  103.  
  104.  
  105.    private DatabaseHelper mOpenHelper;   //The Database Helper we just defined above
  106.  
  107.  
  108.  
  109.    @Override
  110.  
  111.     public boolean onCreate() {        //onCreate for the DatabaseManager implementing ContentProvider
  112.  
  113.  
  114.  
  115.         mOpenHelper = new DatabaseHelper(getContext());
  116.  
  117.         contextPointer = getContext();                //Set a pointer to the context so that the
  118.  
  119.                                                   // static class SQLiteHelper will have access to it
  120.  
  121.         mDB = mOpenHelper.getWritableDatabase(); // The database is never created until
  122.  
  123.                                                  // getWritableDatabse is called
  124.  
  125.         return true;
  126.  
  127.     }
  128.  
  129.    
  130.  
  131.     .....
  132.  
  133.     .....
  134.  
  135.     .....
  136.  
  137.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


Thanks guys...
rmanromga
Developer
Developer
 
Posts: 43
Joined: Thu Dec 20, 2007 6:21 am
Location: Baltimore, MD USA

Postby ninor » Sun Oct 05, 2008 12:30 am

Thanks for the snippet!
Image AndDev: Your Android Development Community / Tutorials | Here's my Basic ToolKit
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 4 guests