user3100193
user3100193

Reputation: 561

inserting into sql database android eclipse

I'm total beginner and I don't know how sql works inside android.

I'm having simple app game which needs database. During the game, app is displaying some records from database to users.

I have these questions:

1) does database and records still exists when user closes the app, and will the database and records still be available to the users?

2) code which creates database if it doesn't exists:

 SQLiteDatabase mydatabase = openOrCreateDatabase("tabu",MODE_PRIVATE,null);
    mydatabase.execSQL("CREATE TABLE IF NOT EXISTS rijeci(id INT, rijec VARCHAR, rijec1 VARCHAR, rijec2 VARCHAR, rijec3 VARCHAR, rijec4 VARCHAR);");

I put in the mail activity.java?

3) I want to insert some records into database which will be available o the users during the game (records will always be the same). My only idea is to insert values in database in main acitivty.java if they don't exist. But this does not seem like best solution because that code will be executed on every app start. Especially this will be bad solution if I need to insert large amount of data (something like 10000 records). What is better solution to insert records in database for once, before user even starts the app and never again?

Upvotes: 0

Views: 163

Answers (1)

Olumide
Olumide

Reputation: 757

  1. Yes the database exists even after the user closes the app. This database itself is a file stored somewhere on the phone, it's not tied to the process/activity that created it.

  2. Android uses sqlite and sqlite doesn't have the the VARCHAR data type, it only has NULL, INTEGER, BLOB, REAL and TEXT. Look at the sqlite.org website for more details in the data types. The android developer website actually has a pretty neat explanation of how the database works, and they provided the SQLiteOpenHelper class to help with basic database handling.

  3. If you're going to be inserting as many as 1000 entries, you should probably be using an AsyncTask for that, because that's a lot of work to do on the main thread. You could always just check if the number of entries in the table is the right amount and only proceed to delete the entries and insert the entries otherwise.

Upvotes: 1

Related Questions