Completely erase all traces of database schema for practice in Android - android

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.

Related

Porting a pre-populated sqlite database in Ionic

Technologies used for the app: Ionic, Capacitor, Angular and SQLite
The requirement is to use a prepopulated SQLite database in the app. The methods that I found over the internet across various blogs and forums mentioned running a SQL script to initialize the DB as a pre-population step, but this brings about an unwanted overhead time to start querying the DB. As in the user needs to wait for a minute or so for the SQL Script to run completely every time, even if the app exists.
This issue became a roadblock because when the need became to port a large-sized database, it was no longer feasible to write SQL scripts as the overhead time increased drastically. Also, a bigger problem is, in Android studio, Java started throwing Heap Memory exceeded error.
A solution that I tried was to port the database directly as an asset of the app so that it can be shipped along with the app. But, while placing the database file in src/assets/, every build of the app generates www/ to create a web app that can be wrapped in a native view. The builder and compiler delete all the contents of the previous build in the www/ folder, we must understand that the capacitor looks for the database file at www/ and not in www/assets/, but we can't place the database file at www/ as it gets deleted on every build.
Link to existing issue on using the sqlite extension
maybe my experience can help you
RULES FOR DEVELOPMENT
1
database1 of bundle ( Android APK) from /assets must be static and not for update. because they can be changed when the application will be reinstalled on you version
2
you must to create new database2 - local , on the device , programmatically - to store a user's data. you can affill this database2 with data from database1
3
when you will install new version of application: database1 one may be changed, but the database2 becomes constant
the same situation is in browser: programmatically created database not disappear from session to session.
I check - it works in my application...

Android SqliteAssetHelper - merging database tables from asset with the existing one

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).

Sqlite database manual changes are been ignored in queries

I have begun to programming in Android with Eclipse a little application who has a tiny sqlite database. As you know, sqlite is really easy to use. I have been using sqlite for many years, but now I have found somethig unusual and disappointing:
when I try to update manually my database (e.g. using SQLiteman or another SQLite GUI), changes are reflected when I make a simple SELECT * FROM... in SQLiteman. But when I execute my application and make another simple SELECT * FROM... the new row is absent! I doesn't appear!
-I have cleaned my solution and I have tried to update many times with "Import..." without any result.
-I suspect that it may be something related to an internal index not updated, but I'm not sure.
My code is minimal, as you will see:
String query = "Select _id, name FROM mytable";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
int totalRows=cursor.getCount(); //<---- This only shows the older total!
What's wrong with this?
Addendum:
I'm not using an emulator and my device is already rooted. But I can't see my database when I go to the DDMS in Eclipse (but this is another question).
I'm editing the database in my PC and then I import it to Eclipse. I have edited too directly in Eclipse, but without no luck.
Thank you
I guess you must be running the older version of your database. Just try to uninstall the old application from the emulator or device and install the new updated version.
I am sure it will reflect.
Whenever you change your database explicitly at that time to get it reflected into application you need to upgrade the version of your database otherwise it will not affect your database. Or you need to uninstall the old version of your application and install updated application that will reflect the SQLite.
Most likely, you are not doing a "COMMIT" on the new data, so it isn't persisting in the database in Android

Using a pre-built db4o database in android app

Im REALLY struggling to get a pre baked db4o database to work inside my android app.
If i include it in the /res/raw folder then read it, it doesn't work.
If i copy it to the app_data or sdcard then try and read it, it will act as if im opening a new database and provide me with 0 entries on queries.
I was previously just creating all my entries when the app was first opened but as the db grew, so did the creation time to unsustainable levels.
I would really, really appreciated any help you can give me. It would be perfect if i can include my pre-populated db4o file in the app.
Additional Info:
I have double checked the db4o file with ome and theres no issue with the db creation on my local machine.
Thanks in advance.
If any one falls into the same trap as me here's the answer:
On android the class is injected into the db4o db with the package name prefixed to the class name by defualt. So accessing from another application will look be looking for +.

Changing package name but keeping database on Android

I've created an Android application which has a certain package name that I've been using personally for months now. I'm about to release it on the market, and I have to change the package name. This cannot be avoided.
My issue is that the application has an SQLite database attached to it that I want to keep, but I know if I change the package name, it'll install as a separate application and I'll have to restart my database, which would take a very long time.
Is there a good way to change a package name while maintaining the SQLite database? Or at least moving the database easily? This will just be for my own phone since it hasn't been released to the public yet.
Step #1: Add a backup/restore function to your app, that copies the database to/from the SD card. Be sure your SQLiteDatabase and SQLiteOpenHelper objects are closed first.
Step #2: Install a copy of this app, built with the old package, to your phone, and use it to back up your database.
Step #3: Install the production copy of this app to your phone and use it to restore your database.
For anybody wanting to do the same thing, I've written out how to do backup/import/restore of a database and included a whole class for doing it at this link:
http://www.hxcaine.com/blog/2010/09/14/backing-up-importing-and-restoring-databases-on-android/

Categories

Resources