Android:数据库不起作用

时间:2015-11-14 15:01:07

标签: android database arraylist database-connection

我的Android应用程序中的SQL数据库存在问题。它在开始时崩溃,我不明白为什么:实际上,直到我在Android服务监视器上的数据/数据/数据库更新其SQL数据库时它才起作用。 另外,如果删除行:

currentQ = quesList.get(qid);

Simulazione.class中,应用程序不会崩溃(但显然数据库未加载到数组中)。

感谢您的帮助。

DbHelper.class

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import com.example.mrx.sss.Question;

public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "inferdb";
// tasks table name
private static final String TABLE_QUEST = "quest";
// tasks Table Columns names
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);
}
SQLiteDatabase db;

@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);            
    //db.close();
}
@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>();

    String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
    dbase=this.getReadableDatabase();
    Cursor cursor = dbase.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            Question quest = new Question();
            quest.setID(cursor.getInt(0));
            quest.setQUESTION(cursor.getString(1));
            quest.setANSWER(cursor.getString(2));
            quest.setOPTA(cursor.getString(3));
            quest.setOPTB(cursor.getString(4));
            quest.setOPTC(cursor.getString(5));
            quest.setOPTD(cursor.getString(6));
            quesList.add(quest);
        } while (cursor.moveToNext());
    }

    return quesList;
}
public int rowcount()
{
    int row=0;
    String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    row=cursor.getCount();
    return row;
}
}

Simulazione.class

import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class Simulazione extends Activity {
List<Question> quesList;
Question currentQ;

TextView txtQuestion, domanda, yyy, prova, risposta;
RadioButton rda, rdb, rdc, rdd;
Button butNext;
int score=0;
int qid=0;
int kid=6;
int did[] = new int[60]; 
int scelta[]= new int[60] ;
String corretta[] = new String[60];
String codice[] = new String[60];
int ggg,row;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);
    DbHelper db = new DbHelper(this);

    quesList = db.getAllQuestions();
    currentQ = quesList.get(qid);

    txtQuestion = (TextView) findViewById(R.id.textView1);
    rda = (RadioButton) findViewById(R.id.radio0);
    rdb = (RadioButton) findViewById(R.id.radio1);
    rdc = (RadioButton) findViewById(R.id.radio2);
    rdd = (RadioButton) findViewById(R.id.radio3);

}

Question.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 个答案:

没有答案