1

here is my database helper class:

public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            Log.i("Database", "DATABASE CREATE");
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
                    + " VARCHAR NOT NULL, " + KEY_HOTNESS
                    + " VARCHAR NOT NULL);"

            );
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            Log.i("Database", "DATABASE CREATE");
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(db);

        }

    }

is there any way insert a default database entries when you first opened the application. like its inside the onCreate/onUpgrade method?

2
  • 1
    Did you mean to say that db.qxecSQL() statements should be executed on each and every start of the application ? Commented Dec 2, 2013 at 12:46
  • yes but i only know how to use db.insert(); not db.qxecSQL(); is it like mysql_query(); in php? Commented Dec 2, 2013 at 13:30

6 Answers 6

3
File dbFolder=new File("/data/data/yourpackagename/databases/");
            if(!dbFolder.exists())
            {
                dbFolder.mkdirs();
            }
            File dbFile = new File("/data/data/yourpackagename/databases/"+"dbfilename");
            if(!dbFile.exists() || dbFile.length()<=0)
            {
                dbFile.createNewFile();

                InputStream fIn=context.getAssets().open("QuizDB.jpeg");
                byte[] buffer =new byte[1024];
                FileOutputStream fout=new FileOutputStream(dbFile);
                int c=fIn.read(buffer);
                while (c!=-1)
                {
                    fout.write(buffer);
                    c=fIn.read(buffer);
                }
                fout.close();

Keep your database ready file in assets and write the above code after splash screen. I have done the same for my quiz app. In this i had question and answer in hand already. so i used above code. QuizDB.jpeg is my database file name.

SQLiteDatabase sqLiteDatabase;
sqLiteDatabase=dbHelper.getReadableDatabase();

    Cursor c = sqLiteDatabase.query("QuizTable1", null, null, null, null, null, null);
    if( c.moveToNext() )
    {
      c.getString(0);
    }

Above code to read database. Note::QuizTable1 is the table I created in my database.

Sign up to request clarification or add additional context in comments.

3 Comments

this is the most efficient answer i have come across but i don't know how to use buffer reader in java.
U can refer java docs at Oracle site but why do u need to know buffer for this? By using above code you can create database.
To read database I have edited the post. plz refer it. If this answer is helpful, please accept the answer.
3

Yes there is plugin available for Creating and inserting data manually on sqlite data base.

If you are using mozilla firefox than use this adons

https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/

After Installing to open it on mozilla firefox Tools->SQLite Manager->And Create Database

Install it and create database manually and insert record manually from there.After creating database you can put data base with .sqlite extension on Asset Folder.

After putting on asset folder you can read This Tutorial for reading records from external database [I.E from .sqlite file put on asset folder]

Comments

1

Just add whatever insert calls you want to your onCreate() after creating the table.

E.g.

db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
                + " VARCHAR NOT NULL, " + KEY_HOTNESS
                + " VARCHAR NOT NULL);"

ContentValues cv = new ContentValues();
cv.put(KEY_NAME, "name");
cv.put(KEY_HOTNESS, "burning");
db.insert(DATABASE_TABLE, null, cv);

3 Comments

is it ok if i put like 60 default entries? i know it'll be inefficient but i wanna ask anyway..
@ChristianBurgos Shouldn't be a problem. onCreate() already runs in a database transaction so the time waiting for I/O is minimized.
will this be saved even if i put some extra contents on the database?
0

what you mean by default database entries . you can add DEFAULT value after column .please elaborate your problem

Comments

0

Have your default database saved in a file, then load it in your onCreate().

Check this article.

Comments

0

yes its possible. Insert default values inside onCreate Method of your helper class.

 @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            Log.i("Database", "DATABASE CREATE");
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
                    + " VARCHAR NOT NULL, " + KEY_HOTNESS
                    + " VARCHAR NOT NULL);"

            );

            SQLiteDatabase db=getWritableDatabase();

        ContentValues values=new ContentValues();

        values.put(KEY_NAME, "name");
        values.put(KEY_HOTNESS, "hotness");

        db.insert(DATABASE_TABLE, null, values);
        db.close();

        }

3 Comments

where is the problem ?
Even if you get the reference of the db from methods other than onCreate(), its still the same variable of the same SQLiteOpenHelper class. :)
Ah, didn't see the getWritableDatabase() call at first due to the indentation. Sry. But that's still not necessary as db is already open for writing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.