0

第二次插入数据时出现此错误。我第一次在数据库中插入数据时效果很好。但后来我的应用程序被强制关闭或添加了项目但未显示在列表视图中。当我尝试插入第二条记录时,我的 logcat 看起来像

05-06 04:14:06.220: E/SQLiteDatabase(773): Error inserting imei_id=0 c_addr=54     i_date=6/5/2013 c_nm=rte e_date=6/5/2013 cl_no=tre exe_flg=true l_no=65 l_type=dfdg
05-06 04:14:06.220: E/SQLiteDatabase(773): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
05-06 04:14:06.220: E/SQLiteDatabase(773):  at android.database.sqlite.SQLiteStatement.native_executeInsert(Native Method)
05-06 04:14:06.220: E/SQLiteDatabase(773):  at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:113)
05-06 04:14:06.220: E/SQLiteDatabase(773):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
05-06 04:14:06.220: E/SQLiteDatabase(773):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1591)
05-06 04:14:06.220: E/SQLiteDatabase(773):  at com.smartwallet.database.DbAdapterDLicense.insertDL(DbAdapterDLicense.java:49)
05-06 04:14:06.220: E/SQLiteDatabase(773):  at com.exampl.smartwallet.Add_DLicense.onClick(Add_DLicense.java:139)

我要添加记录的课程是

package com.exampl.smartwallet;

import java.util.Calendar;

import com.smartwallet.database.DbAdapterDLicense;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Add_DLicense extends Activity implements OnClickListener
{
private TextView et_idt,et_edt;
private EditText et_cnm,et_lno,et_add,et_cno,et_ltype;
private Button bt_add;
private Long dlid,l;    
private int mYear,mMonth, mDay;
static final int DATE_DIALOG_IDI = 0;
static final int DATE_DIALOG_IDE = 1;
private DatePickerDialog.OnDateSetListener mDateSetListenerI;
private DatePickerDialog.OnDateSetListener mDateSetListenerE;
private DbAdapterDLicense DBA;
//private TelephonyManager tm;
private String imei="9898";

@Override
public void onCreate(Bundle b) 
{
    super.onCreate(b);
    setContentView(R.layout.add_dlicense);
    dlid=null;
    Bundle extras=getIntent().getExtras();

    dlid=(b==null)? null:(Long)b.getSerializable("imei_id");
    if(extras!=null)
    {
        dlid=extras.getLong("imei_id");
    }

    DBA=new DbAdapterDLicense(this);
    DBA.open();


    et_cnm=(EditText)findViewById(R.id.et_cnm);
    et_idt=(TextView)findViewById(R.id.et_idt);
    et_edt=(TextView)findViewById(R.id.et_edt);
    et_lno=(EditText)findViewById(R.id.et_lno);
    et_add=(EditText)findViewById(R.id.et_add);
    et_cno=(EditText)findViewById(R.id.et_cno);
    et_ltype=(EditText)findViewById(R.id.et_ltype);
    et_idt.setOnClickListener(this);
    et_edt.setOnClickListener(this);

    bt_add=(Button)findViewById(R.id.bt_save);

    bt_add.setOnClickListener(this);        

    mDateSetListenerI = new DatePickerDialog.OnDateSetListener() 
    {
            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) 
            {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateDisplay1();
            }
        };

        final Calendar c1 = Calendar.getInstance();
        mYear = c1.get(Calendar.YEAR);
        mMonth = c1.get(Calendar.MONTH);
        mDay = c1.get(Calendar.DAY_OF_MONTH);

        updateDisplay1();
        mDateSetListenerE = new DatePickerDialog.OnDateSetListener() 
        {
                public void onDateSet(DatePicker view, int year, 
                                      int monthOfYear, int dayOfMonth) 
                {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay2();
                }
            };

    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);

    updateDisplay2();
}

