2016-11-12 105 views
0

我正在为班级作业创建一个琐事游戏。我最初想要将类别问题存储在sqlite数据库中。尽管我想为类别,级别和用户添加其他人,但我首先希望只用问题来解决问题。程序编译并安装到仿真器中,第一部分工作正常。直到应用程序去创建数据库,我得到一个错误。错误读取:在创建sqlite数据库的方法中出现错误

造成的:

android.database.sqlite.SQLiteException: near "TABLEquestion": syntax error (code 1): , while compiling: CREATE TABLEquestion(questionIDINTEGER PRIMARY KEY,questionNameTEXT,optionATEXT,optionBTEXT,optionCTEXT,answerTEXT,questionLevelIDINTEGER,categoryIDINTEGER) 
                          at android.database.sqlite 

我的主要活动代码,智力问题的活动,问题类,DBHelper类和对智力问题活动XML布局。请注意,当为某个类别选择单选按钮时会发生错误,该类别将启动琐事问题活动。 (在添加数据库之前,启动工作没有错误)。另外,我知道我需要创建更新和删除记录的方法,但希望首先使数据库创建工作。我已经看了2天,并找不到问题。任何援助将不胜感激。

CLASS问题:

public class Question { 
    private Integer questionID; 
    private String questionName; 
    private String optionA; 
    private String optionB; 
    private String optionC; 
    private String answer; 
    private Integer questionLevelID; 
    private Integer categoryID; 
    public Question(){ 
     //// TODO: 11/5/2016 
    } 
    public Question (Integer questionID, String questionName, String optionA, 
        String optionB, String optionC, String answer, Integer questionLevelID, 
        Integer catID){ 
     this.questionID=questionID; 
     this.questionName=questionName; 
     this.optionA=optionA; 
     this.optionB=optionB; 
     this.optionC=optionC; 
     this.answer=answer; 
     this.questionLevelID=questionLevelID; 
     this.categoryID = catID; 

    } 
    public void setqID(Integer questionId){ 
     this.questionID = questionId; 
    } 
    public void setqName(String questionName){ 
     this.questionName=questionName; 
    } 
    public void setqOptA(String optionA){ 
     this.optionA = optionA; 
    } 
    public void setqOptB(String optionB){ 
     this.optionB = optionB; 
    } 
    public void setqOptC(String optionC){ 
     this.optionC=optionC; 
    } 
    public void setqAns(String answer){ 
     this.answer = answer; 
    } 
    public void setQLevel(Integer questionLevelID){ 
     this.questionLevelID = questionLevelID; 
    } 
    public void setqcatID(Integer categoryID){ 
     this.categoryID= categoryID; 
    } 
    public int getqID(){ 
     return questionID; 
    } 
    public String getqName(){ 
     return questionName; 
    } 
    public String getqOptA(){ 
     return optionA; 
    } 
    public String getqOptB(){ 
     return optionB; 
    } 
    public String getqOptC(){ 
     return optionC; 
    } 
    public String getqAns(){ 
     return answer; 
    } 
    public Integer getqLevel(){ 
     return questionLevelID; 
    } 
    public Integer getqCatID(){ 
     return categoryID; 
    } 
} 

主要活动:

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.CompoundButton; 
import android.widget.RadioButton; 
import android.util.Log; 

import java.util.List; 


import static android.R.attr.id; 

public class EduTriviaMain extends AppCompatActivity { 

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


    } 

    public String onCheckedChanged(View view) { 
     boolean checked = ((RadioButton) view).isChecked(); 
     String category = ""; 
     switch (view.getId()) { 
      case R.id.englishRadioButton: 
       if (checked) { 
        category = "english"; 
        Intent intent = new Intent(this,TriviaQuestion.class); 
        startActivity(intent); 
        return category; 
       } 
       break; 

      case R.id.historyRadioButton: 
       if (checked) { 
       category = "history"; 
        Intent intent = new Intent(this,TriviaQuestion.class); 
        startActivity(intent); 
        return category; 
       } 
       break; 
      case R.id.mathRadioButton: 
       if (checked) { 
        category = "math"; 
        Intent intent = new Intent(this,TriviaQuestion.class); 
        startActivity(intent); 
        return category; 
       } 
       break; 
      default: 

       break; 

     } 
     return category; 
    } 
} 

DBHelper类:

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import java.util.ArrayList; 
import java.util.List; 

