私はデータベースを開いてそこからデータを取得したいと思います。私はこれについてのチュートリアルを読んで、アプリケーションを作った。私は、データベースが存在するかどうかを確認しようとしている場合は、アプリケーションデータベースのAndroidのデフォルトのシステムパスに新しいデータベースを作成し、そこから(アセットフォルダにある)データベースにデータをコピーします。Android - オープンSQLiteデータベース
SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase$CursorFactory, String, String[], String) line: 1568
は、誰かが私が間違って作られたものについていくつかのアドバイスを与えることができます:私は、アプリケーションを実行しているときしかし、私はエラーを得ましたか?ここでは、コードは以下のとおりです。クラスレベルここ
private static String ASSETS_DB_FOLDER = "db";
デシベルで
package thesis.app.quiz;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelperForQuiz extends SQLiteOpenHelper {
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/thesis.app.quiz/databases/";
private static String DB_NAME = "QuizDB";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DBHelperForQuiz(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void openDataBase() {
String myDbPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myDbPath, null,
SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
public List<Question> getQuestionsList(int difficulty, int numberOfQuestions) {
List<Question> questionsList = new ArrayList<Question>();
Cursor c = myDataBase.rawQuery("SELECT * FROM questions", null);
while (c.moveToNext()) {
Question q = new Question();
q.setQuestionText(c.getString(1));
q.setRightAnswer(c.getString(2));
q.setAnswerOption1(c.getString(3));
q.setAnswerOption2(c.getString(4));
q.setAnswerOption3(c.getString(5));
q.setQuestionDiffLevel(difficulty);
questionsList.add(q);
}
return questionsList;
}
public void createDataBase() throws IOException {
if (!checkIfDBAlredyExist()) {
this.getReadableDatabase();
try {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String newDbPath = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(newDbPath);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Error copying database");
}
}
}
public boolean checkIfDBAlredyExist() {
SQLiteDatabase tmpDb = null;
String dbPath = DB_PATH + DB_NAME;
try {
tmpDb = SQLiteDatabase.openDatabase(dbPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLException e) {
e.printStackTrace();
}
if (tmpDb != null) {
tmpDb.close();
//return true;
}
return tmpDb != null ? true : false;
}
}
これは実際のエラーのようには見えません。 –
LogCatの出力を投稿してください – Asahi
ごめんなさい。私はLogCatの出力を保存しています。何か気づいたので、何かを見つけようとしています。たぶん私はこの問題を解決することができます。私はあなたの時間を無駄にしたくありません。私が再び失敗するなら、私はここにログをコピーします。 – user1036555