protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.predmet_zapis); dbHelper = new DBHelper(this); ed1 = (EditText) findViewById(R.id.et_predmet); bt1 = (Button) findViewById(R.id.but1); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { bd = dbHelper.getWritableDatabase(); ContentValues cv = new ContentValues(); String chisl1 = ed1.getText().toString(); cv.put("chisl", chisl1); long rowID = bd.insert("mypoints", null, cv); Log.d("LOG_TAG", "row inserted, ID = " + rowID); dbHelper.close(); } }); } public class DBHelper extends SQLiteOpenHelper { public DBHelper(Context context) { // конструктор суперкласса super(context, "myDB", null, 1); } @Override public void onCreate(SQLiteDatabase db) { // создаем таблицу с полями Log.d("123","cоздана БД"); db.execSQL("create table mypoints (" + "id integer primary key autoincrement," + "chisl text"+");"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } 

I try to create a DB, but does not form. Writes "no such table: mypoints".

I deleted the application from the phone, reinstalled it - it worked, but after I decided to change the table name, it stopped working with the same error, why is that?

  • one
    Your Example does not describe the creation of the database, but rather the description of the onClick class, when you click the bt1 button, which implements work with the database, or rather the insert, into the "mypoints" table of values ​​from EditText - ed1. And the error says that such a table as "mypoints" does not exist. - Shwarz Andrei
  • I removed the application from the phone, reinstalled it worked, but after I decided to change the table name, it stopped working with the same error, why is that? - roma
  • There is no error in the code. If you change the name of the table, you must also change the name - where you insert the value. Otherwise, an error will pop up that there is no such table as you have. Lay out the code when you have a mistake, and not when it was working, save time for yourself and others, because looking for a mistake where it is not there is sometimes much more difficult than where it is enough. - Shwarz Andrei
  • That's just the point, I posted the code when there was an error. The name of the database changed in both places - roma

1 answer 1

onCreate is called when there is no database. When updating the database application already exists and onCreate not called, therefore a new table is not created.

If you decide to change the database structure, then you need to increase the version number of the database in the call to the parent constructor:

 super(context, "myDB", null, 2); 

And add the logic to the onUpgrade function: what to do to bring the old database to the new structure:

 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { if(oldVersion < 2) { db.execSQL("alter table mytable rename to mypoints;"); } } 
  • unUpgrade implemented? Is the error the same? - sercxjo
  • Thank you very much !!!))) flew out of my head that the table needs to be renamed) - roma
  • @roma, if the answer has solved your problem, then you need to mark it "true" - this is the tick button to the left of the question body, immediately below the arrows. Also vote for a question by pressing the up arrow is welcome. - Yuriy SPb
  • one
    If the application is a test and not from users, then it is easier to simply remove the application from the device under test, the database will also be deleted and at the next test run it will be recreated with a new table - pavlofff