执行insert语句时,Android Sqlite没有这样的表错误

时间:2015-05-09 17:28:43

标签: android sqlite

当我尝试在“opmerkingen”-table中插入一行时,它会抛出一个sqlexception,说没有这样的表。我试过更改数据库版本,但这没有做任何事情。

抛出错误的方法是在代码末尾附近的“voegOpmerkingToe”。

我不知道如何解决这个问题,非常感谢任何帮助。

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.sql.SQLException;

/**
 * Created by Michaël Desomer on 9/05/2015.
 */
public class DBAdapter {
    static final String DATABASE_NAME = "SportingDatabase";
    static final String DATABASE_TABLE_LID = "Lid";
    static final String DATABASE_TABLE_SPORTSESSIE = "Sportsessie";
    static final String DATABASE_TABLE_OPMERKINGEN = "Opmerkingen";
    static final String DATABASE_TABLE_BADGES_PER_LID = "Badges_Per_Lid";
    static final String DATABASE_TABLE_BADGES = "Badges";
    static final int DATABASE_VERSION = 2;

    static final String KEY_LID_LOGINNAAM = "loginnaam";
    static final String KEY_LID_NAAM = "naam";
    static final String KEY_LID_VOORNAAM = "voornaam";
    static final String KEY_LID_LEEFTIJD = "leeftijd";
    static final String KEY_LID_PASWOORD = "paswoord";

    static final String KEY_SPORTSESSIE_LIDLOGIN = "lid_login";
    static final String KEY_SPORTSESSIE_SPORTNAAM = "sportnaam";
    static final String KEY_SPORTSESSIE_DUUR = "duur";

    static final String KEY_OPMERKINGEN_OPMID = "opm_id";
    static final String KEY_OPMERKINGEN_LIDLOGIN = "lid_login";
    static final String KEY_OPMERKINGEN_OPMERKING = "opmerking";

    static final String KEY_BADGESPERLID_BADGECODE = "badge_code";
    static final String KEY_BADGESPERLID_LIDLOGIN = "lid_login";

    static final String KEY_BADGES_BADGECODE = "badge_code";
    static final String KEY_BADGES_AANTAL = "aantal";
    static final String KEY_BADGES_DUUR = "duur";

    static final String DATABASE_CREATE_LID = "create table if not exists Lid (" +
            "loginnaam text primary key, " +
            "naam text, voornaam text, " +
            "leeftijd integer, " +
            "paswoord text not null)";

    static final String DATABASE_CREATE_SPORTSESSIE = "create table if not exists Sportsessie (" +
            "lid_login text references Lid(loginnaam) deferrable initially deferred, " +
            "sportnaam text, " +
            "duur bigint, " +
            "primary key(lid_login, sportnaam, duur)";

    static final String DATABASE_CREATE_OPMERKINGEN = "create table if not exists Opmerkingen (" +
            "opm_id integer primary key autoincrement, " +
            "lid_login text not null references Lid(loginnaam) deferrable initially deferred, " +
            "opmerking text not null)";

    static final String DATABASE_CREATE_BADGESPERLID = "create table if not exists Badges_Per_Lid (" +
            "badge_code text references Badges(badge_code) deferrable initially deferred, " +
            "lid_login text references Lid(loginnaam) deferrable initially deferred)";

    static final String DATABASE_CREATE_BADGES = "create table if not exists Badges (" +
            "badge_code text primary key, " +
            "aantal integer, " +
            "duur bigint)";

    final Context context;

    DatabaseHelper DBHelper;
    SQLiteDatabase db;

    public DBAdapter(Context context){
        this.context = context;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper{

        DatabaseHelper(Context context){
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            try{
                db.execSQL(DATABASE_CREATE_LID);
                db.execSQL(DATABASE_CREATE_SPORTSESSIE);
                db.execSQL(DATABASE_CREATE_OPMERKINGEN);
                db.execSQL(DATABASE_CREATE_BADGESPERLID);
                db.execSQL(DATABASE_CREATE_BADGES);
            } catch(Exception e){
                e.printStackTrace();
            }
        }

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

        }
    }

    public DBAdapter open() throws SQLException{
        db = DBHelper.getWritableDatabase();
        return this;
    }

    public void close(){
        DBHelper.close();
    }

    public void registreer(String login, String naam, String voornaam, int leeftijd, String paswoord) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_LID_LOGINNAAM, login);
        initialValues.put(KEY_LID_NAAM, naam);
        initialValues.put(KEY_LID_VOORNAAM, voornaam);
        initialValues.put(KEY_LID_LEEFTIJD, leeftijd);
        initialValues.put(KEY_LID_PASWOORD, paswoord);

        db.insert(DATABASE_TABLE_LID, null, initialValues);
    }

    public Cursor getPaswoord(String login, String paswoord){
        Cursor cursor = db.query(true, DATABASE_TABLE_LID, new String[]{KEY_LID_PASWOORD}, KEY_LID_LOGINNAAM + "='" + login+"'", null, null, null, null, null);

        if(cursor != null){
            cursor.moveToFirst();
        }
        return cursor;
    }

    public Cursor getLid(String login){
        Cursor c = db.query(true, DATABASE_TABLE_LID, new String[]{KEY_LID_LOGINNAAM, KEY_LID_NAAM, KEY_LID_VOORNAAM, KEY_LID_LEEFTIJD, KEY_LID_PASWOORD}, KEY_LID_LOGINNAAM + "='" + login+"'", null, null, null, null, null);
        if(c != null){
            c.moveToFirst();
        }
        return c;
    }

    public void voegOpmerkingToe(String login, String opmerking){
        ContentValues values = new ContentValues();

        values.put(KEY_OPMERKINGEN_OPMERKING, opmerking);
        values.put(KEY_OPMERKINGEN_LIDLOGIN, login);
        db.insert(DATABASE_TABLE_OPMERKINGEN, null, values);
    }

    public Cursor getAlleOpmerkingen(String login){
        Cursor c = db.query(true, DATABASE_TABLE_OPMERKINGEN, new String[]{KEY_OPMERKINGEN_OPMERKING}, KEY_OPMERKINGEN_LIDLOGIN + "='" + login + "'", null, null, null, null, null, null);
        if(c != null){
            c.moveToFirst();
        }
        return c;
    }


}

错误消息:

05-09 19:19:47.700    5635-5635/desomer_michael_2app.desomer_michael_eindopdracht_app E/SQLiteLog﹕ (1) no such table: Opmerkingen
05-09 19:19:47.730    5635-5635/desomer_michael_2app.desomer_michael_eindopdracht_app E/SQLiteDatabase﹕ Error inserting opmerking=lol lid_login=Micha12354
    android.database.sqlite.SQLiteException: no such table: Opmerkingen (code 1): , while compiling: INSERT INTO Opmerkingen(opmerking,lid_login) VALUES (?,?)
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1118)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:691)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1589)
            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1461)
            at desomer_michael_2app.desomer_michael_eindopdracht_app.Util.DBAdapter.voegOpmerkingToe(DBAdapter.java:147)
            at desomer_michael_2app.desomer_michael_eindopdracht_app.SportSessieActivity.opmerkingToevoegen(SportSessieActivity.java:82)
            at desomer_michael_2app.desomer_michael_eindopdracht_app.OpmerkingFragment.opmerkingToevoegenEnAfsluiten(OpmerkingFragment.java:53)
            at desomer_michael_2app.desomer_michael_eindopdracht_app.OpmerkingFragment$1.onClick(OpmerkingFragment.java:41)
            at android.view.View.performClick(View.java:4475)
            at android.view.View$PerformClick.run(View.java:18786)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
            at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案