gpt4 book ai didi

java - 值未正确插入到 SQLite 数据库中

转载 作者:太空宇宙 更新时间:2023-11-04 09:31:57 25 4
gpt4 key购买 nike

我正在将 SQLite 设置到我的应用程序注册页面,并且我想使用我的手机号码而不是电子邮件进行注册。我的应用程序没有显示错误,但 toast(“注册成功”)从未出现,并且 Activity mainWindow 从未启动。

我的数据库文件如下

public class DatabaseHelp extends SQLiteOpenHelper {
public DatabaseHelp( Context context) {
super(context,"Login.db",null,1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table user(First_NAME text ,Last_NAME text,mobile number primary key ,password text)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists user");
}

//inserting in database
public boolean insert(String First_NAME,String Last_NAME,String mobile,String password){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("Fisrt Name",First_NAME);
contentValues.put("Last Name",Last_NAME);
contentValues.put("Mobile Number",mobile);
contentValues.put("Password",password);

long ins=db.insert("user",null,contentValues);
if(ins==-1) {return false;}
else{ return true;}
}

// if number exists
public Boolean chkemail(String mobile){
SQLiteDatabase db= this.getWritableDatabase();
Cursor cursor=db.rawQuery("Select * from user where mobile=?",new String[]{mobile});
if(cursor.getCount()>0) return false;
else return true;
}
}

我的注册页面 java 文件如下

public class signup extends AppCompatActivity {
DatabaseHelp db;
EditText e1,e2,e3,e4,e5;
Button b1;
public void onClick(View view){
Intent i1 = new Intent(this, forgotpass2.class);
startActivity(i1);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
db=new DatabaseHelp(this);
e1=(EditText)findViewById(R.id.editText5);
e2=(EditText)findViewById(R.id.editText6);
e3=(EditText)findViewById(R.id.editText);
e4=(EditText)findViewById(R.id.editText7);
e5=(EditText)findViewById(R.id.editText8);
b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s1=e1.getText().toString();
String s2=e2.getText().toString();
String s3=e3.getText().toString();
String s4=e4.getText().toString();
String s5=e5.getText().toString();
if(s1.equals("")||s2.equals("")||s3.equals("")||s4.equals("")||s5.equals("")) {
Toast.makeText(getApplicationContext(), "Fields are empty", Toast.LENGTH_SHORT).show();
} else {
if(s4.equals(s5)){
Boolean chkemail=db.chkemail(s3);

if(chkemail == true) {
Boolean insert = db.insert(s1,s2,s3,s4);
if(insert == true) {
Toast.makeText(getApplicationContext(),"Registered Successfully",Toast.LENGTH_SHORT).show();

}
} else {
Toast.makeText(getApplicationContext(),"Mobile Number already exits",Toast.LENGTH_SHORT).show();
}
}
Toast.makeText(getApplicationContext(),"Passwords do not match",Toast.LENGTH_SHORT).show();

}
}
});
}
}

我应该怎样做才能使代码正常工作?

最佳答案

您的代码中有几处需要指出。首先,关于 insert 函数,列名有空格,我认为你应该去掉 SQLite 列名的这些空格。修改您的 insert 函数,如下所示。

public boolean insert(String First_NAME,String Last_NAME,String mobile,String password){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("first_name",First_NAME);
contentValues.put("last_name",Last_NAME);
contentValues.put("mobile",mobile);
contentValues.put("password",password);
long ins=db.insert("user",null,contentValues);

if(ins==-1) return false;
else return true;
}

现在,完成此操作后,我认为您的 chkemail 功能现在可以正常工作了。因为,以前在数据库表中找不到字段 mobile,因为列名称不同(即代码中的列名称是 Mobile Number,我在 insert 函数中对其进行了更改以与查询匹配)。

public boolean chkemail(String mobile){
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("Select * from user where mobile=?",new String[]{mobile});

if(cursor.getCount()>0) return false;
else return true;
}

请注意,我已将 chkemail 函数的返回类型从 Boolean 更改为 boolean

最后,在 signup Activity 中,您还需要修改下面提到的部分。

// Changed from Boolean to boolean
boolean insert = db.insert(s1,s2,s3,s4);

if(insert == true) {
Toast.makeText(getApplicationContext(),"Registered Successfully",Toast.LENGTH_SHORT).show();

// Start the new activity here
Intent newIntent = new Intent(this, mainWindow.class);
startActivity(newIntent);
}

希望有帮助!

关于java - 值未正确插入到 SQLite 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57010312/

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