使用SQLite为数据库创建Android应用程序时出错

时间:2016-05-31 18:06:57

标签: android sqlite

com.example.dilshadazmat.generalknowledge, PID: 8203
java.lang.RuntimeException: Unable to start activity ComponentInfo
{com.example.dilshadazmat.
generalknowledge
/com.example.dilshadazmat.generalknowledge.Islamic_mcqs}: 
java.lang.IllegalStateException: getDatabase called recursively
at android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2689)
at android.app.ActivityThread.handleLaunchActivity
(ActivityThread.java:2756)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5940)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:1389)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
Caused by: java.lang.IllegalStateException: getDatabase 
called recursively android.database.sqlite.SQLiteOpenHelper.
getDatabaseLocked(SQLiteOpenHelper.java:203)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase
(SQLiteOpenHelper.java:163)
at com.example.dilshadazmat.generalknowledge.DbIslamic.
addQuestions(DbIslamic.java:81)
at com.example.dilshadazmat.generalknowledge.DbIslamic.
addQuestions(DbIslamic.java:59)
at com.example.dilshadazmat.generalknowledge.
DbIslamic.onCreate(DbIslamic.java:49)
at android.database.sqlite.SQLiteOpenHelper.
getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.
 getReadableDatabase(SQLiteOpenHelper.java:187)
 at   com.example.dilshadazmat.generalknowledge.
 DbIslamic.getAllQuestions
(DbIslamic.java:97)
at com.example.dilshadazmat.generalknowledge.Islamic_mcqs.onCreate
slamic_mcqs.java:47)
at android.app.Activity.performCreate(Activity.java:6283)
at android.app.Instrumentation.callActivityOnCreate
(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:26  at
androi

这是我想要创建新数据库的数据库类。这里有我无法理解的问题是什么错误列表让所有人感到无聊。

public class DbIslamic extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "islamicMcqs";
private static final String TABLE_QUESTION = "questions";
private static final String KEY_ID = "id";
private static final String KEY_QUESTION = "question";
private static final String KEY_ANSWER = "answer";
private static final String KEY_OPTA = "opta";
private static final String KEY_OPTB = "optb";
private static final String KEY_OPTC = "optc";
private static final String KEY_OPTD = "optd";

private SQLiteDatabase database;

public DbIslamic(Context context)

{
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db)
{
    database = db;

    String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUESTION +
    "(" +KEY_ID +
 "INTEGER, "
            + KEY_QUESTION + "TEXT," + KEY_OPTA + "TEXT," +
   KEY_OPTB +     "TEXT," + 
   KEY_OPTC + "TEXT,"
            + KEY_OPTD + "TEXT,"+ KEY_ANSWER + "TEXT)" ;
    db.execSQL (sql);

   addQuestions();

    db.close();
}


private void addQuestions()
{
    Islamic_mcqs q1 = new Islamic_mcqs("Who was the 
 last prophet in"+" the list of prophets ?", "Hazrat Adam(A.S)",
            "Hazrat Muhammad(S.A.W)","Hazrat Yousaf(A.S)",
  "Hazrat Ibraheem (A.S)","B");
    this.addQuestions(q1);

}



@Override
public void onUpgrade(SQLiteDatabase db, int oldValue, int newValue)
{
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " +TABLE_QUESTION);
    // Create tables again
    onCreate(db);
}
// Adding new question
 public void addQuestions(Islamic_mcqs questions)
 {
   SQLiteDatabase db = this.getWritableDatabase();
   ContentValues values = new ContentValues();
   values.put(KEY_QUESTION,questions.getQUESTION());
   values.put(KEY_ANSWER,questions.getANSER());
   values.put(KEY_OPTA,questions.getOPTA());
   values.put(KEY_OPTB,questions.getOPTB());
   values.put(KEY_OPTC,questions.getOPTC());
   values.put(KEY_OPTD,questions.getOPTD());
   // Inserting Row
   database.insert(TABLE_QUESTION,null,values);
 }
 public List<Islamic_mcqs> getAllQuestions()
 {
    List<Islamic_mcqs> questionsList = new ArrayList<Islamic_mcqs>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_QUESTION;
    database=this.getReadableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Islamic_mcqs q = new Islamic_mcqs();
            q.setID(cursor.getInt(0));
            q.setQUESTION(cursor.getString(1));
            q.setANSWER(cursor.getString(2));
            q.setOPTA(cursor.getString(3));
            q.setOPTAB(cursor.getString(4));
            q.setOPTC(cursor.getString(5));
            q.setOPTD(cursor.getString(6));
            questionsList.add(q);
        } while (cursor.moveToNext());
    }
    // return quest list
    return questionsList;
}

