开发者

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

  1. fields exist in sqlite datatbase.
  2. foreign keys are correct.
  3. unique fields are correct.
  4. not null fields are correct.
  5. if id is AUTOINCREMENT you do not need to fill it.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