-4

I have recently started developing some app in Android studio. I am trying to understand SQLite database. I am trying to create a simple table and inserting into it. My application crashes after running the following code.

SQLiteDatabase db = openOrCreateDatabase("StudentDB",android.content.Context.MODE_PRIVATE, null);                      
db.execSQL("CREATE TABLE IF NOT EXISTS student "+  " (name VARCHAR, number VARCHAR );");
String name = ((EditText) findViewById(R.id.NameField)).getText().toString();
String num = ((EditText) findViewById(R.id.NumberField)).getText().toString();
db.execSQL("INSERT INTO student VALUES('"+name+"','"+num+"');");
JJS
  • 6,431
  • 1
  • 54
  • 70

1 Answers1

0

Create table in onCreate() method:

String CREATE_TABLE = "CREATE TABLE student(name text,num text)";
db.execSQL(CREATE_TABLE);

you have to use the insert() method instead of execSQL?

ContentValues insertValues = new ContentValues();
insertValues.put("name ", name);
insertValues.put("num", num);
db.insert("student", null, insertValues);
Saritha G
  • 2,588
  • 1
  • 15
  • 27