SQLite数据库崩溃应用程序

时间:2014-05-15 14:15:56

标签: android sqlite

我在我的应用程序中运行了一个数据库,之前的工作突然开始崩溃我的应用程序。我改变它试图添加第二个表崩溃应用程序,但当我恢复到工作版本时,应用程序仍然崩溃。我不知道为什么。这是代码

DBHelper

public class DBHelper extends  SQLiteOpenHelper
{
public DBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "triviaQuiz";
// 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 SQLiteDatabase dbase;

@Override
public void onCreate(SQLiteDatabase db) {
dbase=db;
String sql = "CREATE TABLE IF NOT EXIST " + TABLE_QUEST + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
+ " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, "
+KEY_OPTB +" TEXT, "+KEY_OPTC+" TEXT)";
db.execSQL(sql);
addQuestions();

//db.close();
}

private void addQuestions()
{
    Question q1=new Question("8 X 2 " + "=","16", "20", "18", "C");
    this.addQuestion(q1);
    Question q2=new Question("8 X 3  " +    "=", "20", "24", "26", "B");
    this.addQuestion(q2);
    Question q3=new Question("8 X 4 " + " =","30", "35","32","C");
    this.addQuestion(q3);
    Question q4=new Question("8 X 5 " + " = ", "40", "38", "41","A");
    this.addQuestion(q4);
    Question q5=new Question("8 X 6 " +" =","45","48","50","B");
    this.addQuestion(q5);
    Question q6=new Question("8 X 7 " +" =","55","56","58","B");
    this.addQuestion(q6);
    Question q7=new Question("8 X 8 " +" =","64","68","60","A");
    this.addQuestion(q7);
    Question q8=new Question("8 X 9 " +" =","75","72","70","B");
    this.addQuestion(q8);
    Question q9=new Question("8 X 10 " +" =","81","89","80","C");
    this.addQuestion(q9);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
// Create tables again
onCreate(db);
}
// Adding new question
public void addQuestion(Question quest) {
//SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_QUES, quest.getQUESTION());
values.put(KEY_ANSWER, quest.getANSWER());
values.put(KEY_OPTA, quest.getOPTA());
values.put(KEY_OPTB, quest.getOPTB());
values.put(KEY_OPTC, quest.getOPTC());
// Inserting Row
dbase.insert(TABLE_QUEST, null, values);
}
public List<Question> getAllQuestions() {
List<Question> quesList = new ArrayList<Question>();
// Select All Query
String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
dbase=this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
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));
quesList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
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;
}
}

Exercise1.java

public class Exercise1 extends Activity
{
List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
Button butNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.exercise1);
    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);
    butNext=(Button)findViewById(R.id.button1);
    setQuestionView();
    butNext.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.getANSWER().equals(answer.getText()))
            {
                score++;
            }
                if(qid<4)
                {
                    currentQ=quesList.get(qid);
                    setQuestionView();
                }
                else
                {
                    Intent intent = new Intent(Exercise1.this, Tables2.class);
                    Bundle b = new Bundle();
                    b.putInt("score", score); //Your score
                    intent.putExtras(b); //Put your score to your next Intent
                    startActivity(intent);
                    finish();
                }
        }


        });
}
private void setQuestionView()
{
    txtQuestion.setText(currentQ.getQUESTION());
    rda.setText(currentQ.getOPTA());
    rdb.setText(currentQ.getOPTB());
    rdc.setText(currentQ.getOPTC());
    qid++;
}

}

logcat

05-15 15:10:00.164: E/AndroidRuntime(14244): FATAL EXCEPTION: main
05-15 15:10:00.164: E/AndroidRuntime(14244): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.timestableseasy/com.example.timestableseasy.Exercise1}: android.database.sqlite.SQLiteException: near "EXIST": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXIST quest ( id INTEGER PRIMARY KEY AUTOINCREMENT, question TEXT, answer TEXT, opta TEXT, optb TEXT, optc TEXT)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.app.ActivityThread.access$600(ActivityThread.java:150)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.os.Looper.loop(Looper.java:213)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.app.ActivityThread.main(ActivityThread.java:5225)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at java.lang.reflect.Method.invokeNative(Native Method)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at java.lang.reflect.Method.invoke(Method.java:525)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at dalvik.system.NativeStart.main(Native Method)
05-15 15:10:00.164: E/AndroidRuntime(14244): Caused by: android.database.sqlite.SQLiteException: near "EXIST": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXIST quest ( id INTEGER PRIMARY KEY AUTOINCREMENT, question TEXT, answer TEXT, opta TEXT, optb TEXT, optc TEXT)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1682)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1611)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at com.example.timestableseasy.DBHelper.onCreate(DBHelper.java:39)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at com.example.timestableseasy.DBHelper.getAllQuestions(DBHelper.java:89)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at com.example.timestableseasy.Exercise1.onCreate(Exercise1.java:27)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.app.Activity.performCreate(Activity.java:5133)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-15 15:10:00.164: E/AndroidRuntime(14244):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
05-15 15:10:00.164: E/AndroidRuntime(14244):    ... 11 more

1 个答案:

答案 0 :(得分:1)

你应该写&#34; EXISTS&#34;而不是&#34; EXIST&#34;。