我在使用应用时遇到了一些问题。我想实现SQLite以了解它是如何工作的,所以我把它放在一个简单的测验应用程序中。我使用其中一个在线教程作为基础。不幸的是,它因为我第一次使用SQLite而无法理解崩溃...
在点击开始新游戏活动的按钮后,它立即在菜单中崩溃。
Android Studio出错:
06-29 18:23:32.986 25997-25997/com.example.wojciech.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.wojciech.myapplication, PID: 25997
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wojciech.myapplication/com.example.wojciech.myapplication.nowa_g}: android.database.sqlite.SQLiteException: near "CREATE": syntax error (code 1): , while compiling: DROP TABLE IF EXISTS CREATE TABLE bazaPytan(id INTEGER PRIMARY KEY AUTOINCREMENT,question TEXT,choice1 TEXT, choice2 TEXT, choice3 TEXT, answer TEXT);
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: android.database.sqlite.SQLiteException: near "CREATE": syntax error (code 1): , while compiling: DROP TABLE IF EXISTS CREATE TABLE bazaPytan(id INTEGER PRIMARY KEY AUTOINCREMENT,question TEXT,choice1 TEXT, choice2 TEXT, choice3 TEXT, answer TEXT);
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:726)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1784)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1715)
at com.example.wojciech.myapplication.BazaDanychSQLite.onUpgrade(BazaDanychSQLite.java:44)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
at com.example.wojciech.myapplication.BazaDanychSQLite.getAllQuestionsList(BazaDanychSQLite.java:63)
at com.example.wojciech.myapplication.QuestionBank.initQuestions(QuestionBank.java:31)
at com.example.wojciech.myapplication.nowa_g.onCreate(nowa_g.java:42)
at android.app.Activity.performCreate(Activity.java:6272)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
游戏活动:
package com.example.wojciech.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class nowa_g extends AppCompatActivity {
private QuestionBank mQuestionLibrary = new QuestionBank();
private TextView Wynik; //score
private TextView WidokPytan; //view of a question
// private TextView zespol;
//private TextView album;
private Button wybor1; //choice1,2 etc...
private Button wybor2;
private Button wybor3;
private String odpowiedz;
private int score = 0;
private int numberOfQuestions = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nowa_g);
Wynik = (TextView) findViewById(R.id.wynik);
WidokPytan = (TextView) findViewById(R.id.checkedTextView);
wybor1 = (Button) findViewById(R.id.button1);
wybor2 = (Button) findViewById(R.id.button2);
wybor3 = (Button) findViewById(R.id.button3);
//zespol = (TextView) findViewById(R.id.zespol);
// album = (TextView) findViewById(R.id.album);
mQuestionLibrary.initQuestions(getApplicationContext());
newQuestion();
newScore(score);
}
private void newQuestion() {
if (numberOfQuestions < mQuestionLibrary.getLength()) {
WidokPytan.setText(mQuestionLibrary.getQuestion(numberOfQuestions));
wybor1.setText(mQuestionLibrary.getChoice(numberOfQuestions, 1));
wybor2.setText(mQuestionLibrary.getChoice(numberOfQuestions, 2));
wybor3.setText(mQuestionLibrary.getChoice(numberOfQuestions, 3));
// zespol.setText(mQuestionLibrary.getZespol(numberOfQuestions));
// album.setText(mQuestionLibrary.getAlbum(numberOfQuestions));
odpowiedz = mQuestionLibrary.getCorrectAnswer(numberOfQuestions);
numberOfQuestions++;
} else {
Toast.makeText(nowa_g.this, "To było ostatnie pytanie!", Toast.LENGTH_SHORT).show();
Intent i = new Intent(nowa_g.this, NajwyzszyWynik.class);
i.putExtra("score", score);
startActivity(i);
}
}
private void newScore(int punkt) {
Wynik.setText(score);
}
public void onClick(View view){
Button answer = (Button) view;
if(answer.getText().equals(odpowiedz)){
score = score + 1;
Toast.makeText(nowa_g.this,"Dobra odpowiedź!",Toast.LENGTH_SHORT).show(); //correct answer
}else{
Toast.makeText(nowa_g.this,"Zła odpowiedź!",Toast.LENGTH_SHORT).show(); //wrong answer
}
newScore(score);
newQuestion();
}
}
数据库助手
package com.example.wojciech.myapplication;
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.Collections;
import java.util.List;
public class BazaDanychSQLite extends SQLiteOpenHelper {
public static String DATABASE_QUESTION = "pytania.db";
public static final int DATABASE_VERSION = 3;
public static final String TABLE_QUESTION = "bazaPytan";
public static final String KEY_ID = "id";
public static final String QUESTION = "question";
public static final String CHOICE1 = "choice1";
public static final String CHOICE2 = "choice2";
public static final String CHOICE3 = "choice3";
public static final String ANSWER = "answer";
private static final String CREATE_TABLE_QUESTION = "CREATE TABLE "
+ TABLE_QUESTION + "(" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT," + QUESTION + " TEXT,"
+ CHOICE1 + " TEXT, " + CHOICE2 + " TEXT, " + CHOICE3 + " TEXT, "
+ ANSWER + " TEXT);";
public BazaDanychSQLite(Context context) {
super(context, DATABASE_QUESTION, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_QUESTION);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+CREATE_TABLE_QUESTION);
onCreate(db);
}
public long addInitialQuestion (Question question){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(QUESTION, question.getQuestion());
values.put(CHOICE1, question.getChoice(0));
values.put(CHOICE2, question.getChoice(1));
values.put(CHOICE3, question.getChoice(2));
values.put(ANSWER, question.getAnswer());
long insert = db.insert(TABLE_QUESTION, null, values);
return insert;
}
public List<Question> getAllQuestionsList() {
List<Question> questionArrayList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_QUESTION;
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all records and adding to the list
if (c.moveToFirst()) {
do {
Question question = new Question();
String questText= c.getString(c.getColumnIndex(QUESTION));
question.setQuestion(questText);
String choice1Text= c.getString(c.getColumnIndex(CHOICE1));
question.setChoice(0,choice1Text);
String choice2Text= c.getString(c.getColumnIndex(CHOICE2));
question.setChoice(1,choice2Text);
String choice3Text= c.getString(c.getColumnIndex(CHOICE3));
question.setChoice(2,choice3Text);
String answerText= c.getString(c.getColumnIndex(ANSWER));
question.setAnswer(answerText);
// adding to Questions list
questionArrayList.add(question);
} while (c.moveToNext());
Collections.shuffle(questionArrayList);
}
return questionArrayList;
}
}
问题库:
public class QuestionBank {
List <Question> list = new ArrayList<>();
BazaDanychSQLite myDataBaseHelper;
public int getLength(){
return list.size();
}
public String getQuestion(int a) {
return list.get(a).getQuestion();
}
public String getChoice(int index, int num) {
return list.get(index).getChoice(num-1);
}
public String getCorrectAnswer(int a) {
return list.get(a).getAnswer();
}
public void initQuestions(Context context) {
myDataBaseHelper = new BazaDanychSQLite(context);
list = myDataBaseHelper.getAllQuestionsList();
if (list.isEmpty()) {
myDataBaseHelper.addInitialQuestion(new Question("Seems like everybody's got a _____",
new String[]{"price", "prince", "quack"}, "price"));
myDataBaseHelper.addInitialQuestion(new Question("We don't need your ____, ____, ____\n",
new String[]{"money", "honey", "quack"}, "money"));
myDataBaseHelper.addInitialQuestion(new Question("We just wanna make the world _____",
new String[]{"dance", "quack", "spin"}, "dance"));
list = myDataBaseHelper.getAllQuestionsList();
}
}
}
答案 0 :(得分:1)
替换:
db.execSQL("DROP TABLE IF EXISTS "+CREATE_TABLE_QUESTION);
使用:
db.execSQL("DROP TABLE IF EXISTS bazaPytan");
CREATE_TABLE_QUESTION
不是bazaPytan
。 CREATE_TABLE_QUESTION
是完整的CREATE TABLE
语句,不属于DROP TABLE
语句的一部分。