S
Snipestyle
Erfahrenes Mitglied
- 3
Abend zusammen,
ich sitze vor ner kleinen Herausforderung. Ich möchte gerne eine bereitsvorhandene Datenbank in ein Android Projekt einbinden und schließlich ein Datensatz auslesen...
Ich habe dann mal gegooglet und einiges gefunden und auch viel ausprobiert, jedoch hat nichts so wirklich geklappt. Ich habe jetzt hier mal meinen letzten Stand inklusive Logcat und hoffe das mir jemand sagen kann, was ich hier falsch mache?
und hier dann Logcat:
Ich möchte wie gesagt wirklich garnichts großes machen, einfach einen Datensatz auslesen und hinterher halt im Layout ausgeben und das wars.
Hoffe ihr könnt mir helfen
ich sitze vor ner kleinen Herausforderung. Ich möchte gerne eine bereitsvorhandene Datenbank in ein Android Projekt einbinden und schließlich ein Datensatz auslesen...
Ich habe dann mal gegooglet und einiges gefunden und auch viel ausprobiert, jedoch hat nichts so wirklich geklappt. Ich habe jetzt hier mal meinen letzten Stand inklusive Logcat und hoffe das mir jemand sagen kann, was ich hier falsch mache?
PHP:
public class DataBaseHelper extends SQLiteOpenHelper{
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/de.example.databasehelper/databases/";
//private static String DB_PATH = "context.getApplicationInfo().dataDir/databases/";
// Data Base Name.
private static final String DATABASE_NAME = "datenbank.db";
// Data Base Version.
private static final int DATABASE_VERSION = 1;
// Table Names of Data Base.
static final String TABLE_Name1 = "message";
static final String TABLE_Name2 = "sequence";
static final String TABLE_Name3 = "chat";
public Context context;
static SQLiteDatabase sqliteDataBase;
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null ,DATABASE_VERSION);
this.context = context;
}
//check if the database exists
public void createDataBase() throws IOException{
boolean databaseExist = checkDataBase();
if(databaseExist){
// Do Nothing.
}else{
this.getWritableDatabase();
copyDataBase();
// TODO Auto-generated catch block
}
}// end if else dbExist
// end createDataBase().
public boolean checkDataBase(){
File databaseFile = new File(DB_PATH + DATABASE_NAME);
return databaseFile.exists();
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = context.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DATABASE_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
/**
* This method opens the data base connection.
* First it create the path up till data base of the device.
* Then create connection with data base.
*/
public void openDataBase() throws SQLException{
//Open the database
try {
createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String myPath = DB_PATH + DATABASE_NAME;
sqliteDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
/**
* This Method is used to close the data base connection.
*/
@Override
public synchronized void close() {
if(sqliteDataBase != null)
sqliteDataBase.close();
super.close();
}
//declare methods to fetch data
public Cursor getBasicCategoryDetails(){
return sqliteDataBase.rawQuery("Select data from messages where _id = 1", null);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
und hier dann Logcat:
PHP:
03-21 14:42:15.374: E/AndroidRuntime(2683): FATAL EXCEPTION: main
03-21 14:42:15.374: E/AndroidRuntime(2683): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{de.example.databasehelper/de.example.databasehelper.DataBaseHelper}: java.lang.InstantiationException: can't instantiate class de.example.databasehelper.DataBaseHelper; no empty constructor
03-21 14:42:15.374: E/AndroidRuntime(2683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
03-21 14:42:15.374: E/AndroidRuntime(2683): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-21 14:42:15.374: E/AndroidRuntime(2683): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-21 14:42:15.374: E/AndroidRuntime(2683): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-21 14:42:15.374: E/AndroidRuntime(2683): at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 14:42:15.374: E/AndroidRuntime(2683): at android.os.Looper.loop(Looper.java:137)
03-21 14:42:15.374: E/AndroidRuntime(2683): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-21 14:42:15.374: E/AndroidRuntime(2683): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 14:42:15.374: E/AndroidRuntime(2683): at java.lang.reflect.Method.invoke(Method.java:525)
03-21 14:42:15.374: E/AndroidRuntime(2683): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-21 14:42:15.374: E/AndroidRuntime(2683): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-21 14:42:15.374: E/AndroidRuntime(2683): at dalvik.system.NativeStart.main(Native Method)
03-21 14:42:15.374: E/AndroidRuntime(2683): Caused by: java.lang.InstantiationException: can't instantiate class de.example.databasehelper.DataBaseHelper; no empty constructor
03-21 14:42:15.374: E/AndroidRuntime(2683): at java.lang.Class.newInstanceImpl(Native Method)
03-21 14:42:15.374: E/AndroidRuntime(2683): at java.lang.Class.newInstance(Class.java:1130)
03-21 14:42:15.374: E/AndroidRuntime(2683): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
03-21 14:42:15.374: E/AndroidRuntime(2683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
03-21 14:42:15.374: E/AndroidRuntime(2683): ... 11 more
Ich möchte wie gesagt wirklich garnichts großes machen, einfach einen Datensatz auslesen und hinterher halt im Layout ausgeben und das wars.
Hoffe ihr könnt mir helfen