I'm currently working on the database part of a project where I have to merge the contents of two databases and I would like to ask you if there exists a simple API method
within the Android API/SDK itself that dumps me a database to a SQL text file.
Actually I found no hint in the API documentation about such an implementation myself. And I really doubt there is a single line method somewhere buried behind the curtain.
However I've already made a workaround using the sqlite3 shell tools of Android Linux by invoking:
String[] cmd = {"/system/bin/sh", "-c", ..., "sqlite3 ..."};
Runtime.getRuntime().exec( cmd );
Where I have two choices, a) pipe it directly to an output file or b) write the file using BufferedOutputStream.
Nevertheless, most likely due to compatibility issues am I asking you of a more convenient way within the API itself, rather than using the critical shell trick within the App.
I am also pretty much interested in any fast & pretty clues about merging two databases using Android's database methods.
Thanks.
There's nothing built in to the API that will help you. You could query, and write the INSERT statements yourself. There's a blog entry (http://mgmblog.com/2009/02/06/export-an-android-sqlite-db-to-an-xml-file-on-the-sd-card/) , on building an XML file from the results of your query. A few tweaks, and you coule build your own dump file.
You don't say what kind of merge you need to do, but you might be able to use ORMLite, by doing something like override the equals method on your model objects to compare the records and/or combine data from certain fields (assuming the schema is the same), without having to write a lot of SQL.
Related
I am not new to Android or Java but very new to Databases. I have been practicing with SQLite in Android and have now become completely stuck.
I want to completely remove the current database that I have been using in my app and create a new one with more columns, different types of columns, and etc...
I have tried "context.deleteDatabase()" which appears to delete the database but then after that I uninstall the app and re-install it with the myDatabaseHandler java class file having all the new columns and changed added to the file.
The code compiles and runs fine until I try to add info to the database, I receive an error cannot find columns, the error refers specifically to the columns that I added.
Why does it seem that I cannot start over completely. It seems like the structure or schema of the database won't change.
So how do I eliminate any evidence of the databases that I was practicing and messing around with, and just start completely new? After all, thats what practice is, you don't know what you are doing and you learn by making mistakes. So now I need to completely wipe away the mistakes, not upgrade just to make alterations.
Upgrading the database seems to provide an avenue for achieving close to what I want, but ultimately is way more involved and confusing for what I need when I just need to start over with a freshly created databases that has more columns.
The SQLiteOpenHelper class goes to great care to keep the old database to allow you to upgrade it in place.
If you're not interested in the old data, just change the file name. Then it is guaranteed that there is no old version. (You still have to call deleteDatabase() to get rid of the old file, but now that call cannot conflict with any accesses to the new file.)
Using the ADB tools from the SDK/platform-tools folder can help to remove all data (including the database schema)
./adb.exe -s shell pm clear <your app's package name>
will remove all the data associated with your app. Then you install the new version of the app, it will use the new database schema.
I am using Android SqliteAssetHelper library (https://github.com/jgilfelt/android-sqlite-asset-helper)
I have shipped my database using this library and the database contains some records on table1.
Now I want to update my app with a new database with additional records which should be inserted in the one which i already shipped. I am not sure how exactly to write the SQL scripts for the upgrade since the schema is same for both the databases. Did anyone try this?
After comments by the op in other answers, it was clarified that there are two databases to merge together. The op wants to know if there is a convenient way to merge two databases together with the SQLiteAssetHelper library.
Unfortunately there isn't a direct way to do that because that library also uses the same Android pattern of running a script to modify an existing database.
The workaround is to transform the second database (set of 50 records) into 50 INSERT statements that will get put next to the existing 50. (There are various tools all over the internet to simplify that step so you don't have to do it by hand.) So as long as the business logic can work with them all together they can all go in the original table if the schemas are the same; or if you need them separated, use the 50 INSERTs still still but have them INSERT to a different table name instead.
Then, once you have these 50 INSERT statements with the data of the 50 new rows, put the statements in an upgrade script and you can follow the standard library documentation on how to get that script to run via this library.
You can make this happen by using sqldiff to find the differences between an old DB and a new one.
You call sqldiff on your two databases and pipe the output into a file that conforms to SQL Asset Helper's upgrade format (i.e. <database_name>_upgrade_<from_version>-<to_version>.sql).
So, the whole thing would be sqldiff database.db database_new.db > database.db_upgrade_1-2.sql
Then just make sure that .sql file is in the assets/databases directory and change the version numbers in your Java code (in the example case, from 1 to 2).
I have 9000 files in the memory card, I have created an application that read each file and parse it in to a String and put that String in a HashMap in order to avoid repetition,and save the names of only unrepeated files, but that operation is taking about 7 to 10 seconds.. is there any way to make that faster.. I tried to find better method but I didn't find anything...
Object : get the names and Strings of all the unrepeated files..
Is there any way to do that faster using java, new IO, or NDK, is it worth to install NDK and try that using C language and pointers..
I appreciate any help..
Its obviously that even just loading of 9k files takes some time. Imagine there a lot of android devices and there are many devices which are not as fast as device which you are using to test. So the operation gonna take even more time on such devices. The problem is a numerous of small files. Maybe you should pack it in one, lets say a JSON or XML? Reading of 1 file is more faster and reliable.
I'd like to create a project which generates a sqlite database, which will eventually be used by an android application. I'd like to create this project as a standard java application, so I can hook it up to a build script etc. What's a good way to go about doing this, so that the sqlite database I output is conformant with the way android sqlite classes expect to have it in?
I could create this util project as an android project, and then I have access to all the sqlite classes, but the output sqlite file would live on an emulator instance, right? And I'd have to fire up an emulator etc whenever I wanted to run the util, ugh.
Thanks
As others have suggested, I wouldn't build a project for it, I'd find one of the existing utilities out there and create the DB that way. I use SQLite Expert.
Despite what Seva said, there are some things you have to do to make it usable by android. It's readable in any state, but if you want the framework to be able ot make use of it like intended (to populate listviews and other widgets), it has to have certain things.
1) The database must contain a table called "android_metadata"
2) This table must have the column "locale"
3) There should be a single record in the table with a value of "en_US"
4) The primary key for every table needs to be called "_id" (this is so Android will know where to bind the id field of your tables)
Then you put the DB in your assets folder and copy it to your apps data directory on startup.
A good link for this process is here.
Why do you want create a separate Java project to create a SQLite database? There are graphical shells over SQLite out there. I personally like SQLiteStudio.
There's nothing special about the way Android accesses them - SQLite is SQLite, the database format is the same on every platform. Create a new database file, create some tables in it, insert some data, then place it into an Android project and play with it.
can create you other as libray project and can attach it with your project...libaray project may be an android or simple java project as per your need...
Note: use the version of sqlite that comes with the SDK -- it's been modified slightly. If you use the off-the-shelf sqlite3 commandline tool, the databases it generates are incompatible with Android.
I want to monitor the contents of my database during debugging, i.e, I want to see all the rows in the tables, and see the updates as and when they take place. Currently, I'm using android.util.Log. But, if there is a better way to do it, that would be nice. Thanks.
The database file is in the /data/data/package_name/database
You can:
use adb shell, use command "sqlite3 your_database" to manage the database, also there exists a tool in sdk called sqlite3 (a command line tool)
push the database file out, use a sqlite browse tool to check for details (tools such as sqliteadmin, sqlitebrowser)
I asked the same question before: Logging SQL statements using SQLiteDatabase . Seems like there's no decent logging tool for SQLite on Android. It is a real shame.