安卓得到错误插入到DATABSE和code

安卓得到错误插入到DATABSE和code

本文介绍了安卓得到错误插入到DATABSE和code 19:约束失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写了简单的 DataBaseHelper 来使用SQLite的机器人。之后创建类:

 公共类数据库处理器扩展SQLiteOpenHelper {

        私有静态字符串DB_PATH =;

        私有静态最后弦乐DATABASE_NAME =TSMS;

        私有静态字符串RECEIVE_FIELDS_TABLE =ReceiveFields;

        私有静态最后弦乐COLUMN_ID =ID;

        私有静态最后弦乐COLUMN_LASTID =lastId;

        私有静态最后弦乐COLUMN_SMSNUMBER =smsNumber;

        私有静态最后弦乐COLUMN_MOBILENUMBER =移动电话号码;

        私有静态最后弦乐COLUMN_SENDERNAME =发出者;

        私有静态最后弦乐COLUMN_SMSBODY =smsBody;

        私有静态最后弦乐COLUMN_RECEIVEDATE =receiveDate;

        私有静态最终诠释DATABASE_VERSION = 1;

        私人SQLiteDatabase mDataBase;

        / *更新DATABASE_CREATE领域* /

        私有静态最后弦乐DATABASE_CREATE =CREATE TABLE+ RECEIVE_FIELDS_TABLE +(
            + COLUMN_ID +INTEGER UNIQUE
            + COLUMN_LASTID +INTEGER UNIQUE
            + COLUMN_SMSNUMBER +VARCHAR独一无二的,
            + COLUMN_MOBILENUMBER +VARCHAR
            + COLUMN_SENDERNAME +VARCHAR
            + COLUMN_SMSBODY +TEXT
            + COLUMN_RECEIVEDATE +VARCHAR,PRIMARY KEY(+ COLUMN_ID +,+ COLUMN_LASTID +,+));

    私人上下文的背景下;

    公共数据库处理器(上下文的背景下){

        超(背景下,DATABASE_NAME,空,DATABASE_VERSION);

    }


    @覆盖
    公共无效的onCreate(SQLiteDatabase sqLiteDatabase){

        sqLiteDatabase.execSQL(DATABASE_CREATE);

    }

    @覆盖
    公共无效onUpgrade(SQLiteDatabase sqLiteDatabase,诠释oldVersion,诠释静态网页){


        sqLiteDatabase.execSQL(DROP TABLE IF EXISTS+ RECEIVE_FIELDS_TABLE);

        //再次创建表
        的onCreate(sqLiteDatabase);
    }

    //添加新领域
    公共无效addToReceived(ReceiveFields场){

        SQLiteDatabase DB = this.getWritableDatabase();

        ContentValues​​值=新ContentValues​​();

        values​​.put(COLUMN_LASTID,fields.getLastId()); // ReceiveFields最后ID

        values​​.put(COLUMN_MOBILENUMBER,fields.getMobileNumber()); // ReceiveFields手机号码

        values​​.put(COLUMN_SENDERNAME,fields.getSenderName()); // ReceiveFields手机号码

        values​​.put(COLUMN_SMSBODY,fields.getSmsBody()); // ReceiveFields手机号码

        values​​.put(COLUMN_SMSNUMBER,fields.getSmsNumber()); // ReceiveFields手机号码

        values​​.put(COLUMN_MOBILENUMBER,fields.getMobileNumber()); // ReceiveFields手机号码

        values​​.put(COLUMN_RECEIVEDATE,将String.valueOf(fields.getReceiveDate())); // ReceiveFields手机号码

        //插入行
        db.insert(RECEIVE_FIELDS_TABLE,空,价值观);

        db.close(); //闭幕数据库连接
    }
}
 

我得到错误插入到数据库,这种方式:

  db.addToReceived(新ReceiveFields(

                            Long.valueOf(STR1 [0]),

                            STR1 [1],

                            STR1 [2],

                            URLDe coder.de code(STR1 [3],UTF-8),

                            URLDe coder.de code(STR1 [4],UTF-8),

                            ct.getGregorianDate()));
 

Log.i 结果:

  29904757 30007227 00120504001 00120504001 sssasaS 2014年8月3日
 

