Reputation: 37
After every button I clicked on the page (the button update, inschrijven, showallpeople or delete) so every button that has something to do with the SQL database.. is failing. I don't know why but I think it has something to do with the column names. I don't understand it!
So here is my code of all the pages.
Here is my SQLiteHelper class :
package com.example.cedri.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by cedri on 17/05/2016.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME= "KIND_DATABASE.db";
public static final String TABLE_NAME= "KIND_TABLE";
public static final int DATABASE_VERSION=1;
public static final String COL_1="ID";
public static final String COL_2="VOORNAAM";
public static final String COL_3="NAAM";
public static final String COL_4="LEEFTIJD";
public static final String COL_5="EMAIL";
private static final String CREATE_TABLE_KIND= "create table "+TABLE_NAME + " ( " +
COL_1 +" INTEGER PRIMARY KEY AUTOINCREMENT," +
COL_2+" TEXT," +
COL_3+ " TEXT," +
COL_4+ " INTEGER," +
COL_5+" TEXT);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_KIND);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String voornaam,String naam,Integer leeftijd,String email){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(COL_2,voornaam);
contentValues.put(COL_3,naam);
contentValues.put(COL_4,leeftijd);
contentValues.put(COL_5, email);
long result= db.insert(TABLE_NAME,null,contentValues);
if(result==-1){
return false;
}
else{
return true;
}
}
public Cursor getAllData(){
SQLiteDatabase db=this.getWritableDatabase();
Cursor res=db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
public Integer deleteData(String email){
SQLiteDatabase db=this.getWritableDatabase();
return db.delete(TABLE_NAME,"EMAIL=?",new String[]{email});
}
public boolean updateData(String voornaam, String naam, Integer leeftijd, String email){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,voornaam);
contentValues.put(COL_3,naam);
contentValues.put(COL_4,leeftijd);
contentValues.put(COL_5, email);
db.update(TABLE_NAME,contentValues,"EMAIL = ?",new String[]{email});
return true;
}
}
Here is my MainActivity class:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,menu3Fragment.OnMainFragmentInteractionListener {
DatabaseHelper myDb;
TextView txtVoornaam,txtNaam,txtLeeftijd,txtEmail;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
myDb = new DatabaseHelper(this);
}
public void btnUpdate_Click(View view){
boolean isUpdate = myDb.updateData(txtVoornaam.getText().toString(),
txtNaam.getText().toString(),
Integer.valueOf(txtLeeftijd.getText().toString()),
txtEmail.getText().toString());
if(isUpdate==true){
Toast.makeText(MainActivity.this,"De data is geupdate",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this,"De data is niet geupdate",Toast.LENGTH_LONG).show();
}
}
public void btnDelete_Click(View view){
Integer isDelete = myDb.deleteData(
txtEmail.getText().toString());
if(isDelete>1){
Toast.makeText(MainActivity.this,"De data is verwijderd",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this,"De data is niet verwijderd",Toast.LENGTH_LONG).show();
}
}
public void btnGaNaarIngeschrevenLeden_Click(View view) {
Cursor res=myDb.getAllData();
if(res.getCount() == 0){
//show message
showMessage("Error", "Er werd geen data gevonden");
return;
}
StringBuffer buffer = new StringBuffer();
while(res.moveToNext()){
buffer.append("Id :"+ res.getString(0) + "\n");
buffer.append("Voornaam :"+ res.getString(1) + "\n");
buffer.append("Naam :"+ res.getString(2) + "\n");
buffer.append("Leeftijd :"+ res.getInt(3) + "\n");
buffer.append("Email :"+ res.getString(4) + "\n\n");
}
//show all data
showMessage("Ingeschreven kinderen 2016", buffer.toString());
}
public void btnSchrijfIn_Click(View view) {
txtVoornaam= (TextView)findViewById(R.id.Voornaam);
txtNaam= (TextView)findViewById(R.id.Naam);
txtLeeftijd= (TextView)findViewById(R.id.Leeftijd);
txtEmail= (TextView)findViewById(R.id.Email);
boolean isInsertData =
myDb.insertData(
txtVoornaam.getText().toString(),
txtNaam.getText().toString(),
Integer.valueOf(txtLeeftijd.getText().toString()),
txtEmail.getText().toString() );
if(isInsertData==true){
//sendEmail();
Toast.makeText(MainActivity.this,"Uw kind werd succesvol ingeschreven!", Toast.LENGTH_LONG).show();
}
else
{
TextView foutmelding2=(TextView)findViewById(R.id.Foutmelding);
foutmelding2.setVisibility(TextView.VISIBLE);
}
Fragment fragment = new menu1Fragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction =fragmentManager.beginTransaction();
transaction.replace(R.id.main_content,fragment);
transaction.addToBackStack(null);
transaction.commit();
}
Here is my Layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.cedri.chiroeine.InschrijvenPage"
android:background="@color/backgroundzwart">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="@string/Inschrijving_titel"
android:drawableLeft="@drawable/test"
android:layout_marginLeft="45dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:textSize="20dp"
android:textColor="@color/achtergrond_inputvak"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Inschrijving_inputvak1"
android:textColor="@color/bordeaurood"
android:textSize="20dp"/>
<EditText
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_marginLeft="58dp"
android:layout_marginRight="15dp"
android:background="@color/achtergrond_inputvak"
android:id="@+id/Voornaam"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Inschrijving_inputvak2"
android:textColor="@color/bordeaurood"
android:textSize="20dp"/>
<EditText
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_marginLeft="95dp"
android:layout_marginRight="15dp"
android:background="@color/achtergrond_inputvak"
android:id="@+id/Naam"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Inschrijving_inputvak3"
android:textColor="@color/bordeaurood"
android:textSize="20dp"/>
<EditText
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_marginLeft="82dp"
android:layout_marginRight="15dp"
android:background="@color/achtergrond_inputvak"
android:id="@+id/Leeftijd"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Inschrijving_inputvak4"
android:textColor="@color/bordeaurood"
android:textSize="20dp"/>
<EditText
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_marginLeft="100dp"
android:layout_marginRight="15dp"
android:background="@color/achtergrond_inputvak"
android:id="@+id/Email"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="25dp"
android:layout_marginRight="75dp"
android:layout_marginTop="35dp"
android:textColor="@color/rooderror"
android:id="@+id/Foutmelding"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Inschrijving_btn_SchrijfIn"
android:background="@drawable/rounded_btn"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:layout_marginLeft="200dp"
android:id="@+id/SchrijfIn"
android:drawableLeft="@drawable/arrow"
android:onClick="btnSchrijfIn_Click"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Inschrijving_btn_GaNaarIngeschrevenLeden"
android:background="@drawable/rounded_btn"
android:paddingLeft="5dp" android:paddingRight="5dp"
android:layout_marginLeft="10dp"
android:onClick="btnGaNaarIngeschrevenLeden_Click"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/UpdateText"
android:background="@drawable/rounded_btn"
android:layout_marginLeft="10dp"
android:paddingLeft="5dp" android:paddingRight="5dp"
android:onClick="btnUpdate_Click"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/DeleteText"
android:background="@drawable/rounded_btn"
android:layout_marginLeft="10dp"
android:paddingLeft="5dp" android:paddingRight="5dp"
android:onClick="btnDelete_Click"/>
</LinearLayout>
</LinearLayout>
Upvotes: 1
Views: 64
Reputation: 7070
The logs show that you are trying to downgrade the database from version 200 to 1.
The default implementation of onDowngrade()
rejects any downgrade and throws exception which is visible in your logs.
Solution
: Either override onDowngrade()
in your app or just uninstall and re-install your app.
One error in your code - You need to have a space in between EXISTS and table name
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
Upvotes: 2