Sqlite only accesses the bank on a device

Asked

Viewed 67 times

0

I created an application on Android that accesses the Sqlite database, the first application I created works normally!

The application that works is version 7.1 of Android, but when I try to run the same code in emulator 4.4, 5.1 or any other to test, the database cannot be accessed! Returns Nullpointer Exception, but as I said the first application I created works very well.

I think the table is only being created once on one device, and when I try to access from another I have problems, right?

Follows the code:

package com.megavirtua.boalista;

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;
import java.util.List;

public class DBHandler extends SQLiteOpenHelper {

    private static final int VERSAO_BANCO = 1;

    private static final String NOME_BANCO = "banco_configs";

    private static final String TABELA_CONFIGS = "configs";

    private static final String KEY_COD = "codigo";
    private static final String KEY_CONFIG = "config";
    private static final String KEY_DESCR_CONFIG = "descr_config";

    public DBHandler(Context context) {
        super(context, NOME_BANCO, null, VERSAO_BANCO);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABELA_CONFIGS + "("
                + KEY_COD + " INTEGER PRIMARY KEY," + KEY_CONFIG + " TEXT,"
                + KEY_DESCR_CONFIG + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

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

        db.execSQL("DROP TABLE IF EXISTS " + TABELA_CONFIGS);
        onCreate(db);
    }


    public void addConfig(DBConfig config) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_CONFIG, config.getConfig());
        values.put(KEY_DESCR_CONFIG, config.getDescrConfig());

        // Insere linha
        db.insert(TABELA_CONFIGS, null, values);
        db.close();
    }


    // Obtem tudo
    public List<DBConfig> getAllConfigs() {
        List<DBConfig> configList = new ArrayList<DBConfig>();

        String selectQuery = "SELECT  * FROM " + TABELA_CONFIGS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                DBConfig config = new DBConfig();
                config.setCodigo(Integer.parseInt(cursor.getString(0)));
                config.setConfig(cursor.getString(1));
                config.setDescrConfig(cursor.getString(2));

                configList.add(config);
            } while (cursor.moveToNext());
        }


        return configList;
    }


    public List<DBConfig> getConfig(int codigo) {
        List<DBConfig> configList = new ArrayList<DBConfig>();

        String selectQuery = "SELECT  * FROM " + TABELA_CONFIGS + " WHERE codigo=" + codigo;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);


        if (cursor.moveToFirst()) {
            do {
                DBConfig config = new DBConfig();
                config.setCodigo(Integer.parseInt(cursor.getString(0)));
                config.setConfig(cursor.getString(1));
                config.setDescrConfig(cursor.getString(2));

                configList.add(config);
            } while (cursor.moveToNext());
        }


        return configList;
    }


    public void updateConfigEDescricao(int codigo, String config, String descrConfig) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("UPDATE " + TABELA_CONFIGS + " SET "
                + KEY_CONFIG + "= '" + config + "'," +
                KEY_DESCR_CONFIG + "= '" + descrConfig + "'" +
                " WHERE codigo=" + codigo);
    }

    public void updateConfig(int codigo, String config) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("UPDATE " + TABELA_CONFIGS + " SET "
                + KEY_CONFIG + "= '" + config + "' " +
                " WHERE codigo=" + codigo);
    }

    public boolean deleteConfig(int codigo) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete(TABELA_CONFIGS, KEY_COD + "='" + codigo + "' ;", null) > 0;

    }

}
  • Do you have the APP installed in the emulator? Try to remove it before compiling again

  • My friend, in the first emulator it worked well, but in the others it doesn’t work! It doesn’t have logic... Kkk!

  • Hello, what I think is happening is exactly what you deduced, probably as he has already installed version 1 of the bank in another emulator he is not letting you create this bank again once it has been created. Try changing the database version and test run to see if it works.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.