Sqlite database creation on smartphone

Asked

Viewed 61 times

0

I’m having doubts regarding the creation of the bank in Sqlite on the smartphone via USB. When I run my program on emulator, the bank works perfectly and smoothly. But I realize that when I do the same, emulating the app on my smartphone, the program does not create the data bank (which technically should occur). In that regard, I ask:

  • Why the smartphone is not creating the bank?
  • How do I create the bank on the smartphone via USB? What other ways to create the bank on the device?

As requested, the code of one of the tables:

public class BDUserCustomSQLiteOpenHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "MPM_BD.db";
    private static final int DATABASE_VERSION = 5;

    public static final String TABLE_USUARIO = "MPM_USUARIO";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_NOME = "nome";
    public static final String COLUMN_SENHA = "senha";
    public static final String COLUMN_PERNR = "pernr";
    public static final String COLUMN_CODUSU = "codusu";
    public static final String COLUMN_LOGIN = "login";

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

    @Override
    public void onCreate(SQLiteDatabase database) {
        String DATABASE_CREATE = "create table " + TABLE_USUARIO + " ("
                + COLUMN_ID + " integer not null primary key autoincrement, "
                + COLUMN_NOME + " text, "
                + COLUMN_SENHA + " text, "
                + COLUMN_PERNR + " integer, "
                + COLUMN_CODUSU + " integer, "
                + COLUMN_LOGIN + " text)";
        database.execSQL(DATABASE_CREATE);
    }

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

Bancodon:

public class BDUserBancoDao {
private SQLiteDatabase database;
private String[] columns = {BDUserCustomSQLiteOpenHelper.COLUMN_ID, BDUserCustomSQLiteOpenHelper.COLUMN_NOME,
        BDUserCustomSQLiteOpenHelper.COLUMN_SENHA, BDUserCustomSQLiteOpenHelper.COLUMN_PERNR,
        BDUserCustomSQLiteOpenHelper.COLUMN_CODUSU, BDUserCustomSQLiteOpenHelper.COLUMN_LOGIN};
private BDUserCustomSQLiteOpenHelper sqliteOpenHelper;

public BDUserBancoDao(Context context) {
    sqliteOpenHelper = new BDUserCustomSQLiteOpenHelper(context);
}

public void open() throws SQLException {
    database = sqliteOpenHelper.getWritableDatabase();
}

public void close() {
    sqliteOpenHelper.close();
}

public boolean verificaDados(String codigoUsuario){
    long cod = Integer.parseInt(codigoUsuario);
    Cursor cursor = database.query(BDUserCustomSQLiteOpenHelper.TABLE_USUARIO, columns, BDUserCustomSQLiteOpenHelper.COLUMN_ID + " = " + cod, null, null, null, null, null);

    if((cursor!=null&& cursor.getCount()>0)) return true;
    else return false;
}

//Le os dados do usuario e retorna seus valores (id, nome, senha,pernr,codusu,login)
public BDUserGetterSetter lerUsuario(String codigoUsuario) {
    long cod = Integer.parseInt(codigoUsuario);
    Cursor cursor = database.query(BDUserCustomSQLiteOpenHelper.TABLE_USUARIO, columns, BDUserCustomSQLiteOpenHelper.COLUMN_ID + " = " + cod, null, null, null, null);

    BDUserGetterSetter newDadosUser = new BDUserGetterSetter();
    cursor.moveToFirst();

    newDadosUser.setId(cursor.getInt(0));
    newDadosUser.setNome(cursor.getString(1));
    newDadosUser.setSenha(cursor.getString(2));
    newDadosUser.setPernr(cursor.getString(3));
    newDadosUser.setCodusu(cursor.getInt(4));
    newDadosUser.setLogin(cursor.getString(5));
    cursor.close();

    return newDadosUser;
}

public void deletarDados (){
    database.delete(sqliteOpenHelper.TABLE_USUARIO,null,null);
}

public String inserirDados(BDUserGetterSetter dadosUser) {
    // inserindo valores
    ContentValues valores = new ContentValues();
    long resultado;

    valores.put(sqliteOpenHelper.COLUMN_CODUSU, dadosUser.getCodusu());
    valores.put(sqliteOpenHelper.COLUMN_NOME, dadosUser.getNome());
    valores.put(sqliteOpenHelper.COLUMN_LOGIN, dadosUser.getLogin());
    valores.put(sqliteOpenHelper.COLUMN_SENHA, dadosUser.getSenha());
    valores.put(sqliteOpenHelper.COLUMN_PERNR, dadosUser.getPernr());

    resultado = database.insert(BDUserCustomSQLiteOpenHelper.TABLE_USUARIO, null, valores);       

    if (resultado == -1){
        return "Erro ao baixar registro";
    }else{
        return "Registro baixado com sucesso";
    }
}

OBS: I don’t know if this helps anything, but in DMMS, in the smartphone part, the directory "date" is empty.

  • 4

    First, include your creation code in the question, without seeing the code there is no way to verify the problem.

  • In principle the bank should have been created in the smartphone as it was in the emulator but, as @Diegofelipe said, without knowing the code will be difficult to answer the question.

  • Regarding "the "date" directory is empty." this is normal in the case of smartphone see this reply.

No answers

Browser other questions tagged

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