android - 数据已插入 SQLite Android 数据库但未显示
问题描述
我正在尝试插入一些从用户那里获取的实时值并尝试将其传递给 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();
}
}
结果...我得到Name和Contacts的空值。我试图弄清楚为什么我没有得到显示的值,尽管它们被插入了。
你们能帮我解决这个问题吗??
另外,如果有比这更好的方法......那么请建议我。谢谢你..!!
这是 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
解决方案
请为您的 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 方法中使用以下代码来使用自动递增主键,更改后请卸载您的应用程序(因为数据库已经创建,所以您需要新的数据库,因为您是更改列属性)并尝试一下,希望它会起作用
String sql_query = "CREATE TABLE " + TABLE_NAME + "("
+ col_id + " INTEGER PRIMARY KEY AUTOINCREMENT," + col_name + " TEXT,"
+ col_contact + " TEXT" + ")";
sqLiteDatabase.execSQL(sql_query);
推荐阅读
- codenameone - 未能使用 LocalNotificationCallBack 显示使用 Codenameone 显示通知的表单
- c# - 如何使用 PostSharp 定义不变量和后置条件?
- rest - WSO2 Identity Server - 用于权限和角色的 REST API
- php - 根据唯一 ID 在 php 中计数
- android - 如何在 Android 服务中获取 ReactContext?
- php - 在 PHP 嵌套 for 循环中将 O(n4) 复杂度优化为 O(n)
- windows - 如何要求 Windows 10 索引 UWP 应用中的 USB 密钥?
- angular - Angular如何等到长文本完全显示?
- javascript - 没有在节点js中得到结果,mongo db在循环中使用promise
- encryption - 如何在 AES 加密中使用随机随机数解密