i`m still new to android programming, i usually code using Android Studio, here is my question about SQL lite
how do you accsess your database from your android phone?
where do you put your .db file ? some says i need to put it inside the assests folder
i often come through some tutorial where they put database name, create table, drop table query inside the sqllitehelper, why do they
put it again?
after you create the database in DB browser for sqlite software what did you do?
this is a question about android programming, i often come through some tutorial about crud or something related to database but i
dont see any INSERT INTO QUERY
do you need xampp for local server or what?
how do you accsess your database from your android phone?
If using the Android SDK, you'd typically have a Database Helper (subclass of SQLiteOpenHelper, if the database is a pre-existing database then SQLiteAssetHelper will copy the database file from the assets folder).
Note the above assumes the intended use of SQLite as an embedded database, if you want to share a database across multiple devices then SQLite would probably not be the database of choice (Firebase may be suitable).
.
where do you put your .db file ? some says i need to put it inside the
assests folder
As above, if it's a pre-existing database then the assets folder (in the case of SQLiteAssetHelper) will copy the database to the normally used /data/data//databases/ folder.
The asset file is compressed and read only so the App would copy the file to a usable folder, typically as above.
P.S. the file doesn't have to have any file extenstion or could have any valid file extenstion. Nothing that the file name MUST be the same as the database name, as that is the file that will be opened.
.
i often come through some tutorial where they put database name,
create table, drop table query inside the sqllitehelper, why do they
put it again?
This would typically be seen in the onUpgrade method.
What happens with the SQLiteOpenHelper subclass is that if the database doesn't exist, then the database is created and is from your perspective empty.
actually for android at least two system tables will exist;
1) sqlite_master which is a table of the tables and other items aka the schema, and
android_metadata which contains the locale
After the database is created the onCreate method is called and typically the tables (and possible other items, indexes, triggers, views) will be created using appropriate SQL generally invoked by using the SQLiteDatabase execSQL method.
The database is NOT created just by instantiating the helper an attempt (implied or explicit) has to be made to open the database, it is then that the database is checked for it's existence and created.
As such adding, for example, another table is NOT simply a case of adding more the onCreate method, as it will not run.
As such the SQLiteOpenHelper includes a means, by the way of the 4th parameter, the version number to facilitate upgrading the database (e.g. adding the new table).
The version number is stored in the database header and is checked by SQLiteOpenHelper against the value passed. If the value passed is greater than the stored value then onUpgrade is called.
if the value is less then onDownGrade is called and there which will result in an exception should the onDownGrade method not be coded in the sub-class of SQLiteOpenHelper.
Often, onUpgrade will DROP (delete) the tables and then call onCreate to create them and that is what you have seen (probably).
Note ANY EXISTING DATA WILL BE LOST
to not lose data you would use more complicated/in-depth code.
.
after you create the database in DB browser for sqlite software what
did you do?
Copy the file into the assets/database folder (if using SQLiteAssetHelper) or sometimes just into the assets folder.
You may have to create the assets folder and therefore also the database folder.
I would personally recommend closing DB Browser, opening it again, checking the data is as expected and then closing it again before copying the file.
this is a question about android programming, i often come through
some tutorial about crud or something related to database but i dont
see any INSERT INTO QUERY
The android SDK has many convenience methods, that build the SQL on you behalf, including (if the parameters are used accordingly) escaping arguments as required and protecting against SQL injection. Use of the convenience methods is recommended.
Here's an example of what you could instead see (to INSERT) :-
public long addUserReg(long studentId, String course) {
ContentValues cv = new ContentValues();
cv.put(COL_USERREG_STUDENT_ID,studentId);
cv.put(COL_USERREG_COURSE,course);
return mDB.insert(TBL_USERREG,null,cv);
}
The method is passed two values.
A ContentValues object is instantiated (consider this as a list of tuples with a key (name which correlates to the respective COLUMN name) and the value to be placed into that column).
A COLUMN value pair is added to the cv (an instance of a ContentValues object) COL_USERREG_STUDENT_ID holds the column name.
Another value pair is added to cv.
note you can consider cv as intelligent e.g. if you pass a byte array then the respective code for adding a BLOB is generated.
The insert convenience method is invoked, assuming COL_USERREG_STUDENT_ID resolves to studentid and COL_USERREG_COURSE resolves to course and TBL_USERREG resolves to course_table then the resultant SQL would effectively be INSERT OR IGNORE INTO course_table (studentid,course) VALUES('1','3') (assuming the studentid value passed was 1 and the course value was 3).
do you need xampp for local server or what?
No (I beleieve XAMPP is for Apache (server) MYSQL not SQLITE PHP along with PHPMYADMIN) but see above about SQLite being intended as an embedded database not a client/server type database. See Appropriate Uses For SQLite
Related
My Android App use SQLite database as an asset.
I deliver a .db file in /assets directory to provide both read-only tables and read/write tables.
When the user uses the App, it's datas are stored in read/write tables.
Sometimes I need to modify the schema of the database by adding a column in a table for example.
So I need to deliver a new .db file in the release.
If i do this the problem is that the user loses his data.
Is there a way to export/import the user data when he updates the App on his devise ?
I didn't find anything about this issue on the Web.
The standard way of handling this is, assuming that you are using (extending) the SQLiteOpenHelper class, is to utilise the version number in conjunction with overriding the onUpgrade method to ALTER the schema of the existing database, thus retaining the existing data.
version int: number of the database (starting at 1); if the database is older, onUpgrade(SQLiteDatabase, int, int) will be used to upgrade the database; if the database is newer, onDowngrade(SQLiteDatabase, int, int) will be used to downgrade the database
The SQLiteOpenHelper (or if using SQliteAssetHelper which extends SQLiteOPenHelper), when opening the database (if it exists) compares the coded version number against the version number stored in the header of the database. If the coded version number is greater than the stored version number then the onUpgrade method is called which is passed three parameters,
the SQLiteDatabase
the old (stored) version number as an int
the new (coded) version number as an int
You would typically check old and new and have code that makes the changes (ALTER TABLE or an alternative if the limitations of the ALTER TABLE force to do something like rename the original table, CREATE the changed table with the correct name, copy the existing data from the original table to the new table and finally DROP the renamed original table )
If the App is installed for a new user then that is when the asset will be copied. Thus covering both scenarios.
If not using an extension of SQLiteOpenHelper, then you can mimic using the stored version number by reading 4 bytes at offset 60 and comparing this to a coded version or even by copying the asset and getting the asset's version number, which would be managed with whatever tool you use to maintain the asset (e.g. via the user_version PRAGMA)
SQLite Database Header
Am making an app, which contains a database in the assets folder, say the database as db1. When I open the app, am again creating a db using the API'S. Both the db names are similar.
When copying the db I see that the db1 created using api`s is getting over written by db1 in assets. Here I have a solution that first to copy the db from assets then followed by open the same in java.
In assets folder db it contains tables namely A,B,C and the db created using code am making tables A,B,C,D,E,F.
Now what I want is:
When I give an upgrade for the app, I cannot blindly copy the database in the assets will overwrite all the existing data in the db1( All tables ). I want to know can I copy the database with some other name say db2. Then open and copy all the tables in db2 and overwrite those tables in db1 with the newly copied tables; i,e tables A,B,C tables in db1 has to be replaced with with the tables in db2.
I want know if it is possible or not? If possible some give me some samples are references.
Upgrading the app doesn't force the database to be upgraded. So unless you actually change the DB's version number, that you pass to the constructor of SQLiteOpenHelper, the old database won't change.
If you do change the DB's version number because you have changed the database schema, then you have to override the onUpgrade method, copy the necessary information from the existing DB to some temporary location (or memory), copy the DB file from assets and insert the data back.
I am using Mozila extension SQLite Manager to create a database and use it in my app.
I created a table named test.
Then I read the data using the following line and it succeed.
My question is: "testing" should be the name of table right?
But when I use "select * from test" it reads nothing.
Initially I named the table as "testing".
Cursor cursor=dbHelper.QueryData("select * from testing");
It seems that you are indeed asking how to ship a prepopulated database in your app - or I don't see how could you use it in your app.
Now the steps are very simple:
1. If not already existing, copy the database from the assets folder to the data path.
2. Use it from there.
You will find some related posts on SO. For instance: here
and here
Now, if you changed your table name (!!), you must respect the new table name.
The new database must be copied in the assets folder, but the old database now must be removed from the the data path.
This is because otherwise the app will find the database and won't copy the new one over it.
Which is the desired behaviour: you don't want your app to copy the database each time it starts, but only once - since it's a time consuming operation.
I have an android application that relies on a sqlite database, and use OrmLite to access my DB.
Instead of OrmLite creating the tables I rely on downloading the database from a central server as the user will often want to "sync" things. Currently I don't have the fancy sync code written so the app replaces the db. The steps are:
1 Download the latest SQLite db file from the server, as a zip
2 Expand the file to produce a database.sqlite file in a temporary folder
3 Deletes the contents of a data folder, which contains the live database.sqlite file
4 Move the database.sqlite file from the temporary folder to the data folder.
The problem is that the new database file seems to get ignored and DAO queries I run return old data. The only way to show data from the new version of the DB is to restart the application.
To test things I created a table with a timestamp that records when the database was generated, each time you request a new copy of the sqlite db from the server this is updated. I have a fragment that displays this time so you know how fresh your data is. In the fragments onResume method I make a call to the DAO to get the timestamp and put value on screen. I've stepped through this and I see the call to the DAO but the value that comes back is from the old, now deleted, db. Restart the app and the correct value is shown.
So my question is, if I replace the underlying sqlite db file that stores my database, how can I tell ormlite to pick it up or refresh the connection or whatever it has to do???
I tried calling clearObjectCache on the DAO, made no difference.
I have an app that creates a database and do some stuff. I am wondering if i upload a new db to a server and download it to the exact folder where the older one exists it will be overwritten and i am good to go? Or there will be a problem. Assuming it has the same name, same column names, etc. Of course i am reffering to sqlite.
In Android, when performing a database update you should be using onUpgrade inside of the SQLiteOpenHelper. One way of doing this is to download text files that include the sql instructions needed to modify the current database or update rows with new data. The reason you have to do this is because Android will only create the database once. After the initial creation the call to onCreate for the database will not occur.