全LogCate结果:

 六月八日至28号:35:29.974 2698年至2698年/ ir.tsms E /数据库:错误插入lastId = 29904755 receiveDate =到2014年/ 8/3 =者姓名09127574751移动电话号码= 09127574751 smsNumber = 30007227 smsBody =
    android.database.sqlite.SQLiteConstraintException:错误code 19:约束失败
            在android.database.sqlite.SQLiteStatement.native_execute(本机方法)
            在android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)
            在android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549)
            在android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
            在ir.tsms.DataBase.DatabaseHandler.addToReceived(DatabaseHandler.java:96)
            在ir.tsms.wsdl.TSMS.getReceivedSMS(TSMS.java:141)
            在ir.tsms.Receive.ResivedSMS.getResivedSMS(ResivedSMS.java:41)
            在ir.tsms.Receive.ResivedSMS< INIT>(ResivedSMS.java:36)
            在ir.tsms.Activities.DashboardActivity.onTabSelected(DashboardActivity.java:160)
            在android.support.v7.app.ActionBarImplBase.selectTab(ActionBarImplBase.java:486)
            在android.support.v7.app.ActionBarImplBase.addTab(ActionBarImplBase.java:410)
            在android.support.v7.app.ActionBarImplBase.addTab(ActionBarImplBase.java:401)
            在ir.tsms.Activities.DashboardActivity.onCreate(DashboardActivity.java:68)
            在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
            在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
            在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
            在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
            在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
            在android.os.Handler.dispatchMessage(Handler.java:99)
            在android.os.Looper.loop(Looper.java:123)
            在android.app.ActivityThread.main(ActivityThread.java:4627)
            在java.lang.reflect.Method.invokeNative(本机方法)
            在java.lang.reflect.Method.invoke(Method.java:521)
            在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
            在dalvik.system.NativeStart.main(本机方法)
 

