Android:SQLiteConstraintException:错误代码19:约束失败

时间:2013-12-31 02:34:24

标签: android sqlite constraints

我在SO中检查了其他示例,我搜索了很多,没有什么对我有用。数据库文件为this(建议编辑后)。

错误

E/Database(274): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
E/Database(274):    at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
E/Database(274):    at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)
E/Database(274):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549)
E/Database(274):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
E/Database(274):    at com.example.nycgasstationhunter.userRegister$2.onClick(userRegister.java:51)
E/Database(274):    at android.view.View.performClick(View.java:2408)
E/Database(274):    at android.view.View$PerformClick.run(View.java:8816)
E/Database(274):    at android.os.Handler.handleCallback(Handler.java:587)
E/Database(274):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/Database(274):    at android.os.Looper.loop(Looper.java:123)
E/Database(274):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/Database(274):    at java.lang.reflect.Method.invokeNative(Native Method)
E/Database(274):    at java.lang.reflect.Method.invoke(Method.java:521)
E/Database(274):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/Database(274):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/Database(274):    at dalvik.system.NativeStart.main(Native Method)

代码

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;

public class userRegister extends Activity{
//database
SQLiteDatabase db;
DBHelper dbhelper;
Context ourContext;
ContentValues cv;

 public void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_register);

//database  
 dbhelper=new DBHelper(this);
 db=dbhelper.getWritableDatabase();
 cv=new ContentValues();
//editText
final EditText userEdit=(EditText)  findViewById(R.id.userEdit);
final EditText emailEdit=(EditText) findViewById(R.id.emailEdit);
final EditText passwordEdit=(EditText)  findViewById(R.id.passwordEdit);
final EditText retypePassEdit=(EditText) findViewById(R.id.retypePassEdit);
//Register button
    Button regButton = (Button) findViewById(R.id.regButton);
      regButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
    //string
    final String userName=userEdit.getText().toString();
    final String emailAddress=emailEdit.getText().toString();
    final String password=passwordEdit.getText().toString();
    final String retypePassword=retypePassEdit.getText().toString();

         if (userName.length()!=0){
            if (emailAddress.length()!=0){
              if (password.length()!=0){
                 if (retypePassword.equals(password)){
                   //save in DB
                    cv.put(DBHelper.USER, userName);
                    cv.put(DBHelper.EMAIL,emailAddress);
                    cv.put(DBHelper.PASSWORD, retypePassword); 

                    db.insert(DBHelper.USER_TABLE, null, cv);

                    Intent intent = new Intent (userRegister.this,Profile.class);    
                     startActivity(intent);
               }
                 else 
                     Toast.makeText(userRegister.this,"Password mismatch", Toast.LENGTH_SHORT).show(); 
            }
              else 
                     Toast.makeText(userRegister.this,"Invalid password", Toast.LENGTH_SHORT).show();
        }
            else 
                 Toast.makeText(userRegister.this,"Invalid email", Toast.LENGTH_SHORT).show();
    }
         else 
             Toast.makeText(userRegister.this,"Invalid user name", Toast.LENGTH_SHORT).show();
 }
});
}
}

当我插入信息并按regButton时,它会将我发送到个人资料活动。数据库中没有保存信息。我不明白LogCat中的错误,我该如何解决?为什么数据库中什么都没有? (我使用SQLite Database Browser来检查数据。)谢谢

3 个答案:

答案 0 :(得分:6)

错误SQLiteConstraintException表示违反了完整性约束。

从您的Create Table SQL命令我认为第一个问题是您的主键。它应该声明为autoincrement

更改字符串createDB:

public final String createDB="create table "+USER_TABLE+"("
                    + C_ID + " integer primary key autoincrement, "
                    + USER + " text not null,"  
                    + EMAIL + " text not null," 
                    + PASSWORD + " text not null," 
                    + TIME + " text not null);";  

此外,所有字段都声明为not null,您应该测试retypePassword是否为空,并且您必须将值设置为字段TIME。

编辑

为了使autoincrement生效,需要增加数据库版本值 在DBHelper类中,将DATABASE_VERSION=1;更改为DATABASE_VERSION=2;

答案 1 :(得分:2)

如果你在评论中提到数据库模式如下(来自this question)...

public final String createDB="create table "+USER_TABLE+"("
                     +C_ID+" integer primary key, "
                     +USER+" text not null,"  
                     +EMAIL+ " text not null," 
                     +PASSWORD+ " text not null," 
                     +TIME+ " text not null);";

...然后您没有为具有TIME约束的not null列指定值。

一些选项:

  • TIME之前向ContentValues添加insert()值。

  • 更改架构并提供合适的默认值,例如

    TIME + " text not null default current_timestamp"
    

    与往常一样,在更改数据库架构时,请删除旧数据库,以便使用新代码调用onCreate()(清除应用程序数据或仅卸载应用程序)。

答案 2 :(得分:0)

就我而言,我进行了两个HTTP请求并获得了两个列表。然后将它们写入两个表。但是首先在子表中,然后在父表中。很奇怪,但是在Android 4.4.2上出现了问题,但在Android 5.0上却没有。