3

Is there a way to replace a complete SQLite DB? I'm looking at building an app which holds a lot of data and I need to be able to update it. Fortunately I am able to verify that the DB structure won't change, just the data. Is there a clean way of doing this?

-- EDIT--

Apologies - I need to add more detail:

In order to update the data in the app, the plan is to just replace the DB which will be grabbed from a server somewhere. So I won't be using the onUpgrade or adding data manually - I want to dump the existing file and copy a new file in to replace the old one.

I think a DB will normally be stored in "/data/data/YOUR_PACKAGE/databases/", do I have access to this directory? In which case can I jst download the file from the server and copy it over the top (obviously making sure that nothing is accessing the file first etc)

Martyn
  • 16,432
  • 24
  • 71
  • 104
  • 2
    Well, a SQLite database is just a file. So, if you want to start with a new database, then start a new file... –  Sep 07 '11 at 15:46

3 Answers3

2

This question is the same as mine : Can I download an SQLite db on /sdcard and access it from my Android app?

File dbfile = new File("/sdcard/mydb.sqlite" ); 
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
System.out.println("Its open? "  + db.isOpen());
Community
  • 1
  • 1
Martyn
  • 16,432
  • 24
  • 71
  • 104
0

in the SQLiteOpenHelper class is an onUpgrade method, and if a new Database Version is passed through the constructor that method is called. In here you could drop the database and create a new one, or you could alter and create tables.

manno23
  • 184
  • 1
  • 9
0

Not clear what you're trying to do.

If the application is read-only and uses an SQLite database created outside the application then you can simply replace the SQLite database file.

If you want to replace individual tables within a database while leaving your applications writeable tables untouched, you can delete the data from the existing table and use INSERT INTO to get data from a different database. You can also use the supplied command line tool and the ".import" command to load from CSV files.

Larry Lustig
  • 49,320
  • 14
  • 110
  • 160