Datenbanktabelle existiert nicht

  • 6 Antworten
  • Neuester Beitrag
Diskutiere Datenbanktabelle existiert nicht im Android App Entwicklung im Bereich Betriebssysteme & Apps.
Z

zblack

Gast
Ich brauche Hilfe. Ich habe meine Datenbank und die Helper klasse wie hier gezeigt wurde : Using your own SQLite database in Android applications | ReignDesign erstellt. Die Datenbank wurde auch mit dem Tool SQLite Database Browser erstellt. Ich kriege keine Fehlermeldung wenn ich die Datenbank aufmache. Wenn ich aber was aus der Datenbank auslesen möchte kriege ich die SQL Meldung dass die Tabelle mit dem angebenem Namen nicht exisitert. Die Tabelle ist aber in der Datenbank und sie hat ein Eintrag drin. Der Code der Helper Klasse ist hier:
Code:
public class DataBaseHelper extends SQLiteOpenHelper {

    private static String DB_PATH = "/data/data/de.meineapp/databases/";
    private static String DB_NAME = "testDB";
    private SQLiteDatabase myDatabase;
    private final Context myContext;
    private static String DB_TABLE = "Test";
    
    public static final String ROWID 
    = "_id";
    public static final String NAME 
    = "MyName";
    public static final String TELEFON
    = "MyTelefon";
    
    public DataBaseHelper(Context context) {
        super(context, DB_NAME, null, 1);
        // TODO Auto-generated constructor stub
        this.myContext = context;
    }

    public void createDataBase () throws IOException{
        boolean dbExist = checkDataBase ();
        if (dbExist){         
        }
        else{
            this.getReadableDatabase();
            try {
                copyDB ();                
            }
            catch (IOException e){
                throw new Error ("Error createDataBase");
            }
        }
    }
    
    private void copyDB() throws IOException {
        // TODO Auto-generated method stub
        InputStream myInput = myContext.getAssets().open(DB_NAME);
        String outFile = DB_PATH + DB_NAME;
        OutputStream out = new FileOutputStream(outFile);
        
        byte [] buffer = new byte [1024];
        int length;
        while ((length = myInput.read(buffer)) > 0){
            out.write(buffer, 0 ,length);
        }
        out.flush();
        out.close();
        myInput.close();
    }
    
    public void openDataBase ()  throws SQLException{
        String path = DB_PATH + DB_NAME;
        myDatabase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
    }

    @Override
    public synchronized void close() {
        // TODO Auto-generated method stub
        if (myDatabase != null){
            myDatabase.close();
        }
        super.close();
    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
        try {
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        }
        catch (SQLiteException e) {
            // TODO: handle exception
        }
        
        if (checkDB != null){
            checkDB.close();
        }
        return checkDB != null ? true : false;    
    }

