Mon application se bloque quand je clique sur le bouton Ajouter un enregistrent

Résolu/Fermé
rapidegoyes Messages postés 70 Date d'inscription samedi 22 février 2020 Statut Membre Dernière intervention 1 septembre 2023 - 22 févr. 2020 à 20:15
rapidegoyes Messages postés 70 Date d'inscription samedi 22 février 2020 Statut Membre Dernière intervention 1 septembre 2023 - 23 févr. 2020 à 23:39
Bonsoir à tous,

Je vous serais reconnaissant de m'épauler, j'ai cherché dans tous les sens mais je n'y arrive pas.

J'ai suivi un tuto, j'ai bien vérifié la syntaxe, rien à faire mon appli bloque lorsque j'appui sur le bouton destiné à l'affichage des données.

Lorsque je désactive le bouton d'affichage l'appli se lance et s'affiche sans problème.

L'erreur suivante marquée dans l'onglet logcat comme suit :

02-20 23:20:17.377 16522-16522/com.example.sqlitedatabase E/SQLiteLog: (1) near "tableBook_table": syntax error

android.database.sqlite.SQLiteException: near "tableBook_table":
syntax error (code 1): , while compiling: CREATE tableBook_table
(ID INTEGER PRIMARY KEY AUTOINCREMENT, NOM TEXT, AUTEUR TEXT, CATEGORY TEXT)

Voici mon code ci-dessous:

Fichier SQLiteDataBaseHelper.java

1

package com.example.sqlitedatabase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;

import java.util.Locale;

//méthode pour création BDD et Table
public class SQLiteDataBaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Book.db";
public static final String TABLE_NAME = "book_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NOM";
public static final String COL_3 = "AUTEUR";
public static final String COL_4 = "CATEGORY";

//constructeur rempli
public SQLiteDataBaseHelper (Context context)
{
super(context, DATABASE_NAME,null, 1);
}

public void onCreate (SQLiteDatabase db) {
db.execSQL("CREATE table" + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, NOM TEXT, AUTEUR TEXT, CATEGORY TEXT)");
}

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

public boolean insertData(String nom, String auteur, String categorie ) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, nom);
contentValues.put(COL_3,auteur);
contentValues.put(COL_4,categorie);
long result=db.insert(TABLE_NAME, null, contentValues);
if (result == -1)
return false;
else
return true;
}

public Cursor getAllData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("select * from " + TABLE_NAME, null);
return result;

}

}


Mon fichier MainActivity.java


package com.example.sqlitedatabase;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
SQLiteDataBaseHelper db;
//déclaration des objets widget
EditText nomInput, auteurInput, categorieInput;
Button buttonAddData;
Button buttonViewAllData;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new SQLiteDataBaseHelper(this);
nomInput = (EditText) findViewById(R.id.nom_edittext);
auteurInput = (EditText) findViewById(R.id.auteur_edittext);
categorieInput = (EditText) findViewById(R.id.category_edittext);
buttonAddData = (Button) findViewById(R.id.ajouter_button);AddData();
buttonViewAllData = (Button) findViewById(R.id.afficher_donnnes_button);
ViewAll();

}

public void AddData(){
buttonAddData.setOnClickListener(
new View.OnClickListener(){
public void onClick (View view){ // insert the new rav data on click of the button
boolean isInserted = db.insertData(nomInput.getText().toString(), auteurInput.getText()
.toString(), categorieInput.getText().toString());
if (isInserted==true) {
Toast.makeText(MainActivity.this, "Les données sont insérer avec succès", Toast.LENGTH_LONG).show();
} else
Toast.makeText(MainActivity.this, "Les données ne sont pas insérées", Toast.LENGTH_LONG).show();
}
}

);
}

private void ViewAll(){
buttonViewAllData.setOnClickListener(
new View.OnClickListener(){
public void onClick(View view){
Cursor data = db.getAllData();
if (data.getCount()== 0) {
//show message
showMessage("Error","No Data Found !");
return;
}
StringBuffer buffer = new StringBuffer();
while (data.moveToNext()){
buffer.append("ID : " + data.getString(0) + "\n");
buffer.append("Nom :" + data.getString(1) + "\n");
buffer.append("Auteur :" + data.getString(2) + "\n");
buffer.append("Catégorie :" + data.getString(3) + "\n");
}
showMessage("Data", buffer.toString());
}
}

);
}

public void showMessage(String titel, String message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(titel);
builder.setMessage(message);
builder.show();
}


}


Mon fichier activity_main.xml

Mon fichier actiuvity_main.xml


<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/nom_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nom"
android:textStyle="bold"
android:textSize="30sp"
/>

<EditText
android:id="@+id/nom_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="nom"
/>

<TextView
android:id="@+id/auteur_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Auteur"
android:textStyle="bold"
android:textSize="30sp" />


<EditText
android:id="@+id/auteur_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="auteur"
android:inputType="text" />

<TextView
android:id="@+id/category_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Catégorie"
android:textSize="30sp"
android:textStyle="bold" />

<EditText
android:id="@+id/category_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="catégorie"
android:inputType="text" />

<Button
android:id="@+id/ajouter_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5sp"
android:layout_marginTop="3sp"
android:layout_marginBottom="3sp"
android:background="@color/colorAccent"
android:clickable="true"
android:padding="20sp"
android:text="Ajouter à la base"
android:textSize="20sp"
android:textStyle="bold"/>

<Button
android:id="@+id/afficher_donnnes_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5sp"
android:layout_marginTop="3sp"
android:layout_marginBottom="3sp"
android:background="@color/colorAccent"
android:clickable="true"
android:padding="20sp"
android:text="Afficher les données"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="bold" />

</LinearLayout>


Mon fichier string.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="app_name">SQLiteDatabase</string>
<string name="nom_textview">nom</string>
<string name="nom_edittext">nom</string>
<string name="auteur_textview">auteur</string>
<string name="auteur_edittext">auteur</string>
<string name="category_textview">categorie</string>
<string name="category_edittext">categorie</string>
<string name="ajouter_button">Ajouter à la base</string>
<string name="afficher_donnnes_button">Afficher les données</string>

</resources>


Je suppose qu'il y a une erreur dans le passage du code suivant qu'en pensez vous ?

public void  onCreate (SQLiteDatabase db) {
db.execSQL("CREATE table" + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, NOM TEXT, AUTEUR TEXT, CATEGORY TEXT)");
}


Merci pour votre aide

Cordialement
rapidego
A voir également:

2 réponses

jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650
22 févr. 2020 à 21:22
Bonjour
en lisant le message d'erreur on se rencontre qu'il manque un espace entre le mot table et le nom de ta table
1
rapidegoyes Messages postés 70 Date d'inscription samedi 22 février 2020 Statut Membre Dernière intervention 1 septembre 2023 7
23 févr. 2020 à 23:39
Bonsoir,

Super un grand merci à jordane45 tout fonctionne!!!!!!!

Très cordialement
rapidegoyes et à +++
0