### oefening 1 ###
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/etInlognaam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Inlognaam" />
<Button
android:id="@+id/inloggen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inloggen"
android:onClick="onClick"/>
<Button
android:id="@+id/tonen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tonen"
android:onClick="onClick"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/etWachtwoord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Wachtwoord" />
<Button
android:id="@+id/registeren"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Registreren"
android:onClick="onClick"/>
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Download hier het bestand.
### oefening 2 ###
public class MainActivity extends AppCompatActivity {
...
public void onClick(View view){
SQLiteDatabase gebruikersDB = this.openOrCreateDatabase("gebruikersDB", MODE_PRIVATE,null);
gebruikersDB.execSQL("CREATE TABLE IF NOT EXISTS gegevens (Inlognaam VARCHAR, Wachtwoord VARCHAR);");
EditText username = (EditText)findViewById(R.id.etInlognaam);
EditText password = (EditText)findViewById(R.id.etWachtwoord);
String gebruiker = username.getText().toString();
String ww = password.getText().toString();
switch(view.getId()) {
case R.id.registeren:
String sql = "INSERT or replace INTO gegevens (Inlognaam, Wachtwoord) VALUES ('" + gebruiker + "', '" + ww + "')";
gebruikersDB.execSQL(sql);
break;
}
}
...
}
Download hier het bestand.
### oefening 3 ###
switch(view.getId()) {
..
case R.id.inloggen:
String table = "gegevens";
String[] columnsToReturn = { "Wachtwoord" };
String selection = "Inlognaam =?";
String[] selectionArgs = { gebruiker };
try {
Cursor cursor = gebruikersDB.query(table, columnsToReturn, selection, selectionArgs,
null, null, null);
cursor.moveToLast();
String column1 = cursor.getString((0));
if (column1.equals(ww)) {
Log.d(gebruiker, " succesvol ingelogd");
} else {
Log.d(gebruiker, " niet succesvol ingelogd");
}
cursor.close();
} catch (android.database.CursorIndexOutOfBoundsException e) {
Log.d(gebruiker, " bestaat waarschijnlijk niet");
}
break;
..
}
Download hier het bestand.
### oefening 4 ###
switch(view.getId()) {
...
case R.id.tonen:
Cursor c = gebruikersDB.rawQuery("SELECT * FROM gegevens",null);
if (c.getCount()==0) {
Log.d("database ", " is leeg");
} else {
while (c.moveToNext()){
int Column1 = c.getColumnIndex("Inlognaam");
int Column2 = c.getColumnIndex("Wachtwoord");
String Name = c.getString(Column1);
String WW = c.getString(Column2);
Log.d(Name,WW);
}
}
break;
...
}
Download hier het bestand.
