0

I am trying to create SQLite database via android 3.1. But it doesn't work as code following below. Please kindly help thanks.

Main Activity.class

public class MainActivity extends AppCompatActivity { DatabaseHelper myDb; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDb = new DatabaseHelper(this);  // this is contact, this is new extension of DatabaseHelper

    } 
} 

DatabaseHelper.class

public class DatabaseHelper extends SQLiteOpenHelper { 
    public static final String DATABASE_NAME = "Iters.db";
    public static final String TABLE_NAME = "iters_table"; //called onCreate method
    public static final String COL_1 = "ID";
    public static final String COL_2 = "NAME";
    public static final String COL_3 = "SURNAME";
    public static final String COL_4 = "MARKS";


    public DatabaseHelper(Context context) {

        super(context, DATABASE_NAME, null, 1);
        SQLiteDatabase db = this.getWritableDatabase(); 
    }

    @Override

    public void onCreate(SQLiteDatabase db) {

        db.execSQL("CREATE TABLE TABLE_NAME (ID INTEGER PRIMARY KEY AUTOINCREMENT," +
                "NAME TEXT," +
                "SURNAME TEXT," +
                "MARKS INTEGER)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
        onCreate(db);
    } 
 }
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Butthry
  • 11
  • 2

1 Answers1

0

It would appear that your issue is probably that you are naming the table TABLE_NAME rather than iters_table.

You could correct this by using :-

db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY," +
                "NAME TEXT," +
                "SURNAME TEXT," +
                "MARKS INTEGER)");
  • Note I've also removed the AUTOINCREMENT keyword as you very likely don't need it (the ID column will still be auto-generated 1, then 2 then 3 etc (probably)) as :-
    • a) It's perhaps most mis-used keyword, and
    • b) It's a bit costly in terms of performance
    • You may wish to have a read of SQLite Autoincrement, considering that the the first sentence is The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and disk I/O overhead and should be avoided if not strictly needed. It is usually not needed. I believe that the overhead is a loss of performance to the tune of between 8% and 12% (as per What are the overheads of using AUTOINCREMENT for SQLite on Android? )
MikeT
  • 51,415
  • 16
  • 49
  • 68
  • Dear MikeT, First I would like to say sorry for missing this. and really thanks for your advised answer, I have just seen and may continue using this platform from now. nice to know you, have a great day. :) – Butthry Nov 25 '22 at 04:04