Problems with reading the database [SQLite] [closed]

0

Day number 3. The app insists on "crashing" on startup and the frustration is strong at the moment. The problem, I believe, lies with the getFromDb () method, more specifically on the line where I call the db.getReadableDatabase () since the app runs NORMALLY without the excerpt between "/ * * /" highlighted in the code below.

Follow the problematic code.

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

    public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "productsManager";
    private static final String TABLE_PRODUCTS = "products";

    private static final String _PRODUCT = "product";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TABLE = "CREATE TABLE products (id INTEGER PRIMARY KEY AUTO_INCREMENT, product TEXT )";
        db.execSQL(CREATE_TABLE);
    }

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

        onCreate(db);
    }

    public void addToDb(String product){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues valor = new ContentValues();
        valor.put(_PRODUCT, product);

        db.insert(TABLE_PRODUCTS, null, valor);
        db.close();
    }

    public ArrayList<String> getFromDb(){
        ArrayList<String> stringList = new ArrayList<String>();
/*        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_PRODUCTS, null, null, null, null, null, null);

        if(cursor.getCount() > 0){
            cursor.moveToFirst();
            do{
                stringList.add(cursor.getString(1));
            }while(cursor.moveToNext());
        }

        cursor.close();*/
        return  stringList;
    }
}

Please anyone know what is wrong? If needed I can add more information later.

    
asked by anonymous 13.03.2017 / 03:52

2 answers

0

It is really missing information, but from what I could see in the code posted, you should have done something like:

   public ArrayList<String> getFromDb(){
    ArrayList<String> stringList = new ArrayList<String>();
    SQLiteDatabase db = this.getReadableDatabase();
    String sqlCmd = "SELECT * FROM "+TABLE_PRODUCTS+";";
    // se voce tiver alguma clausula WHERE, acrecenta a mesma na linha acima
    Cursor cursor = db.query(sqlCmd);

    if(cursor.getCount() > 0){
        cursor.moveToFirst();
        do{
          stringList.add(cursor.getString(cursor.getColumnIndex("codigo")));
          stringList.add(cursor.getString(cursor.getColumnIndex("descricao")));
          stringList.add(cursor.getString(cursor.getColumnIndex("categoria")));
          stringList.add(cursor.getString(cursor.getColumnIndex("preco")));
            // e quantos campos mais voce quiser, lembrando que:
            // ...(getColumnIndex('campo') se refere ao indice de uma
            // coluna da tabela referenciada pelo seu nome
        }while(cursor.moveToNext());
    }
    cursor.close();
    return  stringList;
}

If there is any problem, in case of manipulation of data in a db, whatever it is, good practice insists on creating constant variables with the names of the tables, it is easier for you to manipulate its code, since being a constant variable and the same being referenced in several points of the code, if you change the constant you will consequently change your references as well.

    
13.03.2017 / 07:03
0

Have you tried using rawQuery instead of the query?

SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM "+TABLE_PRODUCTS;
Cursor cursor = db.rawQuery(query, null);
    
13.03.2017 / 14:32