How to store bulk questions in android sqlite db? - android

I am working on quiz application. In my project I need to store huge number of questions to display on the screen. How can I do that? I have downloaded the sqlite browser and when I tried to import .sql file it is displaying error. Also I have kept the .sql file in assets folder in eclipse.
How to store those questions into my sqlite db?

The thing is that even if you port the .sql file to the assets folder in eclipse its not going to work like that when you are going to use it in a real android mobile rather that an emulator by using the final .apk file.
One method what i did was to enter all the entries using a method. In order not to do this at each load, i check if the number of entries is the same else i drop the table and then enter it new once again. Thats the option that i know of. Maybe there are better options. But this works for sure.
public long createValue(int semester, String subname, int credits)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_SEMESTER, semester);
initialValues.put(KEY_SUBNAME, subname);
initialValues.put(KEY_CREDITS, credits);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
Create a method like the above to enter the values and then call this method with the values to enter. So you can just have n number of calls to this method with the values to enter.
mDb is a SQliteDatabase object
Maybe this will help you out.

Related

Adding data to database from program. Android

I faced with the following situation. In my program I have to keep files in database. This database contains title of the article which keeps file and path to the file. All files are kept in Assets folder and are created manually. But what if I want to add files from the program itself. For example to create a special edittexts where user can write title and articles. How can I keep this data? I understand how to add title,entered by user,to database,it's easy. But what about the articles. I can't place them with file which were created manually,as Assets can't keep such files. I thought to add all full articles to database,but how can I add asset's files in such case?
Files (images, PDF'd. Word documents .........) are not structured data and serve little purpose being stored in the database (Full Text Search (FTS) an extension for SQLite is a different matter).
The recommended technique is to store some reference to the file (e.g. full path or perhaps file name (if all files are stored at a location that is discernible by the file name)) in the database. So when you locate/search and obtain a row you then use the file itself.
However, if the files average around about 100k or less then SQLite can actual be faster and thus it may performance wise, be better to store the files in the database.
Note the 100k based upon the link below.
35% Faster Than The Filesystem
You would store files as BLOB's (byte arrays). You read them into a byte array and on Android, assuming java and that the byte array is named my_byte_array and that the SQLiteDatabase is db then :-
ContentValues cv = new Contentvalues();
cv.put("FILE_COLUMN",my_byte_array);
........ other cv.put's for the other columns
long inserted_id = db.insert("The_Table",null,cv);
if (inserted_id < 1) {
.... code to handle row not inserted.
} else {
.... code to handle row inserted OK
}
WARNING
Files greater than 2M will be impossible to easily retrieve as a Cursor Window is limited to 2M of memory. Even 1M may well cause issues and may be unusable at some stage if the App needs to be backwardly compatible as I believe that Cursor window's were restricted to 1M (not sure when).
Retrieval from the database
To retrieve data from a BLOB you use the Cursor getBlob(column_offset) method, or generally better use the Cursor getColumnIndex(column_name) method to retrieve the column offset according to the column name. So if the Cursor is named csr then** my_other_byte_array = csr.getBlob(csr.getColumnIndex(column_name));**
Noting that you have to move to a valid row before using the getBlob method.

Build SQLite database from JSON file in runtime - is it Good?

I need opinion regarding rendering of data in android app. I have all the data stored in a json file abc.json which is in res > raw folder. i have a class that then reads data from that json file and build SQLite database when the app runs and later on i'm performing all operations like searching the data using sql queries for that database. But i am afraid if thats not a good option and the code is not optimized because code now contains so many functions for adding the items to database.
For example, json file has Authors, books, keywords, references, acknowledgements, subauthors and when the database is built, data is read and a specific function is called for each item. I'm just concerned because of too many functions as one for each item. Like whenever json is parsed for an item, e.g author, it calls addAuthors function to add that to database. Following are 2 of the functions for example.
//Sample function code for adding authors to db
public void addAuthors(Integer id, String Name, String is_corresponding) {
ContentValues value = new ContentValues();
value.put("_id", id);
value.put("NAME", Name);
value.put("IS__CORRESPONDING", is_corresponding);
authors_id = database.insert(TABLENAME_AUTHOR, null, value);
}
//example function for adding keywords to db
public void addKeyWord(String KeyWords, Integer id) {
ContentValues values = new ContentValues();
values.put("KEYWORDS", KeyWords);
values.put("_id ", id);
database.insert(TABLENAME_ABSTRACT_KEY_WORDS, null, values);
}
I need help with optimizing my code. Is there any way to optimize the current code ? Kindly help me with this and suggest some improvements for it. Thanks in advance
I would recommend bundling a sqlite database as an asset in your APK instead of bundling the JSON file and then inserting the data into a database. If your data isn't changing, you can then get rid of all your insert functions. You will also save the cost of creating and populating your database dynamically.
You can use the methods described here to create your database and to copy it from the assets of your APK. Be sure to copy it first before you try to open in in your app -- you can't open it directly as an asset.

Populate an SQLiteDatabase in Android with static data

I have a database with multiple tables. One of these tables (sport) is where i have to put a static list of object, each one with an _id, name, logo and an int. The _id will be used by other tables to do some queries (eg. select from "table X" where sport_id = _id), so it shouldn't change overtime (is there a way to update all the reference to this _id if it will change?).
Where should i put the code (i think it will be a simple list of db.insertSport()) to make it add this row only one time (and check if the row number grow, to add the new ones)?
There won't be much row, 50 at the best.
I think I would make a method in the dbHelper to insert that data, then call that method immediately upon app start. I'm making a couple of assumptions here... first that you are shipping this static info with the app and when you want to add more info you will be shipping a new version.
You could store the data as a text file in your assets folder and then read the file in execute a batch insert in the method.
If you set it up right (use insertWithOnConflict and the CONFLICT_IGNORE flag in the method) it will only add the new rows (if any) each time so you can run it every time the app starts and not worry about duplicate data or crashes for constraint violations.
If you only want it to run the once and then again when there is additional info, put a version number in the text file and check that against the previous one (which you can store in SharedPreferences).
EDIT
Example of using insertWithOnConflict:
public long createItem(String yourdata) {
ContentValues initialValues = new ContentValues();
initialValues.put(YOUR_COLUMN, yourdata);
return mDb.insertWithOnConflict(YOUR_TABLE, null, initialValues,
SQLiteDatabase.CONFLICT_IGNORE);
}
You can read up on the SQLiteDatabase class (which has the constants and methods) here

SQLiteException: no such table

I am using my own SQLite3 database as opposed to creating a new one each time my app runs, as I have a few tables with static data that I am trying to display. I created my database and placed it in my assets folder. I then created my database helper and when I start my app I can open my database without problem but when I try to open my first table using the following code
private Cursor getData()
{
try
{
myDbHelper = new DatabaseHelper(this);
SQLiteDatabase db = myDbHelper.getReadableDatabase();
Cursor cursor = db.query("exhibitor", FROM, null, null, null,null, ORDER_BY);
startManagingCursor(cursor);
return cursor;
}
catch(SQLiteException e)
{
String err = e.toString();
return null;
}
}
It throws an error saying android.database.sqlite.SQLiteException: no such table: exhibitor: , while compiling: SELECT _id, EXHIBITOR FROM exhibitor ORDER BY EXHIBITOR but when I check the database exhibitor is there.
What am I missing?
Settings -> Applications -> Manage Applications -> (Click on your application) -> Clear data
Whenever you create new table in an existing database the table doesnt create because the OnCreate function of database handler will not be called everytime but only if required(like database not initiated). If that is not the case your newly created table actually hasnt created. Clear the data to force the db to instantiate itself.
Have you moved the database from the assets folder to /data/data/YOUR_PACKAGE/databases/ on the emulator?
This is a good detailed post about moving the database to /data/data/YOUR_PACKAGE/databases/ if it does not exist.
Here is another short and simple solution to it.
Clear Data and uninstall application from your device and re-install application in device...
Settings -> Applications -> Manage Applications -> Your Application Name -> Clear data
Using SQLiteOpenHelper.onCreate() does not create a new database every time your app starts - rather it creates a database if it does not already exist.
It sounds like you might have an empty database created by or for your helper (if there was no database at all, I might expect a different exception), and your separate database that you created outside of Android. Looks like you and your app are not looking in the same place for your data.
Just clearing the data did not work for me.
What worked was:
Uninstall app
Restart device or emulator
Disable Instant run (Not just the main group but all individual instant run settings)
Build -> Clean Project
Build -> Rebuild Project
hapend to me once
change your DATABASE_VERSION
if your DATABASE_VERSION =1 it will see just three table
if your DATABASE_VERSION = 2 it will see just more table but i really didn't know how many
good luck
I just had a simple mistake.
Reason:
Solution:
Just happened to me. I don't exactly know why but changing the DB_VERSION attribute to a bigger number made it work. The thing is: each time i'm changing the fields of the DB (attributes of the SQLiteDB class), i need to change that number.
Here is my answer according to the description of #ReivieraKid.
No, Uninstall, No Restart, Just checking the app memory if it is your real database, if return false, then copy the database to the memory again. Then You will All set. But to apply this method, you have to know the minimum size of your database.
https://stackoverflow.com/a/73332470/7608371

Cursor.getCount() is negative (= -1)

I tried to load a list of values from a table, but the cursor returns a length of -1?
Is there a possibility to view a sqlite database on a android emulator?
The Code which is buggy:
final Cursor c = db.query(
ACCESS_TOKEN_TABLE,
new String[] { ACCESS_TOKEN_COL_ID, ACCESS_TOKEN_COL_VALUE },
ACCESS_TOKEN_COL_SERVER_ID + "=" + serverId,
null,
null,
null,
null);
public static final String COL_ID = "_id";
public static final String ACCESS_TOKEN_TABLE = "accesstoken";
public static final String ACCESS_TOKEN_COL_ID = COL_ID;
public static final String ACCESS_TOKEN_COL_SERVER_ID = "server_id";
public static final String ACCESS_TOKEN_COL_VALUE = "value";
And there is one entry in the database. The value of ServerID is 1 and there exists one entry, where the ServerID is 1.
Sincerely
xZise
PS: Two questions only to open the database.
you have an sqlite3 command if you "adb -e shell" with a CLI interface to the db.
better yet - i know of an eclipse plugin that can view sqlite database content using the ddms. but my personal favorite is to pull the db file out of the emulator, and use sqlite database browser to view the contents.
getCount() = -1 -> nothing to count... (are you sure your query is OK?)
Don't know about viewing sqlite db with Android emulator but you can use apps like Root Explorer to view Databases.
If the cursor is -1 the SQL statement is not returning any results. Maybe if you were to add the code you are using for your SQL select statement.
As for the second, viewing the DB - yes it is possible:
In Eclipse, DDMS view, click on the name of the emulator in the Devices tab (If the Devices tab isn't showing: Window->Show View->Devices)
Next click on the File Explorer tab (If the tab isn't showing: Window->Show View->File Explorer)
Now browse through the file tree. You need to go to:
data/data/com.package.name/databases
Now click on the name of the database you want to inspect. You will need to copy it to your computer. (To do this, click the picture of a floppy disc with an arrow on it. Save it somewhere you can find.)
You will need an SQLite browser, like the SQLite Manager add-on for Firefox
In Firefox, open the SQLite Manager (Tools->SQLite Manager)
In SQLite Manager, click Database, Connect Database (You may have to change the file type from SQLite DB Files (.sqlite;) to All Files as the Android DB doesn't have to have the .sqlite extension on the file)
Another option is to use the Questiod SQLite Browser plugin for eclipse - I have just added this - makes the above process twice as easy!
Ahr.... I was two lines to high:
So the problem isn't that the cursor returns a negative length, the problem is that it doesn't can give me the indexes of the columns.
I'm very sorry. Because of an totally other question, I posted a new question:
Cursor doesn't find columns?
Sincerely
xZise

Categories

Resources