本课程中的另一个类Islamic_mcqs我想从这个数据库中获取数据库中的所有数据,我将保存多项选择题。

public class Islamic_mcqs extends AppCompatActivity{

private  int ID;
private String QUESTION;
private String OPTA;
private String OPTB;
private String OPTC;
private String OPTD;
private String ANSWER;

List<Islamic_mcqs> questionList;

int score = 0;
int qid = 0;


TextView tv_t,tv_num,tv_q;
RadioButton rb_a,rb_b,rb_c,rb_d;
Button bnext;

Islamic_mcqs currentQ;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.islamic_mcqs);

    DbIslamic database = new DbIslamic(this);
    questionList = database.getAllQuestions();
    Islamic_mcqs currentQ = questionList.get(qid);

    tv_t = (TextView)findViewById(R.id.tv_title);
    tv_q = (TextView)findViewById(R.id.tv_question);
    tv_num = (TextView)findViewById(R.id.tvNumber);

    rb_a = (RadioButton)findViewById(R.id.rb_A);
    rb_b = (RadioButton)findViewById(R.id.rb_B);
    rb_c = (RadioButton)findViewById(R.id.rb_C);
    rb_d = (RadioButton)findViewById(R.id.rb_D);

    bnext = (Button)findViewById(R.id.b_next);

    setQuestionView();

}
private void setQuestionView()
{
    tv_q.setText(currentQ.getQUESTION());
    rb_a.setText(currentQ.getOPTA());
    rb_b.setText(currentQ.getOPTB());
    rb_c.setText(currentQ.getOPTC());
    rb_d.setText(currentQ.getOPTD());
    qid++;


}
public Islamic_mcqs()
{
    ID = 0;
    QUESTION = "";
    OPTA = "";
    OPTB = "";
    OPTC = "";
    OPTD = "";
    ANSWER = "";
}

public Islamic_mcqs(String islQuestion, String optA, 
 String optB, String optC, String optD, String answer)
{
    QUESTION = islQuestion;
    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 getANSER()
{
    return ANSWER;
}
public void setID(int id)
{
    ID = id;
}
public void setQUESTION(String islQuestion)
{
    QUESTION = islQuestion;
}
public void setOPTA(String optA)
{
    OPTA = optA;
}
public void setOPTAB(String optab)
{
    OPTB = optab;
}
public void setOPTC(String optc)
{
    OPTC = optc;
}
public void setOPTD(String optd)
{
    OPTD = optd;
}
public  void setANSWER(String answer)
{
    ANSWER = answer;
}
}

2 个答案:

答案 0 :(得分:0)

替换你的Sql。

String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUESTION +
    "(" +KEY_ID +
 " INTEGER, "
            + KEY_QUESTION + " TEXT," + KEY_OPTA + " TEXT," +
   KEY_OPTB +     " TEXT," + 
   KEY_OPTC + " TEXT,"
            + KEY_OPTD + " TEXT,"+ KEY_ANSWER + " TEXT);" ;
例如,你需要KEY_OPTC和“TEXT”之间的空格,否则sql创建将是'optcTEXT'。

有空格:KEY_OPTC +“TEXT,”='optc TEXT,'

答案 1 :(得分:0)

INTEGER PRIMARY KEY AUTOINCREMENT, 检查SQLite语句

     String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUESTION +
    "(" +KEY_ID +
 "INTEGER, "
            + KEY_QUESTION + "TEXT," + KEY_OPTA + "TEXT," +
   KEY_OPTB +     "TEXT," + 
   KEY_OPTC + "TEXT,"
            + KEY_OPTD + "TEXT,"+ KEY_ANSWER + "TEXT)" ;

将其替换为

   String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUESTION +
    "(" +KEY_ID +
 "INTEGER PRIMARY KEY AUTOINCREMENT, "
            + KEY_QUESTION + " TEXT," + KEY_OPTA + " TEXT," +
   KEY_OPTB +     " TEXT," + 
   KEY_OPTC + " TEXT,"
            + KEY_OPTD + " TEXT,"+ KEY_ANSWER + " TEXT)" ;