在ir.tsms.wsdl.TSMS.getReceivedSMS(TSMS.java:141)行是:

  db.addToReceived(新ReceiveFields(
 

解决方案

检查这些东西,它可能会导致这些原因:

1,你没有添加 COLUMN_ID 所以很空。

2 - COLUMN_ID COLUMN_LASTID 不是唯一 (清除数据库或Android应用程序的数据,也许它存储在数据库你第一次运行它,现在再要添加你不能因为它不是唯一的,现在,每次运行时,您的分贝是在内存中,它不clread,以清除它,你必须删除应用程序,数据或运行删除SQL语句)

所以我建议你看看你的数据库文件,如果你使用Eclipse试试这个:

3 - COLUMN_ID COLUMN_LASTID 不是整型我的意思是 fields.getLastId() 可以返回字符串

检查正是你的表的定义与你的价值观插入!

i'm wrote simple DataBaseHelper to use SQlite in android. after create class as :

public class DatabaseHandler extends SQLiteOpenHelper{

        private static String DB_PATH                   = "";

        private static final String DATABASE_NAME       = "tsms";

        private static String RECEIVE_FIELDS_TABLE      = "ReceiveFields";

        private static final String COLUMN_ID           = "id";

        private static final String COLUMN_LASTID       = "lastId";

        private static final String COLUMN_SMSNUMBER    = "smsNumber";

        private static final String COLUMN_MOBILENUMBER = "mobileNumber";

        private static final String COLUMN_SENDERNAME   = "senderName";

        private static final String COLUMN_SMSBODY      = "smsBody";

        private static final String COLUMN_RECEIVEDATE  = "receiveDate";

        private static final int DATABASE_VERSION = 1;

        private SQLiteDatabase mDataBase;

        /* UPDATE DATABASE_CREATE FIELD*/

        private static final String DATABASE_CREATE = "CREATE TABLE " + RECEIVE_FIELDS_TABLE + "("
            + COLUMN_ID           + " INTEGER  UNIQUE , "
            + COLUMN_LASTID       + " INTEGER  UNIQUE , "
            + COLUMN_SMSNUMBER    + " VARCHAR  UNIQUE , "
            + COLUMN_MOBILENUMBER + " VARCHAR , "
            + COLUMN_SENDERNAME   + " VARCHAR , "
            + COLUMN_SMSBODY      + " TEXT , "
            + COLUMN_RECEIVEDATE  + " VARCHAR , PRIMARY KEY (" + COLUMN_ID + ", " + COLUMN_LASTID + ", " + "))";

    private Context context;

    public  DatabaseHandler(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        sqLiteDatabase.execSQL(DATABASE_CREATE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {


        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + RECEIVE_FIELDS_TABLE);

        // Create tables again
        onCreate(sqLiteDatabase);
    }

    // Adding new fields
    public void addToReceived(ReceiveFields fields) {

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(COLUMN_LASTID,       fields.getLastId());           // ReceiveFields last ID

        values.put(COLUMN_MOBILENUMBER, fields.getMobileNumber());     // ReceiveFields Mobile Number

        values.put(COLUMN_SENDERNAME,   fields.getSenderName());       // ReceiveFields Mobile Number

        values.put(COLUMN_SMSBODY,      fields.getSmsBody());          // ReceiveFields Mobile Number

        values.put(COLUMN_SMSNUMBER,    fields.getSmsNumber());        // ReceiveFields Mobile Number

        values.put(COLUMN_MOBILENUMBER, fields.getMobileNumber());     // ReceiveFields Mobile Number

        values.put(COLUMN_RECEIVEDATE,  String.valueOf(fields.getReceiveDate())); // ReceiveFields Mobile Number

        // Inserting Row
        db.insert(RECEIVE_FIELDS_TABLE, null, values);

        db.close(); // Closing database connection
    }
}

I get Error Inserting to database with this way:

db.addToReceived(new ReceiveFields(

                            Long.valueOf(str1[0]),

                            str1[1],

                            str1[2],

                            URLDecoder.decode(str1[3], "UTF-8"),

                            URLDecoder.decode(str1[4], "UTF-8"),

                            ct.getGregorianDate()));

Log.i Result:

 29904757    30007227    00120504001    00120504001     sssasaS     2014/8/3

full LogCate Result:

08-28 06:35:29.974    2698-2698/ir.tsms E/Database﹕ Error inserting lastId=29904755 receiveDate=2014/8/3 senderName=09127574751 mobileNumber=09127574751 smsNumber=30007227 smsBody=
    android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
            at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
            at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)
            at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549)
            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
            at ir.tsms.DataBase.DatabaseHandler.addToReceived(DatabaseHandler.java:96)
            at ir.tsms.wsdl.TSMS.getReceivedSMS(TSMS.java:141)
            at ir.tsms.Receive.ResivedSMS.getResivedSMS(ResivedSMS.java:41)
            at ir.tsms.Receive.ResivedSMS.<init>(ResivedSMS.java:36)
            at ir.tsms.Activities.DashboardActivity.onTabSelected(DashboardActivity.java:160)
            at android.support.v7.app.ActionBarImplBase.selectTab(ActionBarImplBase.java:486)
            at android.support.v7.app.ActionBarImplBase.addTab(ActionBarImplBase.java:410)
            at android.support.v7.app.ActionBarImplBase.addTab(ActionBarImplBase.java:401)
            at ir.tsms.Activities.DashboardActivity.onCreate(DashboardActivity.java:68)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
            at android.app.ActivityThread.access$2300(ActivityThread.java:125)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:4627)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:521)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
            at dalvik.system.NativeStart.main(Native Method)

at ir.tsms.wsdl.TSMS.getReceivedSMS(TSMS.java:141) line is :

db.addToReceived(new ReceiveFields(
解决方案

Check these things, it may causes by these reason:

1- you did not add COLUMN_ID to values so it is null.

2-COLUMN_ID and COLUMN_LASTID are not UNIQUE (clear your database or android app data , maybe it stored in your db first time you ran it and now again you want to add you can not because it is not unique now, every time you run, your db is in memory and it is not clread, in order to clear it you must remove app data or run delete SQL statement)

so i suggest you to see your DB file, if you use eclipse try this:

http://www.tylerfrankenstein.com/browse-android-emulator-sqlite-database-eclipse

3-COLUMN_ID and COLUMN_LASTID are not int i mean fields.getLastId() may return String

check exactly your table defination and your values to insert !!!

这篇关于安卓得到错误插入到DATABSE和code 19:约束失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:28