在我的sql数据库中插入数据后应用程序崩溃

时间:2016-05-17 19:21:17

标签: android database sqlite

在我点击页面上的每个按钮(按钮更新,inschrijven,showallpeople或删除)后,每个与SQL数据库有关的按钮都失败了。我不知道为什么,但我认为它与列名有关。我不明白!

所以这是我所有页面的代码。

这是我的SQLiteHelper类:

      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;

}
}

这是我的MainActivity类:

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();
}

这是我的布局文件:

    <?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>

1 个答案:

答案 0 :(得分:2)

日志显示您正在尝试将数据库从版本200降级为1。 Solution的默认实现拒绝在日志中显示的任何降级和抛出异常。

onDowngrade():在您的应用中覆盖@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME); onCreate(db); } ,或者只是卸载并重新安装您的应用。

代码中的一个错误 - 您需要在EXISTS和表名之间留一个空格

<ToggleButton
    android:id="@+id/toggleButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/btntoggle_selector"
    android:textColor="@android:color/white"
    android:textOff="Toggle Button OFF"
    android:textOn="Toggle Button ON "
    android:onClick="onToggleButtonClick" />