I created an application on Android that accesses the SqLite bank, 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 4.4, 5.1 or any other emulator to test, the database can not be accessed! Returns NullPointer exception, but as I said the first application I created works fine.
I think the table is only being created once in a single device, and when I try to access another I have problems, right?
Follow 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;
}
}