3
Good guys, how do I make a select in a table using Sqlite on Android? So far I have the table created and the Insert.
For example: search by User login and password, to be able to compare.
Follows the code:
Codless:
public class BancoDados extends SQLiteOpenHelper {
public static final String NOME_BANCO = "banco.db";
public static final String NOME_TABELA = "usuario";
public static final int VERSAO_BANCO = 1;
public BancoDados(Context context) {
super(context, NOME_BANCO, null, VERSAO_BANCO);
}
@Override
public void onCreate(SQLiteDatabase bd) {
bd.execSQL("create table if not exists " +
"usuario(_id integer primary key autoincrement, " +
"nome text not null," +
"login text not null," +
" senha text not null," +
" email text not null);");
}
@Override
public void onUpgrade(SQLiteDatabase bd, int oldVersion, int newVersion) {
}
}
Control:
public class ControleBanco {
private SQLiteDatabase bd;
private BancoDados banco;
public ControleBanco(Context context) {
banco = new BancoDados(context);
}
public String inserirDados(String nome, String login, String senha, String email)
{
long resultado;
ContentValues valores = new ContentValues();
bd = banco.getWritableDatabase();
valores.put("nome", nome);
valores.put("login", login);
valores.put("senha", senha);
valores.put("email", email);
resultado = bd.insert(BancoDados.NOME_TABELA, "", valores);
bd.close();
if (resultado == -1) {
return "Falha ao cadastrar";
} else {
return "Cadastrado com sucesso";
}
}
In this part: mail FROM user WHERE email = ?" the query has simple quotes? And how could I manipulate these data returning in my Activity?
– Christian Gomes da Silva
This interrogation is giving problem in mine here
– Christian Gomes da Silva
@Christiangomesdasilva edited my reply with an example of how to call the method in your Activity to work with the recovered data.
– CloudAC
@Christiangomesdasilva is not working? I tested it here and it worked. Which version of android studio are you using? I tested this code in android studio version 2.3.3; It is likely (not sure) that in version 3.0.0 of android studio need to put simple quotes in interrogation.
– CloudAC
@Christiangomesdasilva if it doesn’t work by putting simple quotes in the question, put the error here in the comments to see what can be.
– CloudAC
Gives this error <expr> expected, got '?'
– Christian Gomes da Silva
@Christiangomesdasilva is right. I searched the American forums and found that from android studio 3.0 the syntax engine for query sql became more rigorous and some people are having the same problem as you. I edited my reply with one of the solutions that some users pointed out and that can work.
– CloudAC