gpt4 book ai didi

java - Android 插入数据库时​​出错,代码 19 : constraint failed

转载 作者:行者123 更新时间:2023-12-02 05:30:38 24 4
gpt4 key购买 nike

我编写了简单的DataBaseHelper来在android中使用SQlite。创建类后:

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
}
}

我用这种方式插入数据库时​​出错:

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 结果:

 29904757    30007227    00120504001    00120504001     sssasaS     2014/8/3

完整的 LogCate 结果:

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)

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

db.addToReceived(new ReceiveFields(

最佳答案

检查这些事情,可能是由这些原因引起的:

1-您没有将 COLUMN_ID 添加到 values,因此它为空。

2-COLUMN_IDCOLUMN_LASTID 不是 UNIQUE (清除您的数据库或 Android 应用程序数据,也许它存储在您的数据库中第一次运行它,现在再次想要添加,但不能,因为它现在不是唯一的,每次运行时,您的数据库都在内存中并且没有 clread,为了清除它,您必须删除应用程序数据或运行删除SQL语句)

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

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

3-COLUMN_IDCOLUMN_LASTID 不是 int 我的意思是 fields.getLastId() 可能返回 String

准确检查您的表定义和要插入的值!!!

关于java - Android 插入数据库时​​出错,代码 19 : constraint failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25582156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com