0

This is my sample database class. Simply I want to create multiple tables on sqlite database. Then i wrote this database helper class below.

package com.example.databasesample;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MyDatabase extends SQLiteOpenHelper{

    public static final String TABLE_ROUTE = "route";   
    public static final String COL_RT_ID = "key";
    public static final String COL_RT_ROUTNM = "routnm";
    public static final String COL_RT_DES= "desc";
    public static final String COL_RT_LOGTME = "lgtime";
    private static final String DATABASE_CREATE_ROUTS = "create table "
            + TABLE_ROUTE + "( " 
            + COL_RT_ID + " integer primary key autoincrement, "
            + COL_RT_ROUTNM + " text not null, "
            + COL_RT_DES + " text, "
            + COL_RT_LOGTME + " text "
            + " );";

    /* TABLE USERMAS */
    public static final String TABLE_USERS = "usermas"; 
    public static final String COL_ID = "key";
    public static final String COL_USR = "user";
    public static final String COL_PASS = "pass";
    public static final String COL_LOGTME = "lgtime";
    /* TABLE USERMAS */
    private static final String DATABASE_CREATE_USERS= "create table "
            + TABLE_USERS + "( " 
            + COL_ID    + " integer primary key autoincrement, "
            + COL_USR   + " text not null, "
            + COL_PASS + " text not null, "
            + COL_LOGTME + " text "
            + " );";    


    /* TABLE ROUTS_DET */
    public static final String TABLE_CUSTOMER = "customers";    
    public static final String COL_CUST_ID = "key";
    public static final String COL_CUST_FOREGN ="key_f";
    public static final String COL_CUST_NM = "rname";   
    public static final String COL_CUST_TP= "tp";   
    public static final String COL_CUST_ADR= "des";
    public static final String COL_CUST_LOGTME = "lgtime";  
    /* TABLE ROUTS_DET */

    private static final String DATABASE_CREATE_CUSTMERS = "create table "
            + TABLE_CUSTOMER + "( " 
            + COL_CUST_ID   + " integer primary key autoincrement, "
            + COL_CUST_FOREGN + " integer not null, "
            + COL_CUST_NM   + " text, "
            + COL_CUST_TP + " text, "           
            + COL_CUST_ADR + " text, "          
            + COL_CUST_LOGTME + " text "
            + " );";

    public MyDatabase(Context context) {
        super(context, "mydb", null, 1);
        // TODO Auto-generated constructor stub
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE_ROUTS);
        db.execSQL(DATABASE_CREATE_CUSTMERS);
        db.execSQL(DATABASE_CREATE_USERS);
        Log.d("MYDATABASE", "onCreate");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d("MYDATABASE", "onUpgrade");
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CREATE_ROUTS);
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CREATE_ROUTS);
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CREATE_ROUTS);
        onCreate(db);

    }

}

Then i instantiate object of my database class in a MainActivity.java ,under the onCreate method.But this code not generate database and tables.where am i wrong?

package com.example.databasesample;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyDatabase db = new MyDatabase(getApplicationContext());
        Log.d("MYACTIVIRY", "ACTIVITY CREATED");
        Log.d("MYACTIVIRY", db.getDatabaseName());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
5
  • What makes you think the tables and the database are not being created? Commented Jan 28, 2014 at 19:10
  • I checked via adb tool,that package directory and i cannot found any database for above name mydb Commented Jan 28, 2014 at 19:12
  • 1
    Your onUpgrade() drops the same table 3 times, just FYI. Commented Jan 28, 2014 at 19:16
  • @mohammedmomn makes a good point. Android will not generate the database until it gets used for the first time. That is, until you call getReadableDatabase or getWriteableDatabase. Instantiating MyDatabase will not create it yet. Commented Jan 28, 2014 at 19:21
  • I thought same thing,but i saw some books mentioned it's is the main theory for SQLiteOpenHelper class Commented Jan 28, 2014 at 19:22

1 Answer 1

2

onCreate() gets called the first time you call getReadableDatabaseor getWriteableDatabase.

try this code :

db = this.getWritableDatabase();

when you call constructor you just create the database with the name and version you provide in code not the tables ,

feed me back for any issue

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

Comments

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.