public class DBHandler extends SQLiteOpenHelper{ 
    //Database Version 
    private static final int DATABASE_VERSION=1; 
    //Database Name 
    private static final String DATABASE_NAME ="eduTrivia"; 
    //table names 

    private static final String TABLE_QUESTION="question"; 
    //question table column names 
    private static final String KEY_QUESTIONID = "questionID"; 
    private static final String KEY_QUESTION="questionName"; 
    private static final String KEY_OPTIONA="optionA"; 
    private static final String KEY_OPTIONB="optionB"; 
    private static final String KEY_OPTIONC="optionC"; 
    private static final String KEY_ANSWER="answer"; 
    private static final String KEY_LEVEL = "questionLevelID"; 
    private static final String KEY_CATEGORYID="categoryID"; 

    private static final String CREATE_TABLE_QUESTION ="CREATE TABLE" 
      + TABLE_QUESTION +"(" 
      + KEY_QUESTIONID +"INTEGER PRIMARY KEY," 
      + KEY_QUESTION + "TEXT," 
      + KEY_OPTIONA + "TEXT," 
      + KEY_OPTIONB + "TEXT," 
      + KEY_OPTIONC + "TEXT," 
      + KEY_ANSWER + "TEXT," 
      + KEY_LEVEL + "INTEGER," 
      + KEY_CATEGORYID + "INTEGER"+")"; 

      @Override 
    public void onCreate(SQLiteDatabase db) { 
      db.execSQL(CREATE_TABLE_QUESTION); 
     addQuestions(); 
     } 

     private void addQuestions(){ 
     Question q1 = new Question(1,"How do you write this number using words? 752", 
       "five hudnred sixty-two","seven hundred sixty-two", "seven hundred fifty-two", 
       "C",1,1); 
     Question q2 = new Question(2,"Round 5,764,438 to the nearest hundred thousand", 
       "6,200,000","5,800,000","5,700,000","B",1,1); 
     Question q3= new Question(3,"Which equation shows the associative property of addition", 
       "5+4=3+6","7+(4+3)=(7+4)+3", "0+8=8","B",1,1); 
     Question q4 = new Question(4,"Select the adjective in this sentence: Nina is a strong worker", 
       "Nina","strong","worker","B",1,2); 
     Question q5 = new Question (5,"Select the adjective in this sentence: The twon has three banks", 
       "The","town","three","C",1,2); 
      } 

      @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("drop table if exists"+TABLE_QUESTION); 
    onCreate(db); 
    } 
     //constructor and getInstance() method 
    private static DBHandler mDBHANDLER; 
     public static synchronized DBHandler getInstance(Context context) { 
      if (mDBHANDLER==null){ 
       mDBHANDLER=new DBHandler(context.getApplicationContext()); 
      } 
      return mDBHANDLER; 
     } 
    public DBHandler(Context context){ 
     super(context, DATABASE_NAME,null,DATABASE_VERSION); 
    } 
     public void addQuestion(Question question){ 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(KEY_QUESTIONID,question.getqID()); 
     values.put(KEY_QUESTION,question.getqName()); 
     values.put(KEY_OPTIONA,question.getqOptA()); 
     values.put(KEY_OPTIONB,question.getqOptB()); 
     values.put(KEY_OPTIONC,question.getqOptC()); 
     values.put(KEY_ANSWER,question.getqAns()); 
     values.put(KEY_LEVEL,question.getqLevel()); 
     values.put(KEY_CATEGORYID,question.getqCatID()); 
     db.insert(TABLE_QUESTION,null,values); 
     db.close(); 
    } 
      //reading records 

      public Question getQuestion(int id){ 
     SQLiteDatabase db=this.getReadableDatabase(); 
     Cursor cursor = db.query(TABLE_QUESTION, new String[]{ 
      KEY_QUESTIONID, KEY_QUESTION 
     },KEY_QUESTIONID + "=?", 
     new String[]{ 
       String.valueOf(id)},null,null,null,null); 
     if (cursor !=null) 
      cursor.moveToFirst(); 
     Question question = new Question(Integer.parseInt(cursor.getString(0)), 
       cursor.getString(1),cursor.getString(2), cursor.getString(3), 
       cursor.getString(4),cursor.getString(5),Integer.parseInt(cursor.getString(6)), 
       Integer.parseInt(cursor.getString(7))); 

     return question; 
    } 
    public List<Question> getAllQuestions(){ 
     //Select all questions query 
     List questionList = new ArrayList<Question>(); 
     String selectAll = "SELECT * FROM "+TABLE_QUESTION; 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectAll,null); 
     //loop through all rows and add to the list 
     if (cursor.moveToFirst()){ 
      do{ 
       Question question = new Question(); 
       question.setqID(Integer.parseInt(cursor.getString(0))); 
       question.setqName(cursor.getString(1)); 
       question.setqOptA(cursor.getString(2)); 
       question.setqOptB(cursor.getString(3)); 
       question.setqOptC(cursor.getString(4)); 
       question.setqAns(cursor.getString(5)); 
       //adding to list 
       questionList.add(question); 
      }while (cursor.moveToNext()); 
     } 
     return questionList; 
    } 
    } 

