无法启动活动,没有这样的表(sqlite)

时间:2016-05-19 03:46:31

标签: android sqlite

我有一个我从未见过的问题。

这是我的logcat的一部分:

  

致命的例外:主要                                                                         处理:ad.training.repertoire,PID:1108                                                                         java.lang.RuntimeException:无法启动活动   ComponentInfo {ad.training.repertoire / ad.training.repertoire.MainActivity}:   android.database.sqlite.SQLiteException:没有这样的表:Ichar(代码   1):,编译时:select * FROM Ichar

以及有关方面:

  

at ad.training.repertoire.MySQLite.donneesBDD(MySQLite.java:73)                                                                             在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);
    }
}

和我的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 = 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});



}

}

你能帮助我吗?

由于

1 个答案:

答案 0 :(得分:1)

根据您的描述和代码,代码中没有任何问题,但如果您在创建此数据库之前在设备中运行应用程序,然后您已应用此逻辑来创建数据库和表以及所有,那么您需要先从设备卸载您的应用程序,否则将无法在打开的应用程序上创建数据库,它无法执行任何操作。

因此,首先从设备上卸载应用,然后再次运行。