Data is getting inserted in the SQLite Android DataBase but not getting shown

0

I am trying to insert some real time values taken from user and trying to pass it to SQLite DB. Data is getting inserted successfully but when I move on to next Activity, Clicking on "Show Data" button, I am getting empty values.

Although I can see that if I keep adding the values...the list goes on increasing.

This is the Code for my DataHelper Class

public class DataBaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "contact.db";
public static final String TABLE_NAME = "contact_table";

public static final String col_id = "ID";
public static final String col_name = "Name";
public static final String col_contact = "Contact";

public DataBaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);

}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    String sql_query = "CREATE TABLE " + TABLE_NAME + "("
            + col_id + " INTEGER PRIMARY KEY," + col_name + " TEXT,"
            + col_contact + " TEXT" + ")";
    sqLiteDatabase.execSQL(sql_query);

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(sqLiteDatabase);
}

public boolean insertdata(String name, String contact) {
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(col_name, name);
    contentValues.put(col_contact, contact);
    long result = sqLiteDatabase.insert(TABLE_NAME, null, contentValues);
    sqLiteDatabase.close();
    // To check whether data is inserted or not
    if (result == -1) {
        return false;
    } else {
        return true;
    }
}

public Cursor getAllData() {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = db.rawQuery("Select * from " + TABLE_NAME, null);
    return res;
}
}

Now I have a function ClickMe() which I am calling in my OnClickListener of Insert Button

     private void ClickMe() {
    String NAME = name.getText().toString();
    String CONTACT = contact.getText().toString();
    Boolean result = dataBaseHelper.insertdata(NAME,CONTACT);
    if (result==true){
        Toast.makeText(this, "Data Inserted", Toast.LENGTH_SHORT).show();
        Log.d(NAME,"");
        Log.d(CONTACT,"");
    }
    else {
        Toast.makeText(this, "Sorry", Toast.LENGTH_SHORT).show();
    }
}

I am getting the toast as Data Inserted Successfully. Moving onto next Next Activity:

 button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           Clickme();
        }
    });

The code for this Clickme() is :

 private void Clickme() {
     res = dataBaseHelper.getAllData();
    StringBuffer stringBuffer = new StringBuffer();
    if(res!= null && res.getCount()>0){
        while (res.moveToNext()){
            stringBuffer.append("Name: " + res.getString(1)+"\n");
            stringBuffer.append("Contact: " + res.getString(2)+"\n"+"\n");
        }
        name_tv.setText(stringBuffer.toString());
        Toast.makeText(this, "Data Retrieved Successfully", Toast.LENGTH_SHORT).show();
    }
    else{
        Toast.makeText(this, "No Data to retrieve", Toast.LENGTH_SHORT).show();
    }
}

As Result...I am getting Empty Values for Name and Contacts. I am trying to figure out as why I am not getting the values displayed although they are getting inserted.

Can you guys Help me with this..??

Also If there is some better way than this...then please do suggest me. Thank You..!!

This the LOGCAT Report

