数据已插入SQLite Android数据库中,但未显示

时间:2018-12-26 05:03:43

标签: android sqlite

我试图插入一些取自用户的实时值,并试图将其传递给SQLite DB。数据已成功插入,但是当我转到下一个活动时,单击“显示数据”按钮,我得到的是空值。

尽管我可以看到,如果我继续添加这些值...列表会继续增加。

这是我的DataHelper类的代码

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

现在我有一个函数 ClickMe(),该函数在我的插入按钮

的OnClickListener中调用
     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();
    }
}

我正在为成功插入数据敬酒。 转到下一个下一个活动:

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

Clickme()的代码是:

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

结果...我得到姓名联系人的空值。 我试图弄清楚为什么尽管插入了值却没有得到显示的值。

你们能帮我吗.. ??

如果还有比这更好的方法……那么请建议我。 谢谢.. !!

这是LOGCAT报告

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

2 个答案:

答案 0 :(得分:0)

使用以下命令更新您的Clickme函数:

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

我在您的if块中添加了一个附加条件:res.moveToFirst() 并将您的while循环更改为do-while

如果您想知道为什么我添加了moveToFirstRefer this SO link; 另外,请不要忘记致电res.close()

close your cursor

编辑:

也许是您的插入函数内部存在的问题:

您要插入NameContact,但是Id中没有ContentValues

您的插入方法:

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

由于您已经提到 Id 作为主键,并且没有任何价值,因此数据永远不会插入。

现在,您有两种方法可以解决此问题:

  1. 通过添加带有以下内容的AUTOINCREMENT来更改您的创建表查询:

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

  2. 在插入ContentValues的同时添加另一个密钥:

    contentValues.put(col_id, 1);

我建议使用选项1

答案 1 :(得分:0)

请为您的getAllData方法替换以下代码。

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

更新:您的 col_id INTEGER PRIMARY KEY ,并且您没有插入任何值,因此必须使用自动递增主键,方法是使用以下代码在onCreate方法中,更改后请卸载您的应用程序(由于数据库已经创建,因此您需要新的数据库,因为您是change列的属性),然后尝试一下,希望它可以工作

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