SQLiteException: no such table

P

Panikmeister

Neues Mitglied
0
Hi Leute,

ich habe jetzt 4 Stunden gegooglet und probiert und probiert, aber weiß nicht mehr weiter.

Ich habe eine Datenbank (ist nur eine TestDB, nachdem es mit der richtigen auch nicht geklappt hatte) erstellt mit "Database Browser for SQLite":



Wichtig sind ja der Unterstrich für die id-Spalten und die android_metadata Spalte.

Ich bekomme trotzdem IMMER den Fehler, dass die Tabelle angeblich nicht existiert.



So sieht die Helperklasse aus und auf der linken Seite kann man die beiden DB-Daten erkennen. Das Fragezeichen verwundert mich ein wenig.


Der Code der Helperklasse:

Code:
public class DataBaseHelper extends SQLiteOpenHelper {

    //The Android's default system path of your application database.
    private static String DB_PATH = "";

    private static String DB_NAME = "testdb";

    private SQLiteDatabase dataBase;

    private final Context myContext;

    /**
     * Constructor
     * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
     * @param context
     */
    public DataBaseHelper(Context context) {

        super(context, DB_NAME, null, 1);// 1? its Database Version
        if(android.os.Build.VERSION.SDK_INT >= 17){
            DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
        }
        else
        {
            DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
        }

        this.myContext = context;
    }

    /**
     * Creates a empty database on the system and rewrites it with your own database.
     * */
    public void createDataBase() throws IOException {

        boolean dbExist = checkDataBase();

        if(dbExist){
            //do nothing - database already exist
        }else{

            //By calling this method and empty database will be created into the default system path
            //of your application so we are gonna be able to overwrite that database with our database.
            this.getReadableDatabase();

            try {

                copyDataBase();

            } catch (IOException e) {

                throw new Error("Error copying database");

            }
        }

    }

    /**
     * Check if the database already exist to avoid re-copying the file each time you open the application.
     * @return true if it exists, false if it doesn't
     */
    private boolean checkDataBase(){

        SQLiteDatabase checkDB = null;

        try{
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        }catch(SQLiteException e){

            //database does't exist yet.

        }

        if(checkDB != null){

            checkDB.close();

        }

        return checkDB != null ? true : false;
    }

    /**
     * Copies your database from your local assets-folder to the just created empty database in the
     * system folder, from where it can be accessed and handled.
     * This is done by transfering bytestream.
     * */
    private void copyDataBase() throws IOException{

        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;

        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        //transfer bytes from the inputfile to the outputfile
        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();

    }

    public void openDataBase() throws SQLException {

        String myPath = DB_PATH + DB_NAME;
        dataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }

    @Override
    public synchronized void close() {

        if(dataBase != null)
            dataBase.close();

        super.close();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


    public SQLiteDatabase getDataBase() {
        return dataBase;
    }

    public void setDataBase(SQLiteDatabase dataBase) {
        this.dataBase = dataBase;
    }
}

Code:
public class Datasource {

    private Context context;

    private SQLiteDatabase database;

    private static final String COL_PERSON = "person";

    public Datasource(final Context con){
        this.context = con;


        DataBaseHelper myDbHelper = new DataBaseHelper(this.context);

        try {
            myDbHelper.createDataBase();
        } catch (IOException ioe) {
            throw new Error("Unable to create database");
        }

        try {
            myDbHelper.openDataBase();
            this.database = myDbHelper.getDataBase();
        }catch(SQLException sqle){
            throw sqle;
        }
    }

    public int getCountOfKategorien(){
      int count = 0;

[B]        Cursor c = this.database.rawQuery("SELECT * FROM " + COL_PERSON, null);[/B]

        List<String> goobypls = new ArrayList<String>();

        c.moveToFirst();
        count = c.getCount();

      return count;
    }

Beim fettgedruckten kracht es.



Falls irgendwer mir nen Rat geben könnte, wäre ich echt dankbar, weil ich nicht mehr weiter weiß :(

Viele Grüße,

Jan
 
Omg...Tabellennamen müssen also immer groß geschrieben werden, gut zu wissen...kann zu, das Ding :scared::laugh::smile::rolleyes2::unsure::huh::sad:
 
wär mir neu, dass die tabellennamen groß geschrieben werden müssen.
 
Das hat bei mir aber das Problem gelöst, finde ich auch komisch, aber immerhin klappt es jetzt^^
 

Ähnliche Themen

5
Antworten
5
Aufrufe
1.237
jogimuc
J
Justice
Antworten
4
Aufrufe
1.695
Justice
Justice
A
Antworten
10
Aufrufe
1.034
swa00
swa00
Zurück
Oben Unten