-1
Ola I have had some problems with my Sqlite database in Android Studio when I try to add a new user it does not insert and presenting the error message when inserting users. According to what I spoke to some colleagues none of them saw why it is not sending.
If anyone could help me I’d be very grateful
Databaseopenhelpe.java
public class DatabaseOpenHelpe extends SQLiteOpenHelper {
public DatabaseOpenHelpe( Context context ) {
super(context,"default", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table Users(" +
"iduser integer primary key AUTOINCREMENT," +
"username text," +
"password text,"+
"email text)");
db.execSQL("create table Restaurante(" +
"idrestaurante integer primary key AUTOINCREMENT," +
"restaurante text, " +
"lugares int)");
db.execSQL("create table Reservas(" +
"idreserva integer primary key AUTOINCREMENT," +
"reserva text," +
"iduser integer," +
"idrestaurante integer, " +
"foreign key(iduser) references Users(iduser)," +
"foreign key(idrestaurante) references Restaurante(idrestaurante))");
db.execSQL("insert into restaurante(idrestaurante,restaurante,lugares) values('0','Cabana do Pastor','70')",null);
db.execSQL("insert into restaurante(idrestaurante,restaurante,lugares) values('1','Sushi Pra ti','40')",null);
db.execSQL("insert into restaurante(idrestaurante,restaurante,lugares) values('2','Dom Fininho','60')",null);
db.execSQL("insert into restaurante(idrestaurante,restaurante,lugares) values('3 ','Marques de Marialva','60')",null);
db.execSQL("insert into restaurante(idrestaurante,restaurante,lugares) values('4','Marisqueira C3','50')",null);
db.execSQL("insert into restaurante(idrestaurante,restaurante,lugares) values('5','Restaurante Júlia Duarte','70')",null);
db.execSQL("insert into restaurante(idrestaurante,restaurante,lugares) values('6','Bar Necal','60')",null);
db.execSQL("insert into restaurante(idrestaurante,restaurante,lugares) values('7','Cova do Finfas','50')",null);
db.execSQL("insert into restaurante(idrestaurante,restaurante,lugares) values('8','Pitada de Sal','50')",null);
db.execSQL("insert into restaurante(idrestaurante,restaurante,lugares) values('9','La Bella Capricciosa','50')",null);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS Users");
}
public long CriarUtilizadores(String username, String password, String email){
SQLiteDatabase db = getWritableDatabase ();
db.execSQL("insert into users(username,password,email) values(?,?,?);",new String[]{username,password,email});
return 0;
}
public String validarlogin(String username, String password ){
SQLiteDatabase db = getWritableDatabase ();
Cursor c = db.rawQuery ( "SELECT * FROM User WHERE username=? AND password=?",new String[]{username, password});
if (c.getCount ()>0){
return "Ok";
}
return "ERRO";
}
}
Register java.
public class Register extends AppCompatActivity {
EditText username, pass1, pass2, mail;
Button bt_confirmar;
DatabaseOpenHelpe db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_register );
db = new DatabaseOpenHelpe ( this );
username = (EditText) findViewById ( R.id.rusername );
mail = (EditText) findViewById ( R.id.email );
pass1 = (EditText) findViewById ( R.id.rpassword1 );
pass2= (EditText) findViewById ( R.id.rpassword2 );
bt_confirmar = (Button) findViewById ( R.id.bt_confirmar );
bt_confirmar.setOnClickListener ( new View.OnClickListener () {
@Override
public void onClick(View v) {
String user = username.getText ().toString ();
String email = mail.getText().toString();
String p1 = pass1.getText().toString ();
String p2 = pass1.getText().toString ();
if(user.equals ("")){
Toast.makeText(Register.this,"Utilizador não inserido",Toast.LENGTH_SHORT).show ();
return;
}
if(p1.equals ( "" ) || p2.equals ( "" )){
Toast.makeText(Register.this,"Password não inserida",Toast.LENGTH_SHORT).show ();
return;
}
if (!p1.equals( p2 )){
Toast.makeText(Register.this,"As palavras pass não correspondem",Toast.LENGTH_SHORT).show ();
return;
}
if (email.equals ( "" )){
Toast.makeText(Register.this,"Email não inserido",Toast.LENGTH_SHORT).show ();
return;
}
long res = db.CriarUtilizadores ( user, p1, email );
if (res>0){
Toast.makeText(Register.this,"Registado com sucesso",Toast.LENGTH_SHORT).show ();
}else {
Toast.makeText(Register.this,"Falha ao registrar, tente novamente",Toast.LENGTH_SHORT).show ();
}
}
});
}
}
Also post the error that appears in the log. It will help too.
– Luídne