android database insert problem
I want to take the datas from the edittext and then insert it into the sqlite. now i am doing like
ContentValues value开发者_运维问答=new ContentValues();
value.put("Name",name);
value.put("id", password);
value.put("email",email);
db.insert("EMP",null,value);
Name,password and email are the variables,but this is not working as no data is being entered into the table.
Any solutions please.
Your code looks OK. The insert function returns the id of the newly created row. You could test that to check whether the call was successful. A value of -1 indicates that an error occurred.
I am a bit concerned that you have a field called id, especially one that is not numeric. It should be OK, but _id is reserved for the row id, and you definitely do not want to go messing with that.
try adding these three lines
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
before your db.insert line.
please try to pass _id as the second parameter of insert() instead of null.
the following code works fine in my program.
dbSqlite.insert(tableName, DataBaseConstant.FIELD_ID,
initialValues);
hope can help you to solve your problem.
if db.insert() return -1 an error occurred. you must check
- fields exist in sqlite datatbase.
- foreign keys are correct.
- unique fields are correct.
- not null fields are correct.
- if id is AUTOINCREMENT you do not need to fill it.
精彩评论