I dont know whats the problem. I think "the app" doas not copy the Database form the asset folder to the android filesystem.
Here is my Code:
package san.tal.tfapp.Database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by Talsan on 17.02.2015.
*/
public class DatabaseHelperStations extends SQLiteOpenHelper {
//Hier oder dort liegt die Datenbank...vielleicht ;)
private static String DB_PATH = "/data/data/san.tal.tfapp/databases/";
protected static String DB_NAME = "stations";
private SQLiteDatabase myDatabase;
private final Context myContext;
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "station_name";
public static final String COLUMN_RIL100 = "station_ril100";
public static final String COLUMN_CKANAL = "station_ckanal";
public static final String COLUMN_LASTSTRECKE = "station_lastrecke";
public static final String COLUMN_LINIE = "station_linie";
public static final String COLUMN_FDL = "station_fdl";
public static final String[] ALL_COLUMNS = new String[] {COLUMN_ID, COLUMN_NAME, COLUMN_RIL100, COLUMN_CKANAL,
COLUMN_LASTSTRECKE, COLUMN_LINIE, COLUMN_FDL};
// Constructor
public DatabaseHelperStations(Context context) {
super(context, DB_NAME, null, 3);
this.myContext = context;
}
// Erstellt eine leere DB und überschreibt sie mit der eigenen DB
public void createDatabase() throws IOException {
boolean dbExist = checkDatabase();
if (dbExist){
//mach nichts weil die DB existiert
}else{
this.getReadableDatabase();
try {
copyDatabase();
}catch (IOException e) {
throw new Error("Fehler beim Kopieren der Datenbank");
}
}
}
// Prüft ob die DB bereits existiert
private boolean checkDatabase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch (SQLiteException e) {
//DB existiert noch nicht
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
// Kopiert die DB aus der lokalen Assets-Folder in die so eben erstellte leere DB im Systemverzeichniss.
// Von dort kann sie angesprochen werden
private void copyDatabase() throws IOException {
//Öffnet die lokale DB als input
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Pfad der eben erstellten DB
String outFileName = DB_PATH + DB_NAME;
//Öffnet die leere DB als Output Stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes von inputfile zum outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0) {
myOutput.write(buffer, 0, length);
}
//schließt die Streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDatabase() throws SQLiteException {
//Öffnet die DB
String myPath = DB_PATH + DB_NAME;
myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if (myDatabase != null) myDatabase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
The Log:
02-17 21:36:29.948 22303-22303/san.tal.tfapp E/SQLiteLog﹕ (1) no such table: stations 02-17 21:36:29.948 22303-22303/san.tal.tfapp D/AndroidRuntime﹕ Shutting down VM 02-17 21:36:29.948 22303-22303/san.tal.tfapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41617ce0) 02-17 21:36:29.978 22303-22303/san.tal.tfapp E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: san.tal.tfapp, PID: 22303 android.database.sqlite.SQLiteException: no such table: stations (code 1): , while compiling: SELECT * FROM stations WHERE station_name = r OR station_ril100 = r; at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58) at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:37) at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253) at san.tal.tfapp.Database.DatabaseSourceStations.searchStation(DatabaseSourceStations.java:67) at san.tal.tfapp.Bahnhoefe.ShowStationsSuche$1.onClick(ShowStationsSuche.java:39) at android.view.View.performClick(View.java:4445) at android.view.View$PerformClick.run(View.java:18446) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5146) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) at dalvik.system.NativeStart.main(Native Method)
If any other infos are required, pls let me know :) Thank you for the help :)
Aucun commentaire:
Enregistrer un commentaire