Importing/Exporting Databases from/to SDCard

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

Importing/Exporting Databases from/to SDCard

Postby aerispaw » Wed Oct 14, 2009 6:30 pm

Hello.

I have an application using an internal SQLite database.
Now I want to replace it with a database file stored on the SDCard.

After copying the file using standard input/output methods I tried to open a connection, but got an exception that table xyz already exists, so it looks like my SQLiteOpenHelper rerun OnCreate even though the database was already present.
The database was an exact copy of a database created by the helper, I just pulled it down, added a few dummy entries and pushed it back to the SDCard where I used my import method to copy it to my package/database directory.
What am I missing? How do I correctly overwrite an existing database with an external one without selecting and inserting row by row?



Also, how do I export an internal database to the SDCard? I tried the normal IO methods, but it looks like I don't have write access to /sdcard/.
I used
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. File ofd = Environment.getExternalStorageDirectory();
  2.  
  3. ofd.canWrite();
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

but it always returns false.
Is there any way to get write permission for the SDCard?
The SDCard is present and mounted, I can read files just fine.


Thank you.
User avatar
aerispaw
Freshman
Freshman
 
Posts: 2
Joined: Wed Oct 14, 2009 5:20 pm
Location: Austria

Top

Postby padde » Wed Oct 14, 2009 9:11 pm

you need write permissions in android manifest to write to SD card.

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
  3.  
  4.  
Parsed in 0.000 seconds, using GeSHi 1.0.8.4
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby Emmanuel7 » Wed Oct 14, 2009 10:09 pm

I have a tutorial on how to embed a database in an application on my blog here :
http://androidblogger.blogspot.com/2009/05/how-to-ship-application-with-pre-baked.html

But at the time of the writing, there was no write permission for the SD card !
Emmanuel7
Senior Developer
Senior Developer
 
Posts: 164
Joined: Fri Dec 12, 2008 2:17 am
Location: Paris

Postby nmc » Thu Oct 15, 2009 12:36 am

But at the time of the writing, there was no write permission for the SD card !

You need this when using 1.6 as target
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby aerispaw » Thu Oct 15, 2009 12:03 pm

Hi, thanks for your answers.

padde wrote:you need write permissions in android manifest to write to SD card.
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
  2.  
Parsed in 0.000 seconds, using GeSHi 1.0.8.4

Ouch, I actually had this in the android:permission tag in my application tag but not in uses-permission, thanks for that.


I could solve the import issue by adding the IF NOT EXISTS clause when creating the tables in onCreate in my SQLiteOpenHelper, shame on me for not having thought of that earlier.


Everything is working fine now, thanks.
User avatar
aerispaw
Freshman
Freshman
 
Posts: 2
Joined: Wed Oct 14, 2009 5:20 pm
Location: Austria

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 4 guests