0

I have a problem that I have never met.

This is a part of my logcat :

FATAL EXCEPTION: main Process: ad.training.repertoire, PID: 1108 java.lang.RuntimeException: Unable to start activity ComponentInfo{ad.training.repertoire/ad.training.repertoire.MainActivity}: android.database.sqlite.SQLiteException: no such table: Ichar (code 1): , while compiling: select * FROM Ichar

and the lines concerned :

at ad.training.repertoire.MySQLite.donneesBDD(MySQLite.java:73) at ad.training.repertoire.MainActivity.onCreate(MainActivity.java:35)

My MainActivity :

    public class MainActivity extends AppCompatActivity  {

    MySQLite sqlite;
    List<Character> characters;
    RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        sqlite = new MySQLite(this);
        characters= new ArrayList<Character>();


 **Ligne 35 : characters = sqlite.donneesBDD();**


        mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView);

        mRecyclerView.setHasFixedSize(true);


        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new MyAdapter(this,characters);
        mRecyclerView.setAdapter(mAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

and my MySQLite :

public class MySQLite extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "character";
private static final int DATABASE_VERSION = 1;
private static final String CHARACTER_TABLE = "Ichar";
private static final String CHAR_TABLE = "create table " + CHARACTER_TABLE + "(nom TEXT,prenom TEXT ,numero TEXT primary key)";


Context context;


public MySQLite(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    // Création de la base de données
    // on exécute ici les requêtes de création des tables
    db.execSQL(CHAR_TABLE); // création table "character"
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Mise à jour de la base de données
    // méthode appelée sur incrémentation de DATABASE_VERSION
    // on peut faire ce qu'on veut ici, comme recréer la base :
    onCreate(db);
}


public void InsererBDD(String nom, String prenom, String numero) {

    Log.d("insert", "before insert");

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues entree = new ContentValues();
    entree.put("nom", nom);
    entree.put("prenom", prenom);
    entree.put("numero", numero);

    db.insert(CHARACTER_TABLE, null, entree);

    db.close();
    Toast.makeText(context, "insérer entrée", Toast.LENGTH_LONG);

    Log.i("insert", "after insert");
}


public List<Character> donneesBDD() {

    List<Character> modelList = new ArrayList<Character>();
    String query = "select * FROM " + CHARACTER_TABLE;

    SQLiteDatabase db = this.getWritableDatabase();

ligne 73 : Cursor cursor = db.rawQuery(query, null);

    if (cursor.moveToFirst()) {
        do {
            Character model = new Character();
            model.setNom(cursor.getString(0));
            model.setPrenom(cursor.getString(1));
            model.setNumero(cursor.getString(2));

            modelList.add(model);

        } while (cursor.moveToNext());
    }

    Log.d("donnee character", modelList.toString());


    return modelList;

}

public void supprimerLigne(String numero){

    SQLiteDatabase db = getWritableDatabase();
    db.delete(CHARACTER_TABLE , " numero" + " = ?", new String[] {numero});



}

}

Can you help me?

Thanks

Ian
  • 30,182
  • 19
  • 69
  • 107

1 Answers1

1

According to your description and code there is not any issue in code, but if you have run application in device before creating this database, and then you have applied this logic for creating db and tables and all, then you need to first uninstall your application from device, otherwise database will not be create on open app and it couldn't perform any operation.

So, first uninstall app from device and then run again.

Vickyexpert
  • 3,147
  • 5
  • 21
  • 34