Android-手动插入SQL

时间:2018-11-05 20:01:45

标签: java android sql database intellij-idea

我目前正在使用一个与数据库链接的问题的android测验应用程序,但是,我能够创建已创建的数据库,但是以某种方式无法手动插入诸如问题列表之类的值。当我打开数据库时,它已经组织了我想要的列,但是没有显示任何值,例如“示例问题”。我是否缺少某些代码行?

非常感谢

public class DatabaseHelper extends SQLiteOpenHelper
{

   public static final String DATABASE_NAME = "Questions.db";
   public static final String TABLE_NAME = "Questions";
   public static final String QUESTION_NUMBER = "Question_Number";
   public static final String QUESTION = "Question";
   public static final String ANSWER_ONE = "Answer_ONE";
   public static final String ANSWER_TWO = "Answer_THREE";
   public static final String ANSWER_THREE = "Answer_FOUR";

   private SQLiteDatabase db;



   public DatabaseHelper(Context context)
   {
      super(context,DATABASE_NAME, null, 1);
      SQLiteDatabase db = this.getWritableDatabase();
   }

   @Override
   public void onCreate(SQLiteDatabase db)
   {
      this.db = db;
      final String SQL_CREATE_QUESTIONS_TABLE = "CREATE TABLE " +
            TABLE_NAME + " ( " +
            QUESTION_NUMBER + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            QUESTION + " TEXT, " +
            ANSWER_ONE + " TEXT, " +
            ANSWER_TWO + " TEXT, " +
            ANSWER_THREE + " TEXT " +
            ")";

      db.execSQL(SQL_CREATE_QUESTIONS_TABLE);
      fillQuestionTable();


   }

   @Override
   public void onUpgrade(SQLiteDatabase db, int i, int i1)
   {
      db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
      onCreate(db);
   }

   private void fillQuestionTable()
   {
      Question q1 = new Question(1,"Example Question?","Yes","Sometimes","No");
      addQuestion(q1);
   }

   private void addQuestion (Question question)
   {
      SQLiteDatabase db = this.getWritableDatabase();
      ContentValues cv = new ContentValues();
      cv.put(QUESTION_NUMBER, question.getQuestionNum());
      cv.put(QUESTION, question.getQuestion());
      cv.put(ANSWER_ONE, question.getOption1());
      cv.put(ANSWER_TWO, question.getOption2());
      cv.put(ANSWER_THREE, question.getOption3());
      db.insert(TABLE_NAME,null,cv);
   }

}

这是活动类

public class MainActivity extends Activity implements View.OnClickListener
{
   Button screenOneButton;
   Button screenTwoButton;
   Button screenThreeButton;
   Button quitButton;
   DatabaseHelper mydb;


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

      DatabaseHelper mydb = new DatabaseHelper(this);
}

这是在Intellij软件下编程的,并且能够在DB SQL软件FYI下打开数据库!

非常感谢

0 个答案:

没有答案