public boolean validate() {

    if(et_cnm.getText().toString().equals(""))
        return false;
    if(et_lno.getText().toString().equals(""))
        return false;
    if(et_add.getText().toString().equals(""))
        return false;
    if(et_cno.getText().toString().equals(""))
        return false;
    if(et_ltype.getText().toString().equals(""))
        return false;
    return true;
}

@Override
public void onClick(View v) 
{
     if(v.equals(et_idt))
        {
            showDialog(DATE_DIALOG_IDI);
        }
         if(v.equals(et_edt))
        {
            showDialog(DATE_DIALOG_IDE);
        }

    if(!validate()) {
        Toast.makeText(this, "Blank fields are not allowed", Toast.LENGTH_SHORT).show();
        return;
    }
    if(v.equals(bt_add))
    {
        if(dlid==null)
        {
            l = DBA.insertDL(0, et_cnm.getText().toString(),et_idt.getText().toString(),et_edt.getText().toString(),et_lno.getText().toString(),et_add.getText().toString(),et_cno.getText().toString(),et_ltype.getText().toString(),"true");
            Toast.makeText(Add_DLicense.this, "Item Added", 2000).show();
            imei+=l;
            //DBA.updateDL(l, Long.parseLong(imei), et_cnm.getText().toString(),et_idt.getText().toString(),et_edt.getText().toString(),et_lno.getText().toString(), et_add.getText().toString(),et_cno.getText().toString(),et_ltype.getText().toString(),"true");
        }
        finish();
    }

}
private void updateDisplay1() 
{
    et_idt.setText(""+mDay+"/"+(mMonth+1)+"/"+mYear);   
}
private void updateDisplay2() 
{
    et_edt.setText(""+mDay+"/"+(mMonth+1)+"/"+mYear);   
}

 @Override
    protected Dialog onCreateDialog(int id) 
    {
        if(id==DATE_DIALOG_IDI)
        {
            return new DatePickerDialog(this,
                    mDateSetListenerI,
                    mYear, mMonth, mDay);
        }
        else if(id==DATE_DIALOG_IDE)
        {
            return new DatePickerDialog(this,
                    mDateSetListenerE,
                    mYear, mMonth, mDay);
        }
        return null;
    }

@Override
protected void onDestroy() 
{
    // TODO Auto-generated method stub
    DBA.close();
    super.onDestroy();
}
}

和 DbAdapter 查询是

public long insertDL(long imei_id,String c_nm,String i_date, String e_date,String l_no,String cl_no,String c_addr,String l_type,String exe_flg) 
{
    ContentValues values = createContentValues_dl(imei_id,c_nm,i_date, e_date, l_no,cl_no,c_addr,l_type,exe_flg);
    return db.insert(T_DLICENSE, null, values);
}

CreateTable 中的表是

private static final String DRIVINGLICENSE 
= "create table DrivingLicense " +
"(dlid integer primary key autoincrement, " +
"imei_id int(20), " +
"c_nm text not null,"+
"i_date text not null, " +
"e_date text not null,"+
"l_no text not null,"+
"cl_no text not null,"+
"c_addr text not null,"+
"exe_flg text not null,"+
"l_type text not null,UNIQUE(imei_id));";

请帮我插入多条记录。我已经搜索但未能找到解决方案。因为第一条记录插入正确但不超过……提前谢谢。

4

2 回答 2

0

您正在为该列插入相同的值imei_id您不能这样做,因为它被声明为唯一的。

于 2013-05-06T04:39:52.550 回答
0

失败的约束意味着您的插入违反了您在创建表时指定的约束。在您的情况下,这是很多字段不为空,一个是键,一个是唯一的。您应该在这里做的是使用调试器,在插入之前停止代码并检查所有值是否符合您的预期,并满足您的约束。

您可能要检查的一件事是字段 imei_id - 您指定它是唯一的,但似乎您对函数 insertDL 的调用将此作为常量 0..

于 2013-05-06T04:42:26.967 回答