SELECT查询:在数据库中找到0行

时间:2019-05-18 08:55:10

标签: java android sqlite

我的数据库有400多行,但是从Android查询时返回0行。我的查询是简单查询(SELECT * FROM表)。

我尝试了2件事: (首先)-在onCreate()中放置创建表的代码。      +在这种情况下,它返回空表。 (第二)-在onCreate()中不要放置代码来创建表。      +在这种情况下,它将返回一个错误,表明该表不存在。

我向您展示的代码是返回空表的代码。 因此,给出另一种情况,只需对此评论:“ db.execSQL(CREAR_TABLA_EJERCICIOS);”

public class EjerciciosDBHelper extends SQLiteOpenHelper {
    public final static String DB_NAME = "fworkout.db";
    public final static String DB_TABLE = "EJERCICIO";

    //Columns
    public static final String CAMPO_ID = "id";
    public static final String CAMPO_IMG = "img";
    public static final String CAMPO_TITULO = "titulo";
    public static final String CAMPO_DESCRIPCION = "descripcion";
    public static final String CAMPO_DURACION = "duracion";
    public static final String CAMPO_DIFICULTAD = "dificultad";
    public static final String CAMPO_INTENSIDAD = "intensidad";
    public static final String CAMPO_ELEMENTOS = "elementos";
    public static final String CAMPO_INCIDIR = "incidir";
    public static final String CAMPO_EVITAR = "evitar";
    public static final String CAMPO_PROGRESION = "progresion";
    public static final String CAMPO_DIM_CAMPO = "dimension_campo";

    public static final String CREAR_TABLA_EJERCICIOS = "CREATE TABLE IF NOT EXISTS " +
            "" + DB_TABLE + " (" + CAMPO_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            CAMPO_IMG + " TEXT," + CAMPO_TITULO + " TEXT," + CAMPO_DESCRIPCION + " TEXT," +
            CAMPO_DURACION + " INTEGER," + CAMPO_DIFICULTAD + " TEXT," + CAMPO_INTENSIDAD +
            " TEXT," + CAMPO_ELEMENTOS + " TEXT," + CAMPO_INCIDIR + " TEXT," + CAMPO_EVITAR +
            " TEXT," + CAMPO_PROGRESION + " TEXT," + CAMPO_DIM_CAMPO + " TEXT)";

    public static final String tituloDuracionQuery = "SELECT id AS _id, titulo AS titulo, " +
            "duracion AS duracion FROM " + DB_TABLE + ";" ;


    public EjerciciosDBHelper(Context context) {
        super(context, DB_NAME, null, 1);
    }

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

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

}

public class EjerciciosActivity extends AppCompatActivity {

    ListView lvEjercicios;
    private EjerciciosDBHelper ejercDB;
    TextView tvTitulo, tvDuracion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ejercicios);

        lvEjercicios = (ListView) findViewById(R.id.lvElemetosEjercicios);

        this.ejercDB = new EjerciciosDBHelper(getApplicationContext());
        SQLiteDatabase sqlLiteDB = ejercDB.getWritableDatabase();
        Cursor cursor = sqlLiteDB.rawQuery(EjerciciosDBHelper.tituloDuracionQuery, null);
        Log.d("DEPURACIÓN", "Nº filas: " + cursor.getCount());
        Log.e("DEPURACIÓN", "Nº filas: ");


        String[] desdeEstasColumnas = {"titulo", "duracion"};
        int[] aEstasViews = {R.id.tvTituloElementoEjercicio, R.id.tvDuracionElementoEjercicio};
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.elemento_ejercicio, cursor, desdeEstasColumnas, aEstasViews, 0);
        lvEjercicios.setAdapter(adapter);

        tvTitulo=(TextView) findViewById(R.id.tvTituloElementoEjercicio);
        tvDuracion=(TextView) findViewById(R.id.tvDuracionElementoEjercicio);
        if (cursor.moveToFirst()){
            tvTitulo.setText(cursor.getString(cursor.getColumnIndex("titulo")));
            tvDuracion.setText(cursor.getString(cursor.getColumnIndex("duracion")));
        }
    }
}

在第一种情况下,表返回0行。

在第二种情况下,返回错误:

E/SQLiteLog: (1) no such table: EJERCICIO
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.android.fworkout.fworkout, PID: 28520
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.fworkout.fworkout/com.android.fworkout.fworkout.EjerciciosActivity}: android.database.sqlite.SQLiteException: no such table: EJERCICIO (code 1): , while compiling: SELECT id AS _id, titulo AS titulo, duracion AS duracion FROM EJERCICIO;
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1613)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6635)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
     Caused by: android.database.sqlite.SQLiteException: no such table: EJERCICIO (code 1): , while compiling: SELECT id AS _id, titulo AS titulo, duracion AS duracion FROM EJERCICIO;
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:501)
        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:46)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1392)
        at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1331)
        at com.android.fworkout.fworkout.EjerciciosActivity.onCreate(EjerciciosActivity.java:27)
        at android.app.Activity.performCreate(Activity.java:7084)
        at android.app.Activity.performCreate(Activity.java:7075)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2767)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1613) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:176) 
        at android.app.ActivityThread.main(ActivityThread.java:6635) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

0 个答案:

没有答案