Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

database is not created in android

Tags:

android

sqlite

In my application activity was running fine but database is not getting created also there is no error in logcat. Main activity class:

package com.example.testdb;

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Database d=new Database(this);
    }

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

Database class:

package com.example.testdb;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

public class Database extends SQLiteOpenHelper{

    String Tablename = "Table1";
    private String Column1 = "RegionID";
    private String Column2 = "RegionName";
    private String Column3 = "Currency";
    SQLiteDatabase db;

    public Database(Context context) {
        super(context, "Test", null, 2);
        this.getWritableDatabase();
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        try {
            final String r_Table = "CREATE TABLE " + Tablename + " (" + Column1+ " INTEGER PRIMARY KEY , " + Column2 + " TEXT, " + Column3 + " Text) ";
            db.execSQL(r_Table);
            ContentValues cv = new ContentValues();
            cv.put(Column1, 1);
            cv.put(Column2, "India");
            cv.put(Column3, "Rupee");
            db.insert(r_Table, null, cv);
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }

        Cursor c = db.rawQuery("Select * from Table1", null);

        if (c != null ) {
            if  (c.moveToFirst()) {
                //Toast.makeText(new MainActivity().getApplicationContext(), c.getCount(), Toast.LENGTH_LONG).show();
                System.out.println("Rows are:"+c.getCount());
            }
        }
        c.close();  
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
    }
}

Logcat:

05-18 12:45:22.042: I/ActivityThread(3270): Switching default density from 160 to 130
05-18 12:47:18.162: I/ActivityThread(3415): Switching default density from 160 to 130

I am using Virtual box for emulator and in DDMS I can't even see the database got created.

Can anyone please help me in this.

Thanks in advance Siva

like image 597
Siva Avatar asked Jan 21 '26 22:01

Siva


1 Answers

According to documentation SqliteOpenHelper uses lazy model of creating databases. This means, that in the absence of database, onCreate will be called not in constructor, but when the database is actually needed, I.e. at the first call to getReadableDatabase or getWritableDatabase. So, you situation is normal.

Think of SqliteOpenHelper as provider of connection to database, containing additional logic for handling situations like "if database not exist" and "if database is outdated". Thus, in your case, don't worry about when database file is actually created.

Database usage in your Activity (if it is the only one, using db) could be something like:

private SqliteOpenHelper connection;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.OnCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    connection = new Database(this); //no actual db creation here
    //if you want to test db table creation without any further work uncomment following line
    //SQLiteDatabase db = connection.getReadableDatabase();
}

public void onBtnAddRecordClick(View view) {
    SqliteDatabase db = connection.getWritableDatabase(); //if something not right with
                                                  //the db this will be corrected here
    ContentValues values;
    //... fill in the record to insert
    db.insert(MY_TABLE_NAME, null, values);
}

Important note: If you are going to access you db from multiple Activities, consider making your Database extending SqliteDatabaseHelper a singleton or wrap it into a ContentProvider, because it is generally frowned upon to use multiple connections to a database without a clear purpose.

like image 158
alexei burmistrov Avatar answered Jan 24 '26 13:01

alexei burmistrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!