1
resposta

problema checar username e password no SQLiteOpenHelper

Criei uma classe DBHelper:

public class DBHelper extends SQLiteOpenHelper {

    public static final String DBNAME="loginMovefastCrud.db";
    public static final int VERSAO_BANCO = 1;
    DBHelper(Context context) {
        super(context, Environment.getExternalStorageDirectory().getAbsolutePath()
                + "loginMovefastCrud.db" + DBNAME, null , VERSAO_BANCO);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table users(usarname TEXT primary key, password TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists users");
    }

    public Boolean insertData(String username, String password) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put("username", username);
        values.put("password", password);

        long result = db.insert("users", null, values);
        if(result == -1) return false;
        else
            return true;

    }

    public Boolean checkusername(String username) {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from users where username=?",
                new String[] {username});
        if(cursor.getCount() > 0)
            return true;
        else
            return false;

    }

    public Boolean checkusernamepassword(String username, String password) {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from users where username=? and password=?",
                new String[] {username, password});
        if(cursor.getCount() > 0)
            return true;
        else
            return false;

    }

}

ta dando erro na linha 57 "SQLiteDatabase db = this.getWritableDatabase();"

e a classe LoginActivity

public class LoginActivity extends AppCompatActivity {

    EditText username, password;
    Button btn_login;
    DBHelper DB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        username = findViewById(R.id.username1);
        password = findViewById(R.id.password1);
        btn_login = findViewById(R.id.btn_login);
        DB = new DBHelper(this);

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String user = username.getText().toString();
                String pass = password.getText().toString();

                if (TextUtils.isEmpty(user) || TextUtils.isEmpty(pass))
                    Toast.makeText(LoginActivity.this, "Todos os campos são obrigatórios"
                            , Toast.LENGTH_SHORT).show();
                else {
                    Boolean checkuserpass = DB.checkusernamepassword(user, pass);
                    if (checkuserpass==true) {
                        Toast.makeText(LoginActivity.this, "Registro com sucesso",
                                Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
                        startActivity(intent);
                    } else {
                        Toast.makeText(LoginActivity.this, "Login falhou"
                                , Toast.LENGTH_SHORT).show();
                    }
                }
            }

        });
    }

}

ta dando erro na linha 40 "Boolean checkuserpass = DB.checkusernamepassword(user, pass);"

 2022-04-20 12:07:45.059 6557-6583/com.br.movefastcrud E/eglCodecCommon: GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping failed for device_type=5, ret=-1
2022-04-20 12:08:00.107 6557-6557/com.br.movefastcrud E/SQLiteLog: (14) cannot open file at line 36667 of [c255889bd9]
2022-04-20 12:08:00.107 6557-6557/com.br.movefastcrud E/SQLiteLog: (14) os_unix.c:36667: (2) open(/storage/emulated/0loginMovefastCrud.dbloginMovefastCrud.db) - 
2022-04-20 12:08:00.173 6557-6557/com.br.movefastcrud E/SQLiteDatabase: Failed to open database '/storage/emulated/0loginMovefastCrud.dbloginMovefastCrud.db'.
    android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14 SQLITE_CANTOPEN): Could not open database
        at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:211)

quando o rodo o app, aparecem esses erros, o que fazer?

1 resposta

Fala Gerardus , de boa ?

Mano, olhando por cima acredito que a forma que tu fez para criar o banco que seja o problema

super(context, Environment.getExternalStorageDirectory().getAbsolutePath()
                + "loginMovefastCrud.db" + DBNAME, null , VERSAO_BANCO);

Testa colocando apenas o DBNAME e fala pra gente se deu certo

super(context, DBNAME, null , VERSAO_BANCO);

Detalhe importante, tu vai precisar remover o app e instala-lo novamente para pegar a mudança