琐事问题的活动:

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 

import java.util.List; 

import static android.R.id.list; 

public class TriviaQuestion extends AppCompatActivity { 
    List<Question> questionList; 
    int score=0; 
    int questionID = 0; 
    Question currentQ; 
    TextView txtQuestion; 
    RadioButton rda,rdb,rdc; 
    Button next; 

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

     DBHandler db = new DBHandler(this); 
     questionList = db.getAllQuestions(); 
     currentQ = questionList.get(questionID); 
     txtQuestion=(TextView)findViewById(R.id.textView1); 
     rda=(RadioButton)findViewById(R.id.radio0); 
     rdb=(RadioButton)findViewById(R.id.radio1); 
     rdc=(RadioButton)findViewById(R.id.radio2); 
     setQuestionView(); 

     } 
     private void setQuestionView() { 
    txtQuestion.setText(currentQ.getqName()); 
    rda.setText(currentQ.getqOptA()); 
    rdb.setText(currentQ.getqOptB()); 
    rdc.setText(currentQ.getqOptC()); 

    next.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      RadioGroup grp = (RadioGroup) findViewById(R.id.radioGroup1); 
      RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId()); 
      if (currentQ.getqAns().equals(answer.getText())) { 
       score++; 
      } 
      currentQ = questionList.get(questionID); 
      setQuestionView(); 
     } 
    }); 
    } 

} 

琐事问题LAYOUT XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_trivia_question" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.rasmussenandroid.sandra.edutrivia.TriviaQuestion"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:orientation="vertical" > 
    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 
    <RadioGroup 
     android:id="@+id/radioGroup1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.04" > 
     <RadioButton 
      android:id="@+id/radio0" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:checked="true" 
      android:text="RadioButton" /> 
     <RadioButton 
      android:id="@+id/radio1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="RadioButton" /> 
     <RadioButton 
      android:id="@+id/radio2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="RadioButton" /> 
    </RadioGroup> 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="next" /> 
</LinearLayout> 

</RelativeLayout> 

回答

1

错误告诉你什么问题是 - SQL查询不正确构造。各个术语之间应该有空格 - 例如,CREATE TABLEquestion应该是CREATE TABLE问题。

试试下面这行CREATE_TABLE_QUESTION:

private static final String CREATE_TABLE_QUESTION ="CREATE TABLE " 
     + TABLE_QUESTION +" (" 
     + KEY_QUESTIONID +" INTEGER PRIMARY KEY, " 
     + KEY_QUESTION + " TEXT , " 
     + KEY_OPTIONA + " TEXT, " 
     + KEY_OPTIONB + " TEXT, " 
     + KEY_OPTIONC + " TEXT, " 
     + KEY_ANSWER + " TEXT, " 
     + KEY_LEVEL + " INTEGER, " 
     + KEY_CATEGORYID + " INTEGER "+")"; 
+0

谢谢。知道这是简单的事情。这解决了CREATE TABLE错误,但现在又出现了另一个错误。致命异常:主要 过程:com.rasmussenandroid.sandra.edutrivia,PID:17021java.lang.RuntimeException:无法启动活动TriviaQuestion}:java.lang.IndexOutOfBoundsException:指数:0,大小:0将尝试和卸载,然后重新安装继续前进。谢谢你的帮助! –

+0

如果您认为它是正确的,请接受答案!另外,你很多人都想向数据库添加一些初始问题,并在查找特定问题之前检查它是否为空。 – rhari

+0

我在DBHelper中的addQuestions函数中添加了初始问题,它使用数据库onCreate进行调用。我进行了一次检查,看看它是否已经创建,但仍然在排除异常。 –