gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:19 24 4
gpt4 key购买 nike

我检查了 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 时,它会将我发送到 Profile Activity 。数据库中没有保存信息。我不明白 LogCat 中的错误,我该如何解决?为什么数据库里什么都没有? (我正在使用 SQLite Database Browser 来检查数据。)谢谢

最佳答案

错误 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 是否不为 null 并且您必须为字段 TIME 设置一个值。

编辑

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

关于Android:SQLiteConstraintException:错误代码 19:约束失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20850876/

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