我为SQLite数据库实现了一个代码,该代码在Android模拟器上成功运行但未在真实设备上运行。它显示未找到错误表。我使用SQLite浏览器创建了数据库,并在Android Device Monitor中导入数据库(fileExplorer> data> data>>数据库)。请检查以下代码并告诉我我错在哪里。三江源。
public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_NAME = "quiz6to8";
private static final String TABLE_QUEST = "tbl_cvs6";
//private static final String TABLE_SCIENCE = "science";
private static final String KEY_ID = "id";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; //correct option
private static final String KEY_OPTA = "opta"; //option a
private static final String KEY_OPTB = "optb"; //option b
private static final String KEY_OPTC = "optc"; //option c
private static final String KEY_OPTD = "optd"; //option d
private SQLiteDatabase dbase;
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
dbase=db;
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
+ " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, "
+KEY_OPTB +" TEXT, "+KEY_OPTC+" TEXT, "+KEY_OPTD+" TEXT)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
db.execSQL(" DROP TABLE IF EXISTS " + TABLE_QUEST);
onCreate(db);
}
public List<Question> getAllQuestions() {
List<Question> quesList = new ArrayList<Question>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
dbase=this.getReadableDatabase();
dbase=this.getWritableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Question quest = new Question();
//quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(0));
quest.setOPTA(cursor.getString(1));
quest.setOPTB(cursor.getString(2));
quest.setOPTC(cursor.getString(3));
quest.setOPTD(cursor.getString(4));
quest.setANSWER(cursor.getString(5));
quesList.add(quest);
}
while (cursor.moveToNext());
}
// return quest list
return quesList;
}
public int rowcount(){
int row= 0;
String selectQuery = "SELECT * FROM " + "tbl_cvs6";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
row=cursor.getCount();
return row;
}
}
question.java class
public class Question {
private int ID;
private String QUESTION;
private String OPTA;
private String OPTB;
private String OPTC;
private String OPTD;
private String ANSWER;
public Question() {
ID=0;
QUESTION="";
OPTA="";
OPTB="";
OPTC="";
OPTD="";
ANSWER="";
}
public Question(String qUESTION, String oPTA, String oPTB, String oPTC, String oPTD, String aNSWER) {
QUESTION = qUESTION;
OPTA = oPTA;
OPTB = oPTB;
OPTC = oPTC;
OPTD = oPTD;
ANSWER = aNSWER;
}
public int getID()
{
return ID;
}
public String getQUESTION() {
return QUESTION;
}
public String getOPTA() {
return OPTA;
}
public String getOPTB() {
return OPTB;
}
public String getOPTC() {
return OPTC;
}
public String getOPTD() {
return OPTD;
}
public String getANSWER() {
return ANSWER;
}
public void setID(int id)
{
ID=id;
}
public void setQUESTION(String qUESTION) {
QUESTION = qUESTION;
}
public void setOPTA(String oPTA) {
OPTA = oPTA;
}
public void setOPTB(String oPTB) {
OPTB = oPTB;
}
public void setOPTC(String oPTC) {
OPTC = oPTC;
}
public void setOPTD(String oPTD) {
OPTD = oPTD;
}
public void setANSWER(String aNSWER) {
ANSWER = aNSWER;
}
}
答案 0 :(得分:2)
public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_NAME = "quiz6to8";
private static final String TABLE_QUEST = "tbl_cvs6";
//private static final String TABLE_SCIENCE = "science";
private static final String KEY_ID = "id";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; //correct option
private static final String KEY_OPTA = "opta"; //option a
private static final String KEY_OPTB = "optb"; //option b
private static final String KEY_OPTC = "optc"; //option c
private static final String KEY_OPTD = "optd"; //option d
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
+ " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, "
+KEY_OPTB +" TEXT, "+KEY_OPTC+" TEXT, "+KEY_OPTD+" TEXT)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
db.execSQL(" DROP TABLE IF EXISTS " + TABLE_QUEST);
onCreate(db);
}
public List<Question> getAllQuestions() {
List<Question> quesList = new ArrayList<Question>();
// Select All Query
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Question quest = new Question();
//quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(0));
quest.setOPTA(cursor.getString(1));
quest.setOPTB(cursor.getString(2));
quest.setOPTC(cursor.getString(3));
quest.setOPTD(cursor.getString(4));
quest.setANSWER(cursor.getString(5));
quesList.add(quest);
}
while (cursor.moveToNext());
}
// return quest list
return quesList;
}
public int rowcount(){
int row= 0;
String selectQuery = "SELECT * FROM " + "tbl_cvs6";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
row=cursor.getCount();
return row;
}
}
用此替换您的代码并检查一次。
答案 1 :(得分:1)
卸载应用并再次安装并尝试一次,或者我认为drop table在这里不起作用。 试试这样:
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
dbase=db;
db.execSQL(" DROP TABLE IF EXISTS " + TABLE_QUEST);
onCreate(db);
}
答案 2 :(得分:0)
您正在查询不存在的表。您正在尝试查询名为tbl_cvs6
的表,但您只创建了一个名为question
的表,该表由常量TABLE_QUEST定义。
您需要在查询之前创建故事。
编辑:
您可能是为模拟器上运行但未在设备上运行的数据库的初始版本创建了tbl_cvs6
。在进一步的更新中,您可能删除了该代码以创建所述表。如果启用了SCM,请考虑查看从版本1到3的代码更改。
答案 3 :(得分:0)
在我的活动中,我正在添加并检查它
public class QuestionClass extends Activity {
List<Question> list = new ArrayList<Question>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
DbHelper dbHelper = new DbHelper(this);
list = dbHelper.getAllQuestions();
if (list.size() < 1) {
dbHelper.addContact(new Question("KEY_QUES", "KEY_Ans", "KEY_OA", "KEY_OB", "KEY_OC", "KEY_OD"));
Toast.makeText(this, "Add", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "SIze more", Toast.LENGTH_LONG).show();
Log.e("list", list.size() + "");
}
然后在我的DbHandler中,我这样给予
public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_NAME = "quiz6to8";
private static final String TABLE_QUEST = "tbl_cvs6";
//private static final String TABLE_SCIENCE = "science";
private static final String KEY_ID = "id";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; //correct option
private static final String KEY_OPTA = "opta"; //option a
private static final String KEY_OPTB = "optb"; //option b
private static final String KEY_OPTC = "optc"; //option c
private static final String KEY_OPTD = "optd"; //option d
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
+ " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, "
+KEY_OPTB +" TEXT, "+KEY_OPTC+" TEXT, "+KEY_OPTD+" TEXT)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
db.execSQL(" DROP TABLE IF EXISTS " + TABLE_QUEST);
onCreate(db);
}
public void addContact(Question contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_QUES, contact.getQUESTION()); // Contact Name
values.put(KEY_ANSWER, contact.getANSWER());
values.put(KEY_OPTA, contact.getOPTA());
values.put(KEY_OPTB, contact.getOPTB());
values.put(KEY_OPTC, contact.getOPTC());
values.put(KEY_OPTB, contact.getOPTD());
// Inserting Row
db.insert(TABLE_QUEST, null, values);
db.close(); // Closing database connection
}
public List<Question> getAllQuestions() {
List<Question> quesList = new ArrayList<Question>();
// Select All Query
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Question quest = new Question();
//quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(0));
quest.setOPTA(cursor.getString(1));
quest.setOPTB(cursor.getString(2));
quest.setOPTC(cursor.getString(3));
quest.setOPTD(cursor.getString(4));
quest.setANSWER(cursor.getString(5));
quesList.add(quest);
}
while (cursor.moveToNext());
}
// return quest list
return quesList;
}
public int rowcount(){
int row= 0;
String selectQuery = "SELECT * FROM " + "tbl_cvs6";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
row=cursor.getCount();
return row;
}
}
问题类
public class Question {
private int ID;
private String QUESTION;
private String OPTA;
private String OPTB;
private String OPTC;
private String OPTD;
private String ANSWER;
public Question(String key_ques, String key_ans, String key_oa, String key_ob, String key_oc, String key_od) {
this.QUESTION=key_ques;
this.ANSWER=key_ans;
this.OPTA=key_oa;
this.OPTB=key_ob;
this.OPTC=key_oc;
this.OPTD=key_od;
}
public Question() {
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getQUESTION() {
return QUESTION;
}
public void setQUESTION(String QUESTION) {
this.QUESTION = QUESTION;
}
public String getOPTA() {
return OPTA;
}
public void setOPTA(String OPTA) {
this.OPTA = OPTA;
}
public String getOPTB() {
return OPTB;
}
public void setOPTB(String OPTB) {
this.OPTB = OPTB;
}
public String getOPTC() {
return OPTC;
}
public void setOPTC(String OPTC) {
this.OPTC = OPTC;
}
public String getOPTD() {
return OPTD;
}
public void setOPTD(String OPTD) {
this.OPTD = OPTD;
}
public String getANSWER() {
return ANSWER;
}
public void setANSWER(String ANSWER) {
this.ANSWER = ANSWER;
}
}