SQLite query data - android

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.

Related

Question about android programming using SQL lite

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

how to upgrade database version with new rows with SQLiteAssetHelper

i have developed a tutorial app in which i have saved more than 500 questions and answers.
their is one more table called favourites. which is for user input.
now, i want to update my app with new questions and answers.
but i dont want to erase the data of favourites table (in case, user has marked some questions favourites, so those questions should not be erased from favourites)
so how can i do it?
because, i have used SQLassethelper library for database connectivity.
my old db contains:
data table(static table)
favourites table(local table)
so, according to sqliteassethelper documentation i added my new db:
that contains: updated data table. i didnt inserted favourites table here coz it will be created in script file.
and stored that db in assets>>databases folder.
then
i created a script fild
db.db_upgrade_1-2.sql
alter table "favourites" rename to "favourites_tmp";
create table "favourites" (
"id" Integer not null primary key autoincrement unique,
"question" text,
"answer" text,
"category" text,
"catid" integer
);
insert into "favourites" ("id","question","answer","category","catid") select from "favourites_tmp" "id","question","answer","category","catid" from "favourites_tmp";
drop table "favourites_tmp";
so i think here favourites table will be created with old data.
but
when i run the project, it says: no such tabld favourites.
The documentation tells you to
create a text file containing all required SQL commands to upgrade the database from its previous version to it's current version.
You can use any SQL commands, not only ALTER TABLE, but also INSERT.
The database file in the assets folder is used only if there is no old data (if the app is installed for the first time). If there is old data, SQLiteAssetHelper executes the SQL upgrade script instead.
The SQLiteAssetHelper project contains an example that shows how such a script would look like.
To keep the data in the favourites table, you do not need to do anything.
To add the new question/answers, use a bunch of INSERT statements.
(For how to get those INSERT statements, see How to compare two SQLite databases.)
finally i got answer for my questions.
as i said i am using SQLiteAssetHelper library.
and now i want to add new records and want to release the update version.
so i was finding the feature by which i can store new updated db in assets folder. and this library will update the old db in user's phone. with keeping particular table in old db.
but currently this library doesnt offers such feature.
the upgrade script can be use only to make changes is old db. we can add new updated db and copy the data from new one.
if we want to add new data and also dont want to erase local data table.
then we need to write insert queries in upgrade script as described in documentation of that library.
but if we have to add 100 queries then their is no shortcut for it.
we have to write 100 insert statements in upgrade script.
and in case if we gate a error saying
cannot upgrade read only database from version x to y then
here is the solution for it:
SQLITE cant upgrade read-only database from version 1 to 2

How can I swap the SQLite file in OrmLite?

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.

Updgrading database downloading it from a server

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.

Android SQLite: what is the best way to create a copy of database

What is the best way to create a copy of all data from database A in database B using SQL (and not a file copy)?
There is two approach check which one you prefers. but I think if possible i will follow first approach. But I have never done it.
First Approach Copy the first database and paste with some othername see following url
How to copy existing database from one app to another
Second Approach copy the contents of two database
Step-1 First Attach two databases
ATTACH DATABASE filename AS database-name;
The DATABASE keyword is optional, but I like the clarity of it. filename should be the path to the database file you wish to attach, and the database-name is a unique name.
Step-2 Run Insert Into commands for the tables you want to transfer.
INSERT INTO X.TABLE(Id, Value) SELECT * FROM Y.TABLE;

Categories

Resources