2018-12-28 10:37:25.633 24721-24721/? I/e.mishr.locall: Late-enabling -Xcheck:jni
2018-12-28 10:37:25.699 24721-24741/com.example.mishr.locally I/e.mishr.locall: The ClassLoaderContext is a special shared library.
2018-12-28 10:37:25.719 24721-24721/com.example.mishr.locally I/e.mishr.locall: The ClassLoaderContext is a special shared library.
2018-12-28 10:37:25.782 24721-24721/com.example.mishr.locally I/Perf: Connecting to perf service.
2018-12-28 10:37:25.784 24721-24721/com.example.mishr.locally E/libc: Access denied finding property "vendor.perf.iop_v3.enable"
2018-12-28 10:37:25.784 24721-24721/com.example.mishr.locally E/libc: Access denied finding property "vendor.perf.iop_v3.enable.debug"
2018-12-28 10:37:25.771 24721-24721/com.example.mishr.locally W/e.mishr.locally: type=1400 audit(0.0:1232127): avc: denied { read } for name="u:object_r:vendor_iop_prop:s0" dev="tmpfs" ino=15852 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:vendor_iop_prop:s0 tclass=file permissive=0
2018-12-28 10:37:25.771 24721-24721/com.example.mishr.locally W/e.mishr.locally: type=1400 audit(0.0:1232128): avc: denied { read } for name="u:object_r:vendor_iop_prop:s0" dev="tmpfs" ino=15852 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:vendor_iop_prop:s0 tclass=file permissive=0
2018-12-28 10:37:25.789 24721-24721/com.example.mishr.locally V/Font: Change font:2
2018-12-28 10:37:25.918 24721-24721/com.example.mishr.locally W/e.mishr.locall: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2018-12-28 10:37:25.919 24721-24721/com.example.mishr.locally W/e.mishr.locall: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2018-12-28 10:37:25.922 24721-24721/com.example.mishr.locally D/OpenGLRenderer: Skia GL Pipeline
2018-12-28 10:37:25.931 24721-24721/com.example.mishr.locally E/libc: Access denied finding property "vendor.perf.iop_v3.enable"
2018-12-28 10:37:25.931 24721-24721/com.example.mishr.locally E/libc: Access denied finding property "vendor.perf.iop_v3.enable.debug"
2018-12-28 10:37:25.921 24721-24721/com.example.mishr.locally W/e.mishr.locally: type=1400 audit(0.0:1232129): avc: denied { read } for name="u:object_r:vendor_iop_prop:s0" dev="tmpfs" ino=15852 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:vendor_iop_prop:s0 tclass=file permissive=0
2018-12-28 10:37:25.941 24721-24721/com.example.mishr.locally W/e.mishr.locally: type=1400 audit(0.0:1232131): avc: denied { read } for name="u:object_r:vendor_iop_prop:s0" dev="tmpfs" ino=15852 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:vendor_iop_prop:s0 tclass=file permissive=0
2018-12-28 10:37:25.950 24721-24721/com.example.mishr.locally E/libc: Access denied finding property "vendor.perf.iop_v3.enable"
2018-12-28 10:37:25.950 24721-24721/com.example.mishr.locally E/libc: Access denied finding property "vendor.perf.iop_v3.enable.debug"
2018-12-28 10:37:25.941 24721-24721/com.example.mishr.locally W/e.mishr.locally: type=1400 audit(0.0:1232132): avc: denied { read } for name="u:object_r:vendor_iop_prop:s0" dev="tmpfs" ino=15852 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:vendor_iop_prop:s0 tclass=file permissive=0
2018-12-28 10:37:25.969 24721-24740/com.example.mishr.locally D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@55c8017[Splash_Screen]
2018-12-28 10:37:25.971 24721-24748/com.example.mishr.locally I/Adreno: QUALCOMM build                   : 0bf40b0, I486bafd568
Build Date                       : 08/22/18
OpenGL ES Shader Compiler Version: EV031.25.03.00
Local Branch                     : 
Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.UM.7.3.R1.08.00.00.423.039
Remote Branch                    : NONE
Reconstruct Branch               : NOTHING
2018-12-28 10:37:25.971 24721-24748/com.example.mishr.locally I/Adreno: Build Config                     : S P 6.0.3 AArch64
2018-12-28 10:37:25.961 24721-24721/com.example.mishr.locally W/RenderThread: type=1400 audit(0.0:1232133): avc: denied { search } for name="proc" dev="debugfs" ino=15670 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:qti_debugfs:s0 tclass=dir permissive=0
2018-12-28 10:37:25.975 24721-24748/com.example.mishr.locally I/Adreno: PFP: 0x016ee170, ME: 0x00000000
2018-12-28 10:37:25.978 24721-24748/com.example.mishr.locally I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 1
2018-12-28 10:37:25.978 24721-24748/com.example.mishr.locally I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2018-12-28 10:37:25.978 24721-24748/com.example.mishr.locally I/OpenGLRenderer: Initialized EGL, version 1.4
2018-12-28 10:37:25.979 24721-24748/com.example.mishr.locally D/OpenGLRenderer: Swap behavior 2
2018-12-28 10:37:29.950 24721-24721/com.example.mishr.locally W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@86875ed
2018-12-28 10:37:29.961 24721-24721/com.example.mishr.locally W/e.mishr.locally: type=1400 audit(0.0:1232134): avc: denied { read } for name="u:object_r:vendor_iop_prop:s0" dev="tmpfs" ino=15852 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:vendor_iop_prop:s0 tclass=file permissive=0
2018-12-28 10:37:29.975 24721-24721/com.example.mishr.locally E/libc: Access denied finding property "vendor.perf.iop_v3.enable.debug"
2018-12-28 10:37:29.976 24721-24721/com.example.mishr.locally E/libc: Access denied finding property "vendor.perf.iop_v3.enable"
2018-12-28 10:37:29.971 24721-24721/com.example.mishr.locally W/e.mishr.locally: type=1400 audit(0.0:1232136): avc: denied { read } for name="u:object_r:vendor_iop_prop:s0" dev="tmpfs" ino=15852 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:vendor_iop_prop:s0 tclass=file permissive=0
2018-12-28 10:37:30.050 24721-24721/com.example.mishr.locally E/libc: Access denied finding property "vendor.perf.iop_v3.enable"
2018-12-28 10:37:30.050 24721-24721/com.example.mishr.locally E/libc: Access denied finding property "vendor.perf.iop_v3.enable.debug"
2018-12-28 10:37:30.041 24721-24721/com.example.mishr.locally W/e.mishr.locally: type=1400 audit(0.0:1232138): avc: denied { read } for name="u:object_r:vendor_iop_prop:s0" dev="tmpfs" ino=15852 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:vendor_iop_prop:s0 tclass=file permissive=0
2018-12-28 10:37:30.097 24721-24740/com.example.mishr.locally D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@d5feb4a[MainActivity]
2018-12-28 10:37:30.202 24721-24721/com.example.mishr.locally I/AssistStructure: Flattened final assist data: 3740 bytes, containing 1 windows, 13 views
2018-12-28 10:37:43.957 24721-24740/com.example.mishr.locally D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@d5feb4a[MainActivity]
2018-12-28 10:37:44.051 24721-24721/com.example.mishr.locally W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@52ed6aa
2018-12-28 10:37:44.069 24721-24721/com.example.mishr.locally E/libc: Access denied finding property "vendor.perf.iop_v3.enable"
2018-12-28 10:37:44.070 24721-24721/com.example.mishr.locally E/libc: Access denied finding property "vendor.perf.iop_v3.enable.debug"
2018-12-28 10:37:44.061 24721-24721/com.example.mishr.locally W/e.mishr.locally: type=1400 audit(0.0:1232141): avc: denied { read } for name="u:object_r:vendor_iop_prop:s0" dev="tmpfs" ino=15852 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:vendor_iop_prop:s0 tclass=file permissive=0
2018-12-28 10:37:44.070 24721-24721/com.example.mishr.locally E/libc: Access denied finding property "vendor.perf.iop_v3.enable.debug"
2018-12-28 10:37:44.061 24721-24721/com.example.mishr.locally W/e.mishr.locally: type=1400 audit(0.0:1232144): avc: denied { read } for name="u:object_r:vendor_iop_prop:s0" dev="tmpfs" ino=15852 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:vendor_iop_prop:s0 tclass=file permissive=0
2018-12-28 10:37:44.097 24721-24721/com.example.mishr.locally E/libc: Access denied finding property "vendor.perf.iop_v3.enable"
2018-12-28 10:37:44.097 24721-24721/com.example.mishr.locally E/libc: Access denied finding property "vendor.perf.iop_v3.enable.debug"
2018-12-28 10:37:44.091 24721-24721/com.example.mishr.locally W/e.mishr.locally: type=1400 audit(0.0:1232145): avc: denied { read } for name="u:object_r:vendor_iop_prop:s0" dev="tmpfs" ino=15852 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:vendor_iop_prop:s0 tclass=file permissive=0
2018-12-28 10:37:44.125 24721-24740/com.example.mishr.locally D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@1dea0f1[Home_Activity]
2018-12-28 10:37:45.711 24721-24721/com.example.mishr.locally W/e.mishr.locally: type=1400 audit(0.0:1232147): avc: denied { read } for name="u:object_r:vendor_iop_prop:s0" dev="tmpfs" ino=15852 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:vendor_iop_prop:s0 tclass=file permissive=0
2018-12-28 10:37:45.720 24721-24721/com.example.mishr.locally E/libc: Access denied finding property "vendor.perf.iop_v3.enable"
2018-12-28 10:37:45.721 24721-24721/com.example.mishr.locally E/libc: Access denied finding property "vendor.perf.iop_v3.enable.debug"
2018-12-28 10:37:45.711 24721-24721/com.example.mishr.locally W/e.mishr.locally: type=1400 audit(0.0:1232148): avc: denied { read } for name="u:object_r:vendor_iop_prop:s0" dev="tmpfs" ino=15852 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:vendor_iop_prop:s0 tclass=file permissive=0
android
sqlite
asked on Stack Overflow Dec 26, 2018 by Atul mishra • edited Dec 28, 2018 by Atul mishra

