SQLite数据库插入正常但页面无法展示数据问题求助
问题:SQLite数据插入后页面无法正常展示数据
背景
我正在开发具备数据存储与展示功能的应用,目前使用两个SQLite数据库:一个用于登录注册,另一个用于信息展示。
初始代码问题
初始代码存在两处标记为//error的问题:
public class huh extends SQLiteOpenHelper { private static final String DATABASE_NAME = "exerbud.db"; private static final int SCHEMA_VERSION = 1; Context c; public huh(Context context) { super(context, DATABASE_NAME, null, SCHEMA_VERSION); c=context; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE hmm ( _id INTERGER PRIMARY KEY AUTOINCREMENT," + //error " namestr TEXT, emailstr TEXT, phonestr TEXT, gender_type TEXT);"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //Will not be called until SCHEMA_VERSION increases //Here we can upgrade the database e.g. add more tables } public Cursor getAll() { return (getReadableDatabase().rawQuery( "SELECT _id, namestr, emailstr, phonestr," + " gender_type FROM hmm ORDER BY namestr", null)); } public void insert (String namestr, String emailstr, String phonestr, String gender_type) { ContentValues cv = new ContentValues(); Toast.makeText(c,"In huh Insert",Toast.LENGTH_LONG).show(); cv.put("namestr", namestr); cv.put("phonestr",phonestr ); cv.put("emailstr",emailstr ); cv.put("gender_type",gender_type ); SQLiteDatabase dbt= getWritableDatabase(); //error if(dbt!=null) { Toast.makeText(c,"writeable database is not null",Toast.LENGTH_LONG).show(); /*getWritableDatabase().insert("hmm", "namestr", cv);*/ }else{ Toast.makeText(c,"writeable database is null",Toast.LENGTH_LONG).show(); } } public String getName(Cursor c) { return (c.getString(1)); } public String getEmail(Cursor c) { return (c.getString(2)); } public String getTel(Cursor c) { return (c.getString(3)); } public String getGenderType(Cursor c) { return (c.getString(4)); } }
注:数据库名为huh,已测试数据库实例不为空。
更新记录
update{1}
将INTERGER修正为INTEGER后,数据库错误解决,但展示页面无法显示预期内容:
- 操作页面:编辑/插入资料页面(用户填写姓名、邮箱、电话、性别等信息)
- 实际输出:页面无预期数据展示
- 预期输出:展示用户填写的所有信息条目
update{2}
更新代码,移除数据库实例判断逻辑,直接调用getWritableDatabase().insert()执行插入:
public class huh extends SQLiteOpenHelper { private static final String DATABASE_NAME = "exerbud.db"; private static final int SCHEMA_VERSION = 1; Context c; public huh(Context context) { super(context, DATABASE_NAME, null, SCHEMA_VERSION); c=context; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE hmm ( _id INTEGER PRIMARY KEY AUTOINCREMENT," + //error已修正 " namestr TEXT, emailstr TEXT, phonestr TEXT, gender_type TEXT);"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //Will not be called until SCHEMA_VERSION increases //Here we can upgrade the database e.g. add more tables } public Cursor getAll() { return (getReadableDatabase().rawQuery( "SELECT _id, namestr, emailstr, phonestr," + " gender_type FROM hmm ORDER BY namestr", null)); } public void insert (String namestr, String emailstr, String phonestr, String gender_type) { ContentValues cv = new ContentValues(); Toast.makeText(c,"In huh Insert",Toast.LENGTH_LONG).show(); cv.put("namestr", namestr); cv.put("phonestr",phonestr ); cv.put("emailstr",emailstr ); cv.put("gender_type",gender_type ); getWritableDatabase().insert("hmm", "namestr", cv); /* //SQLiteDatabase dbt= getWritableDatabase(); //error已移除 if(dbt!=null) { Toast.makeText(c,"writeable database is not null",Toast.LENGTH_LONG).show(); }else{ Toast.makeText(c,"writeable database is null",Toast.LENGTH_LONG).show(); }*/ } public String getName(Cursor c) { return (c.getString(1)); } public String getEmail(Cursor c) { return (c.getString(2)); } public String getTel(Cursor c) { return (c.getString(3)); } public String getGenderType(Cursor c) { return (c.getString(4)); } }
但展示页面仍无法正常显示预期内容,实际输出依旧为空,无用户填写的信息展示。
update{3}
Logcat无任何报错信息。
当前求助
寻求解决页面无法正常展示数据库中存储的数据的问题。
内容的提问来源于stack exchange,提问作者WutsDat Debuff
相关产品推荐
相关产品推荐

