I found out that the code is correct. The problem was somehow with the database. I deleted the existing table in the database and the app automatically created a new table. The code now works without any problems.