android.database.sqlite.SQLiteException:没有这样的表:ANECDOTAS(代码1):,同时编译:SELECT * FROM ANECDOTAS

时间:2018-03-16 13:41:45

标签: java android sqlite

嗨我正在尝试创建一个表,但它不起作用,这是我的代码:

public class AnecdotasSQLiteHelper extends SQLiteOpenHelper {

    static final String DATABASE_NAME = "AnecdotasDB";
    static final int DATABASE_VERSION = 1;
    ArrayList<Anecdota> listaAnecdotas;
    static final String CREATE_TABLE_ANECDOTAS =
          "CREATE TABLE "+ AnecdotasDBContract.Entry.TABLE_NAME +
                  "( "+ AnecdotasDBContract.Entry.COLUMN_TITULO +
                  " TEXT NOT NULL ,"+
                  AnecdotasDBContract.Entry.COLUMN_ANECDOTA +
                  " TEXT NOT NULL)";

    public AnecdotasSQLiteHelper(Context contexto) {
        super(contexto, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_ANECDOTAS);
        cargaInicial(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " +
            AnecdotasDBContract.Entry.TABLE_NAME);
        onCreate(db);
    }
}

AnecdotasDBContract

public class AnecdotasDBContract {
    public static abstract class Entry  {

        public static final String TABLE_NAME = "ANECDOTAS";
        public static final String COLUMN_TITULO = "TITULO";
        public static final String COLUMN_ANECDOTA = "ANECDOTA";
    }
}

在我创建的其他项目中都运行完美,但我不明白为什么我有这个错误

03-16 14:27:38.834 15171-15171/com.example.mario.especial50 E/SQLiteLog: (1) no such table: ANECDOTAS
03-16 14:27:38.835 15171-15171/com.example.mario.especial50 D/AndroidRuntime: Shutting down VM
03-16 14:27:38.837 15171-15171/com.example.mario.especial50 E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.mario.especial50, PID: 15171
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mario.especial50/com.example.mario.especial50.Anecdotas}: android.database.sqlite.SQLiteException: no such table: ANECDOTAS (code 1): , while compiling: SELECT * FROM ANECDOTAS
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2906)
      at android.app.ActivityThread.-wrap11(Unknown Source:0)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1605)
      at android.os.Handler.dispatchMessage(Handler.java:105)
      at android.os.Looper.loop(Looper.java:172)
      at android.app.ActivityThread.main(ActivityThread.java:6637)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
   Caused by: android.database.sqlite.SQLiteException: no such table: ANECDOTAS (code 1): , while compiling: SELECT * FROM ANECDOTAS
      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.<init>(SQLiteProgram.java:58)
      at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
      at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
      at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)
      at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1257)
      at baseDatos.AnecdotasDataSource.leerAnecdotas(AnecdotasDataSource.java:49)
      at com.example.mario.especial50.Anecdotas.cargarLista(Anecdotas.java:51)
      at com.example.mario.especial50.Anecdotas.onCreate(Anecdotas.java:38)
      at android.app.Activity.performCreate(Activity.java:6975)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2784)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2906) 
      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1605) 
      at android.os.Handler.dispatchMessage(Handler.java:105) 
      at android.os.Looper.loop(Looper.java:172) 
      at android.app.ActivityThread.main(ActivityThread.java:6637) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

1 个答案:

答案 0 :(得分:0)

试试这个

package com.historyplay.sanjosehills.util;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class AnecdotasSQLiteHelper extends SQLiteOpenHelper {

    static final String DATABASE_NAME = "AnecdotasDB";
    static final int DATABASE_VERSION = 1;
    ArrayList<Anecdota> listaAnecdotas;
    static final String CREATE_TABLE_ANECDOTAS =
            "CREATE TABLE "+ AnecdotasDBContract.Entry.TABLE_NAME +
                    "( "+ AnecdotasDBContract.Entry.COLUMN_TITULO +
                    " TEXT NOT NULL ,"+
                    AnecdotasDBContract.Entry.COLUMN_ANECDOTA +
                    " TEXT NOT NULL)";
    SQLiteDatabase db;

    public AnecdotasSQLiteHelper(Context contexto) {
        super(contexto, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_ANECDOTAS);
        this.db = db;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " +
                AnecdotasDBContract.Entry.TABLE_NAME);
        onCreate(db);
    }

    public void cargaInicial(){
        // not receive db as parameter, then use this.db
        .... your code
    }
}

并调用执行getWritableDatabase方法

之外的类
AnecdotasSQLiteHelper store = new AnecdotasSQLiteHelper(this.getContext(), "ANECDOTASDB", null, 1);
        store.getWritableDatabase();
        store.cargaInicial();