向数据库添加问题

时间:2017-01-02 06:00:24

标签: android sqlite android-sqlite

我想添加问题,选项,回答数据库。我创建了有5列的表 idQUESTIONOPTION 1OPTION 2OPTION 3ANSWER

基本上,我正在尝试创建一个测验类型的游戏,所以我想将问题,选项及其答案存储在我的表格中。为此,我创建了:

  • Helperclass,其中SQLiteOpenHelper
  • Questions类,其构造函数包含5个参数,即问题,3个选项和答案,并在我的mainactivity.xml

我没有太多只显示问题的文本视图和显示选项的三个按钮。

我收到数据库停止错误但我知道我非常接近,有一些小错误。

logcat窗口说:

  

---引起:java.lang.IllegalStateException:递归调用getDatabase。

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    helperclassAdapter = new HelperclassAdapter(this);
    tv1 = (TextView) findViewById(R.id.labelquestion);
    b1 = (Button) findViewById(R.id.opt1);
    b2 = (Button) findViewById(R.id.opt2);
    b3 = (Button) findViewById(R.id.opt3);
    SQLiteDatabase sqLiteDatabase = helperclassAdapter.helperclass.getWritableDatabase();
}

public void clickopt1(View view) {

}

public void clickopt2(View view) {

}

public void clickopt3(View view) {

}

HelperclassAdapter.java

public class HelperclassAdapter {
Helperclass helperclass;
Context context;

public HelperclassAdapter(Context context) {
    helperclass = new Helperclass(context);
    this.context = context;
}
static public class Helperclass extends SQLiteOpenHelper {
    //i added the SQLiteDatabase object over here and using this object       called insert method
    SQLiteDatabase sqLiteDatabase;
    Context context;
    private static final String DATABASE_NAME = "DATABASE1";
    private static final int DATABASE_VERSION = 39;
    private static final String TABLE_NAME = "TABLE1";
    private static final String UID = "_ID";
    private static final String QUESTION = "QUESTION";
    private static final String OPT1 = "OPT1";
    private static final String OPT2 = "OPT2";
    private static final String OPT3 = "OPT3";
    private static final String ANSWER = "ANSWER";
    private static final String CREATE_TABLE =
            "CREATE TABLE " + TABLE_NAME + " (" + UID + " INTEGER PRIMARY KEY, " + QUESTION + " VARCHAR(255), " + OPT1 + " VARCHAR(255), " + OPT2 + " VARCHAR(255), " + OPT3 + " VARCHAR(255), " + ANSWER + " VARCHAR(255));";
    private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;


    public Helperclass(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
        Toast.makeText(context, "constructor called", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        try {
            sqLiteDatabase.execSQL(CREATE_TABLE);
            addquestions();
            Toast.makeText(context, "oncreate called", Toast.LENGTH_SHORT).show();
        } catch (SQLException e) {
            e.printStackTrace();
            Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        try {
            Toast.makeText(context, "onupgrade called", Toast.LENGTH_SHORT).show();
            sqLiteDatabase.execSQL(DROP_TABLE);
            onCreate(sqLiteDatabase);
        } catch (SQLException e) {
            e.printStackTrace();
            Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
    public void addquestions() {
        Questions q1 = new Questions("what is your hobby?", "singing", "dancing", "reading", "sining");
        this.addingeachquestions(q1);
        Questions q2 = new Questions("how are you?", "fine", "good", "sad", "good");
        this.addingeachquestions(q2);
        Questions q3 = new Questions("whats your name?", "sam", "jhon", "alice", "sam");
        this.addingeachquestions(q3);
        Toast.makeText(context, "addquestion method called", Toast.LENGTH_SHORT).show();
    }
   //BEFORE
    private void addingeachquestions(Questions question) {
        ContentValues values = new ContentValues();
        values.put(Helperclass.QUESTION, question.getQUESTION());
        values.put(Helperclass.OPT1, question.getOPTA());
        values.put(Helperclass.OPT2, question.getOPTB());
        values.put(Helperclass.OPT3, question.getOPTC());
        values.put(Helperclass.ANSWER, question.getANSWER());
        SQLiteDatabase db = getWritableDatabase();
        db.insert(Helperclass.TABLE_NAME, null, values);
        Toast.makeText(context, "questions added", Toast.LENGTH_SHORT).show();
    }
}
     //AFTER
    private void addingeachquestions(Questions question) {
        ContentValues values = new ContentValues();
        values.put(Helperclass.QUESTION, question.getQUESTION());
        values.put(Helperclass.OPT1, question.getOPTA());
        values.put(Helperclass.OPT2, question.getOPTB());
        values.put(Helperclass.OPT3, question.getOPTC());
        values.put(Helperclass.ANSWER, question.getANSWER());
        sqLiteDatabase.insert(Helperclass.TABLE_NAME, null, values);
        Toast.makeText(context, "questions added", Toast.LENGTH_SHORT).show();
    }

Questions.java

public Questions() {
    Question = "";
    Opt1 = "";
    Opt2 = "";
    Opt3 = "";
    Answer = "";
}

public Questions(String qUESTION, String oPT1, String oPT2, String oPT3,
                 String aNSWER) {
    Question = qUESTION;
    Opt1 = oPT1;
    Opt2 = oPT2;
    Opt3 = oPT3;

    Answer = aNSWER;
}

public String getQUESTION() {
    return Question;
}

public String getOPTA() {
    return Opt1;
}

public String getOPTB() {
    return Opt2;
}

public String getOPTC() {
    return Opt3;
}

public String getANSWER() {
    return Answer;
}

1 个答案:

答案 0 :(得分:-1)

  1. 我建议你使用ContentProvider来使用数据库。如果你打招呼它看起来很可怕但你必须学习它!
  2. 内容提供商是您和其他应用(如果您选择)以正确方式访问数据库的一种方式。

    ContentProvider documentation

    1. 我认为hp变量不是intalized?

      @Override
      public void onCreate(SQLiteDatabase sqLiteDatabase) {
          try {
              sqLiteDatabase.execSQL(CREATE_TABLE);
      
      // hp isnt intallized?
      
              hp.addquestions();
      
              Toast.makeText(context, "oncreate called", Toast.LENGTH_SHORT).show();
          } catch (SQLException e) {
              e.printStackTrace();
              Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
          }
      }
      
    2. 阅读我的评论,再次阅读有关contentProvider的内容!它非常重要,不仅适用于创建自己的数据库,还适用于您使用其他数据库。 (比如从图库中获取图像)

      
      
          private void addingeachquestions(Questions question) {
              ContentValues values = new ContentValues();
              values.put(Helperclass.QUESTION, question.getQUESTION());
              values.put(Helperclass.OPT1, question.getOPTA());
              values.put(Helperclass.OPT2, question.getOPTB());
              values.put(Helperclass.OPT3, question.getOPTC());
              values.put(Helperclass.ANSWER, question.getANSWER());
      
      //getWritableDatabase calls the onCreate() methos and the onCreate() method calls             addquestions(); which calls getWriteableDatabase. this is recrusive call, a method that makes infinity loop that crush your app.
      You should add the questions OUTSIDE the Helperclass.
      
              SQLiteDatabase db = getWritableDatabase();
              db.insert(Helperclass.TABLE_NAME, null, values);
              Toast.makeText(context, "questions added", Toast.LENGTH_SHORT).show();
          }