Dealing with Databases

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

Dealing with Databases

Postby anaeem » Mon Jun 14, 2010 4:26 pm

I have a database that is 5 MB. Where should I place this database? I tried putting database in Assets folder and then
copy it to data/data/com.example.android/databases folder using following code
Code: Select all
private void copyDataBase() throws IOException{

       //Open your local db as the input stream
       InputStream myInput = myContext.getAssets().open(DB_NAME);

       // Path to the just created empty db
       String outFileName = DB_PATH + DB_NAME;

       //Open the empty db as the output stream
       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();

    }


Problem is if Database is 2 MB or greater. It exceeds the size and throws an exception. Is there an example online regarding where to place prepopulated big databases? Any help is highly apprecaited.

I followed following tutorial for copying database in case someone is interested.

http://www.reigndesign.com/blog/using-y ... lications/
anaeem
Experienced Developer
Experienced Developer
 
Posts: 85
Joined: Mon May 03, 2010 6:15 am
Location: Texas

Top

Re: Dealing with Databases

Postby vengeance_mj » Mon Jun 14, 2010 9:58 pm

Is your database a .csv file? Then you can probably import everything into a table by using sqlite console. Correct me if I understood u incorrectly
vengeance_mj
Senior Developer
Senior Developer
 
Posts: 174
Joined: Thu Oct 29, 2009 2:32 am

Re: Dealing with Databases

Postby anaeem » Mon Jun 14, 2010 10:04 pm

No my database is not a CVS file. I created database with SQLite browser and pre-populated data in tables. Pardon my ignorance, but I think SQLite doesn't create CVS file. right?
anaeem
Experienced Developer
Experienced Developer
 
Posts: 85
Joined: Mon May 03, 2010 6:15 am
Location: Texas

Re: Dealing with Databases

Postby anarche » Mon Jun 14, 2010 10:23 pm

I haven't tried this, but apparently you are on the right track

http://www.reigndesign.com/blog/using-y ... lications/
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Dealing with Databases

Postby anaeem » Mon Jun 14, 2010 10:29 pm

Following link explains that Android has limitation on Files of size > 1MB
I was hoping if I could bypass chopping my DB into multiple files.

http://www.chriskopec.com/blog/2010/mar ... databases/
anaeem
Experienced Developer
Experienced Developer
 
Posts: 85
Joined: Mon May 03, 2010 6:15 am
Location: Texas

Re: Dealing with Databases

Postby nicholas.hauschild » Tue Jun 15, 2010 12:00 am

If the database is too large, you could place the database on a server somewhere and access it remotely. Of course the issues with this are requiring the Full Internet Access Permission, and, of course, having to rely on data connections to access the database.

But who knows, maybe this will be just fine for you.

Good Luck
-Nick
nicholas.hauschild
Master Developer
Master Developer
 
Posts: 310
Joined: Fri Dec 04, 2009 4:50 am

Top

Re: Dealing with Databases

Postby vengeance_mj » Tue Jun 15, 2010 12:30 am

well could you store the values with which you populated the tables in the database in the [comma seperated file] .csv file . Since even I had the same problem while trying to use WeatherBug API which required cityCodes and trying to do a lookup for a cityCode on the net was making the app go slow.
I got all the values they had in a .csv file and just imported that in databases package using sqlite console.
Hope that helps
vengeance_mj
Senior Developer
Senior Developer
 
Posts: 174
Joined: Thu Oct 29, 2009 2:32 am

Re: Dealing with Databases

Postby anaeem » Tue Jun 15, 2010 12:40 am

My application is sort of ebook. So creating CVS is kind of hard as line feeds lead database to think start of a new record
anaeem
Experienced Developer
Experienced Developer
 
Posts: 85
Joined: Mon May 03, 2010 6:15 am
Location: Texas

Re: Dealing with Databases

Postby anaeem » Tue Jun 15, 2010 5:08 am

okay here is what I have done. I took my database file and using hjsplit utility split it in 500K files. Placed them in Assets folder and on run time created a DB

myInput = myContext.getAssets().open("db1");
while ((length = myInput.read(buffer))>0){
if(Constants.Debug){Log.d("CopyDataBase", "writing database");}
myOutput.write(buffer, 0, length);
myOutput.flush();
if(Constants.Debug){Log.d("CopyDataBase", "copied first file");}
}

and so on for other files.

I hate development and love it at the same time :)
anaeem
Experienced Developer
Experienced Developer
 
Posts: 85
Joined: Mon May 03, 2010 6:15 am
Location: Texas

Re: Dealing with Databases

Postby phix » Tue Jun 15, 2010 6:11 am

Nice.. thank you for putting that up. I'm sure I'll run into that eventually :)

I hate development and love it at the same time


Word.
Posting questions without code is like going to the doctor and saying "it hurts, fix it."
phix
Master Developer
Master Developer
 
Posts: 230
Joined: Mon Mar 15, 2010 10:37 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 16 guests