2 Answers

0

Update your Clickme function with following:

private void Clickme() {
     res = dataBaseHelper.getAllData();
    StringBuffer stringBuffer = new StringBuffer();
    if(res!= null && res.getCount() > 0 && res.moveToFirst()){
       do {
            stringBuffer.append("Name: " + res.getString(1)+"\n");
            stringBuffer.append("Contact: " + res.getString(2)+"\n"+"\n");
        } while (res.moveToNext());

        name_tv.setText(stringBuffer.toString());
        Toast.makeText(this, "Data Retrieved Successfully", Toast.LENGTH_SHORT).show();
    }
    else{
        Toast.makeText(this, "No Data to retrieve", Toast.LENGTH_SHORT).show();
    }
}

I have added an extra condition in your if block : res.moveToFirst() and changed your while loop to do-while

If you are wondering, why I added moveToFirst, Refer this SO link; Also, don't forget to close your cursor by calling res.close()

EDIT:

Maybe, the problem is inside your insert function:

You are inserting Name and Contact but there is no Id inside your ContentValues:

Your insert Method:

SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(col_name, name);
contentValues.put(col_contact, contact);
long result = sqLiteDatabase.insert(TABLE_NAME, null, contentValues);

Since you have mentioned, Id as Primary Key, and there is no value for it, the data is never inserted.

