Elshan
Elshan

Reputation: 7693

Android - SQLite Multiple Table Creation

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;
    }

}

Upvotes: 0

Views: 127

Answers (1)

mohammed momn
mohammed momn

Reputation: 3210

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

Upvotes: 2

Related Questions