2016-05-17 8 views
1

すべてのボタンの後に、ボタン(更新ボタン、inschrijven、showallpeople、またはdelete)をクリックしたので、SQLデータベースと何か関係のあるボタンはすべて失敗しています。なぜかわからないけど、列名と関係があると思う。私はそれを理解していない!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> 
+0

たちにログを表示します。 logcatを読まないと、あなたのアプリ内で何が起こっているのか推測できません。 –

+2

ようこそStackOverflowへ!他の人があなたの問題を理解するのを助けるために、コードのサンプル、ログ(LogCatなど)の出力、または[最小限の、完全で検証可能な例を示す](http://stackoverflow.com/help/)を投稿してください。 mcve) –

+0

しかし、私は試してみましたが、彼らはそれを迷惑メールとして認識しています、良いスクリーンショットですか? – peukertje

答えて

2

ログには、データベースをバージョン200から1にダウングレードしようとしていることが表示されます。 デフォルトの実装onDowngrade()は、ログに表示されるダウングレードおよびスロー例外を拒否します。

Solution:アプリにonDowngrade()を上書きするか、アプリをアンインストールして再インストールするだけです。

一つ、あなたのコードのエラー - あなたは間にスペースを持っている必要が存在し、テーブル名

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME); 
    onCreate(db); 
} 
+0

次のエラーを見てください。 – peukertje

+0

アプリをアンインストールして再インストールします。あなたのデータベースにはいくつかの矛盾があります。それがあなたを助けたら答えを受け入れてください。 –

+0

あなたは私の友人に素晴らしい仕事をしています!本当にありがとう!!私はここで新しいです、私は最初の15の評判に到達する必要があります:/申し訳ありませんが本当にありがとう! – peukertje

関連する問題