Mhche Mhche - 1 month ago 18
Android Question

insert to sqlite false

Can someone help me whith this?
how-to-insert-data-into-a-sqlite-database-in-android

I cannot understand and find out why the result of the insert is not displayed at the main page of the app.
I do the insert of the new student but after i cannot see my new insert.I also have checked the sql file and shows that every time i make a new insert only the id is created and the name(the data i input) still blank,like i have not typed anything.

What happens?

Edit (this is the insert):

public void insertRecord(HashMap<String, String> queryValues) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("Name", queryValues.get("name"));
database.insert("student", null, values);
database.close();
}


(but i don't think that this is the problem)

Answer

I have go through the code and found some case sensitive errors of database column names.

Inside the onCreate method of MainActivity find the following line

ListAdapter adapter = new SimpleAdapter( MainActivity.this,studentList, R.layout.view_student_entry, new String[] { "studentId","studentName"}, new int[] {R.id.studentId, R.id.studentName});

it should be

ListAdapter adapter = new SimpleAdapter( MainActivity.this,studentList, R.layout.view_student_entry, new String[] { "StudentId","StudentName"}, new int[] {R.id.studentId, R.id.studentName});

What I changed is capital "S" instead of simple "s" of "StudentId" and "StudentName"

and there should be another change

go and find the "addNewStudent" method of new Student method

then change the following line

queryValues.put("studentName", studentName.getText().toString());

to

queryValues.put("StudentName", studentName.getText().toString());

Your column name of data entering should be exactly same as database column name.

Now it's done... enjoy the code.....