Now, you have two ways to fix this issue:

  1. Change your create table query by adding AUTOINCREMENT with the following:

    String sql_query = "CREATE TABLE " + TABLE_NAME + "(" + col_id + " INTEGER PRIMARY KEY AUTOINCREMENT," + col_name + " TEXT," + col_contact + " TEXT" + ")";

  2. Add another key, while inserting in ContentValues:

    contentValues.put(col_id, 1);

I would suggest, go with Option 1

answered on Stack Overflow Dec 26, 2018 by Firoz Memon • edited Dec 26, 2018 by Firoz Memon
0

Please replace following code for your getAllData method.

public Cursor getAllData() {
    Cursor cursor = this.database.query(TABLE_NAME , new String[]{col_id, col_name , col_contact }, null, null, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
    }
    return cursor;
}

Update : Your col_id is INTEGER PRIMARY KEY and you are not inserting any value so must use auto increment Primary Key by using following code in onCreate method , after change please uninstall your app ( as DB is already created so you need fresh db because you are change column property) and give a try , hopefully it will work

String sql_query = "CREATE TABLE " + TABLE_NAME + "("
            + col_id + " INTEGER PRIMARY KEY AUTOINCREMENT," + col_name + " TEXT,"
            + col_contact + " TEXT" + ")";
    sqLiteDatabase.execSQL(sql_query);
answered on Stack Overflow Dec 26, 2018 by Mayank Sharma • edited Dec 26, 2018 by Mayank Sharma

User contributions licensed under CC BY-SA 3.0