    /* (non-Javadoc)
     * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
    }
    
    public String getName () throws SQLException
    {
        String [] columns = new String [] {ROWID, NAME, TELEFON};
        Cursor c = myDatabase.query(DB_TABLE, columns, ROWID + "=" + 1, null, null, null, null);
        if (c != null){
            c.moveToFirst();
            String name = c.getString(1);
            return name;
        }
        return null;
    }

    /* (non-Javadoc)
     * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
    }
}
 
Z

zblack

Gast
egal wie ich die Tabelle in der Datenbank benenne kommt die Meldung

01-05 21:44:23.853: I/Zygote(28397): Zygote: pid 28397 has INTERNET permission, then set capability for CAP_NET_RAW(13)
01-05 21:44:24.803: I/ActivityThread(28397): queueIdle
01-05 21:44:24.803: V/ActivityThread(28397): Reporting idle of ActivityRecord{4a4056a8 token=android.os.BinderProxy@4a405118 {de.meinautohaus/de.meinautohaus.AppStartSeteActivity}} finished=false
01-05 21:44:24.803: W/ActivityNative(28397): send ACTIVITY_IDLE_TRANSACTION
01-05 21:44:33.323: I/ActivityThread(28397): queueIdle
01-05 21:44:33.323: V/ActivityThread(28397): Reporting idle of ActivityRecord{4a415e98 token=android.os.BinderProxy@4a415910 {de.meinautohaus/de.meinautohaus.StartLogosActivity}} finished=false
01-05 21:44:33.323: W/ActivityNative(28397): send ACTIVITY_IDLE_TRANSACTION
01-05 21:44:34.373: I/ActivityThread(28397): queueIdle
01-05 21:44:34.373: V/ActivityThread(28397): Reporting idle of ActivityRecord{4a3ff268 token=android.os.BinderProxy@4a40acb0 {de.meinautohaus/de.meinautohaus.MeinAutohausStartActivity}} finished=false
01-05 21:44:34.373: W/ActivityNative(28397): send ACTIVITY_IDLE_TRANSACTION
01-05 21:49:52.753: I/ActivityThread(28477): queueIdle
01-05 21:49:52.753: V/ActivityThread(28477): Reporting idle of ActivityRecord{4a5056a8 token=android.os.BinderProxy@4a505118 {de.meinautohaus/de.meinautohaus.AppStartSeteActivity}} finished=false
01-05 21:49:52.753: W/ActivityNative(28477): send ACTIVITY_IDLE_TRANSACTION
01-05 21:49:54.543: I/ActivityThread(28477): queueIdle
01-05 21:49:54.553: V/ActivityThread(28477): Reporting idle of ActivityRecord{4a515eb8 token=android.os.BinderProxy@4a515930 {de.meinautohaus/de.meinautohaus.StartLogosActivity}} finished=false
01-05 21:49:54.553: W/ActivityNative(28477): send ACTIVITY_IDLE_TRANSACTION
01-05 21:49:55.323: W/dalvikvm(28477): threadid=1: thread exiting with uncaught exception (group=0x400207d8)
01-05 21:49:55.333: E/AndroidRuntime(28477): FATAL EXCEPTION: main
01-05 21:49:55.333: E/AndroidRuntime(28477): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.meinautohaus/de.meinautohaus.MeinAutohausStartActivity}: android.database.sqlite.SQLiteException: no such table: mytable: , while compiling: SELECT _id, MyName, MyTelefon FROM mytable
01-05 21:49:55.333: E/AndroidRuntime(28477): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
01-05 21:49:55.333: E/AndroidRuntime(28477): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
01-05 21:49:55.333: E/AndroidRuntime(28477): at android.app.ActivityThread.access$2300(ActivityThread.java:126)
01-05 21:49:55.333: E/AndroidRuntime(28477): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
01-05 21:49:55.333: E/AndroidRuntime(28477): at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 21:49:55.333: E/AndroidRuntime(28477): at android.os.Looper.loop(Looper.java:123)
01-05 21:49:55.333: E/AndroidRuntime(28477): at android.app.ActivityThread.main(ActivityThread.java:4633)
01-05 21:49:55.333: E/AndroidRuntime(28477): at java.lang.reflect.Method.invokeNative(Native Method)
01-05 21:49:55.333: E/AndroidRuntime(28477): at java.lang.reflect.Method.invoke(Method.java:521)
01-05 21:49:55.333: E/AndroidRuntime(28477): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
01-05 21:49:55.333: E/AndroidRuntime(28477): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-05 21:49:55.333: E/AndroidRuntime(28477): at dalvik.system.NativeStart.main(Native Method)
01-05 21:49:55.333: E/AndroidRuntime(28477): Caused by: android.database.sqlite.SQLiteException: no such table: mytable: , while compiling: SELECT _id, MyName, MyTelefon FROM mytable
01-05 21:49:55.333: E/AndroidRuntime(28477): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
01-05 21:49:55.333: E/AndroidRuntime(28477): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
01-05 21:49:55.333: E/AndroidRuntime(28477): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
01-05 21:49:55.333: E/AndroidRuntime(28477): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
01-05 21:49:55.333: E/AndroidRuntime(28477): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
01-05 21:49:55.333: E/AndroidRuntime(28477): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
01-05 21:49:55.333: E/AndroidRuntime(28477): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1454)
01-05 21:49:55.333: E/AndroidRuntime(28477): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1338)
01-05 21:49:55.333: E/AndroidRuntime(28477): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1293)
01-05 21:49:55.333: E/AndroidRuntime(28477): at de.meinautohaus.DataBaseHelper.getName(DataBaseHelper.java:132)
01-05 21:49:55.333: E/AndroidRuntime(28477): at de.meinautohaus.MeinAutohausStartActivity.onCreate(MeinAutohausStartActivity.java:78)
01-05 21:49:55.333: E/AndroidRuntime(28477): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-05 21:49:55.333: E/AndroidRuntime(28477): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)
01-05 21:49:55.333: E/AndroidRuntime(28477): ... 11 more
 
S

swordi

Lexikon
die metadata tabelle mit eintrag hast angelegt ?
 
Z

zblack

Gast
Hast recht , die hat gefehlt, aber warum braucht man diese Tabelle ?
kann man auch ohne die auskommen ?
 
S

swordi

Lexikon
wenn in der anleitung steht, android braucht sie, dann wird sie android wohl brauchen :D

gehts denn jetzt ?
 
Z

zblack

Gast
aso, ja, es geht jetzt auf jedem fall
 
S

swordi

Lexikon
naja dann erübrigt sich die frage ob man auch ohne die auskommen kann :D