Hikari High Hikari High - 9 days ago 4
Java Question

Andriod SQLite Warn

I have some strange warning in SQLite.
What does it mean?


11-30 13:05:23.653 9964-9964/com.hikari.dev.servermonitor
D/SQLiteDatabase: Open database
java.lang.Throwable: stacktrace
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:815)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:714)
at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:578)
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:269)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
-> at com.hikari.dev.servermonitor.main.onClick(main.java:40)
at android.view.View.performClick(View.java:5269)
at android.view.View$PerformClick.run(View.java:21556)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)


database_helper.java (singleton)

package com.hikari.dev.servermonitor;

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

public class database_helper extends SQLiteOpenHelper
{
// Database
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "ServerMonitorDatabase";
public static final String TABLE_SERVERS = "servers";

//Servers
public static final String SERVER_ID = "_id";
public static final String SERVER_NAME = "name";
public static final String SERVER_MONITORING_STATUS = "monitoring_status";
public static final String SERVER_IP = "ip";
public static final String SERVER_PORT = "port";

//Singleton
private static database_helper instance;

public static database_helper getInstance(Context context)
{
if(instance == null)
instance = new database_helper(context.getApplicationContext());
return instance;
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase)
{
sqLiteDatabase.execSQL("create table " + TABLE_SERVERS + "(" + SERVER_ID
+ " integer primary key," + SERVER_NAME + " text," + SERVER_MONITORING_STATUS
+ " integer," + SERVER_IP + " text," + SERVER_PORT + " integer)");
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
{
sqLiteDatabase.execSQL("drop table if exists" + TABLE_SERVERS);

onCreate(sqLiteDatabase);
}

private database_helper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


}


main.java

(40) SQLiteDatabase database = database_helper.getInstance(this).getReadableDatabase();

Answer

Here I am creating DATABASE in specific location:

public class MessageLocalDB extends SQLiteOpenHelper {


    private static MessageLocalDB mDB;
    private static final String MSG_TABLE_NAME = "message_table";
    private static final String KEY_SRNO = "SRNO";
    private static final String KEY_ENQUIRYID = "ENQUIRYID";
    private static final String KEY_OPERATORID = "OPERATORID";


    public static synchronized MessageLocalDB getInstance(Context context, String StrDBNAme){
        if (mDB==null){
            mDB= new MessageLocalDB(context.getApplicationContext(),StrDBNAme);
        }
        return mDB;
    }

    private MessageLocalDB(Context context, String StrDBNAme) {
        super(context,StrDBNAme,null,1);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        String strQueryComment = "create table " +
                MSG_TABLE_NAME + "(SRNO string,ENQUIRYID string,OPERATORID string)";
        db.execSQL(strQueryComment);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String DROP_MSG_TABLE = "DROP TABLE IF EXISTS" + MSG_TABLE_NAME;
        db.execSQL(DROP_MSG_TABLE);
    }

    public void insertMSG(String srno,String enquiryid,String operatorid,String operatorname,String message,String time,String msgtype){
        SQLiteDatabase sqdb=getWritableDatabase();
        sqdb.beginTransaction();
        try{
            ContentValues values= new ContentValues();
            values.put(KEY_SRNO,srno);
            values.put(KEY_ENQUIRYID,enquiryid);
            values.put(KEY_OPERATORID,operatorid);
            sqdb.insert(MSG_TABLE_NAME,null,values);
            sqdb.setTransactionSuccessful();
        }catch (Exception ee){ee.printStackTrace();}
        finally {
            sqdb.endTransaction();
            sqdb.close();
        }
    }

    public Cursor fetchMSG(int enquiryid){
        SQLiteDatabase sqdb=getReadableDatabase();
        return  sqdb.rawQuery("select * from "+MSG_TABLE_NAME+" where "+KEY_ENQUIRYID+"=" + enquiryid + "", null);

    }
}

And in Activity:

MessageLocalDB msgDB;



String LocalDBMesage ="_chatdb.db";
        String FinalLocalDBName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + LocalDBMesage;
        msgDB= MessageLocalDB.getInstance(MainActivity.this,FinalLocalDBName);

And write data into database like:

msgDB.insertMSG(strSr,strenquiryID,"2323","DIvyesh",currentMSG,fdfg,msgStatus);

And read data like:

Cursor count=msgDB.fetchMSG(Integer.parseInt(strenquiryID));