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
  • You doing right .Check once that – Rahul Kushwaha Dec 26 '18 at 05:10
  • Is there null value or something else in " res = dataBaseHelper.getAllData();". ok, – Rahul Kushwaha Dec 26 '18 at 05:11
  • better way is using room. Its very easy and need less code Follow this link for more details https://developer.android.com/training/data-storage/room/ – Mayank Sharma Dec 26 '18 at 05:12
  • seems correct. maybe you can try adding res.moveToFirst() just before the while loop. also remember to call res.close() after the while loop. – Angel Koh Dec 26 '18 at 05:18
  • While debugging...I am getting **res=null** –  Dec 26 '18 at 05:22
  • Although I can see the values are getting incremented in StringBuffer...but the Values are not being shown. I am just Getting **Name\nContact\n\nName\nContact\n\n.....and so on** –  Dec 26 '18 at 05:29
  • Is the database connection you're selecting from getting closed before you use the results? – Shawn Dec 26 '18 at 06:26

2 Answers2

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

Firoz Memon
  • 4,282
  • 3
  • 22
  • 32
  • Sorry...this doesn't helps. I am still getting empty value. –  Dec 26 '18 at 05:20
  • 1
    If op is using sqlite, he doesn't need AUTOINCREMENT. See https://www.sqlite.org/autoinc.html for details. – Shawn Dec 26 '18 at 06:17
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);
Mayank Sharma
  • 2,735
  • 21
  • 26
  • Sorry Man...this isn't also helping. I am unable to understand as why is this happening...!! Everything seems almost correct but still something is missing out. –  Dec 26 '18 at 05:36
  • what are you getting ? Is cursor is null in that case ? – Mayank Sharma Dec 26 '18 at 05:36
  • If op is using sqlite, he doesn't need AUTOINCREMENT. See https://www.sqlite.org/autoinc.html for details. – Shawn Dec 26 '18 at 06:16
  • I tried adding AUTOINCREMENT and also giving a New DB Name. But it's not helping. Still Nothing is displayed –  Dec 28 '18 at 05:02