Java 输入的数据未被存储,且未显示任何错误
我试图创建一个小的android应用程序,它需要登录和注册。我是android应用程序开发新手。请帮我解决这个问题 当我点击注册按钮时,什么都没有发生,没有显示错误,输入的数据也没有存储。我不明白发生了什么事Java 输入的数据未被存储,且未显示任何错误,java,android,sql,Java,Android,Sql,我试图创建一个小的android应用程序,它需要登录和注册。我是android应用程序开发新手。请帮我解决这个问题 当我点击注册按钮时,什么都没有发生,没有显示错误,输入的数据也没有存储。我不明白发生了什么事 package com.example.savepassword; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; imp
package com.example.savepassword;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.net.PasswordAuthentication;
public class Signup_form extends AppCompatActivity {
private EditText Name;
private EditText Email;
private EditText Password;
private EditText Re_Password;
private EditText Mobile_Number;
private EditText Country;
private Button Signup;
DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup_form);
db = new DatabaseHelper(this);
Name = findViewById(R.id.EtName);
Email = findViewById(R.id.Etemail);
Password = findViewById(R.id.Etpassword);
Re_Password = findViewById(R.id.EtRe_password);
Mobile_Number = findViewById(R.id.Etphone);
Country = findViewById(R.id.EtCountry);
Signup = findViewById(R.id.BtnSignup);
Signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = Name.getText().toString();
String email = Email.getText().toString();
String password = Password.getText().toString();
String re_password = Re_Password.getText().toString();
String mobile_number = Mobile_Number.getText().toString();
int mobile_number1 = Integer.parseInt(mobile_number);
String country = Country.getText().toString();
validateEmail(email);
validatePassword(password);
if (name.equals("") || re_password.equals("") || country.equals("") || mobile_number.equals("")) {
Toast.makeText(getApplicationContext(), "Fields cannot be Empty", Toast.LENGTH_SHORT).show();
}
if (password.equals(re_password)) {
Boolean chkemail = db.checkEmail(email);
if (chkemail == true) {
Boolean insert = db.insert(email, password, name, mobile_number1, country);
if (insert == true) {
Toast.makeText(getApplicationContext(), "Registered Successfully", Toast.LENGTH_SHORT).show();
openHomePage();
}
} else {
Toast.makeText(getApplicationContext(), "Email Already Exists", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_SHORT).show();
}
}
});
}
private void openHomePage() {
Intent intent = new Intent(this, Homepage.class);
startActivity(intent);
}
private Boolean validateEmail(String email) {
email = Email.getText().toString().trim();
if (email.isEmpty()) {
Email.setError("Please Enter your Email Id");
return false;
} else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
Email.setError("Please Enter a valid Email Id");
return false;
} else {
Email.setError(null);
return true;
}
}
private Boolean validatePassword(String password) {
password = Password.getText().toString().trim();
if (password.isEmpty()) {
Password.setError("Please Enter your Password");
return false;
} else {
Password.setError(null);
return true;
}
}
}
这是我使用SQL创建的数据库
package com.example.savepassword;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, "Login.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table user(Username text, email text primary key, password text, mobilenumber integer, country text)");
try {
db.execSQL("some invalid SQL");
} catch (SQLException e) {
e.printStackTrace();
Log.e("onCreate","Error creating table: "+e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("drop table if exists user");
}
// inserting in database
public boolean insert(String Username, String email, String password, int mobileno, String country) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("username", Username);
contentValues.put("email", email);
contentValues.put("password", password);
contentValues.put("mobile_number", mobileno);
contentValues.put("country", country);
long ins = db.insert("user", null, contentValues);
if (ins == -1) {
return false;
} else {
return true;
}
}
// Check if email exists
public Boolean checkEmail(String email) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select * from user where email=?", new String[]{email});
if (cursor.getCount() > 0) {
return false;
} else {
return true;
}
}
// Checking the email and password while login
public Boolean checkemailpassword(String email, String password) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select * from user where email=? and password=?", new String[]{email, password});
if (cursor.getCount() > 0) {
return true;
} else {
return false;
}
}
}您使用了错误的类型“name”来创建用户表。您应该更改表创建查询,如下所示:
db.execSQL("Create table user(Username text, email text primary key, password text, mobilenumber integer, country text)");
在onCreate of DatabaseHelper类中创建表时出现问题
db.execSQL("Create table user(Username name, email text primary key, password text, mobilenumber number, country text)");
在上面的代码中,您错误地声明了用户名。名称在db中不是任何类型。用户名的类型应该是text/varchar,您应该将id作为主键而不是电子邮件,因为这不是最佳做法。像这样更改onCreate
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table user(id INTEGER primary key, username TEXT, email TEXT, password TEXT, mobilenumber INTEGER, country TEXT)");
}
然后它应该会工作。正如其他答案中指出的那样,SQL语法无效。随着应用程序和数据库变得越来越复杂,以下捕获错误的模式可能会有所帮助: 是一个RuntimeException,因此编译器不要求您捕获它-但它是在SQL错误时抛出的,在诊断错误时非常有效和有用:
try {
db.execSQL("some invalid SQL");
} catch (SQLException e) {
e.printStackTrace();
Log.e("onCreate","Error creating table: "+e);
}
这里讨论抛出的异常
插入失败,因为mobile_编号不是有效的列名。请参见创建表
和。关闭游标-最终将失败。我认为您希望创建表userUsername,即创建表userUsername text not null,-很可能创建表失败,因为名称不是列类型。在返回状态可用时总是检查它,或者捕获并记录SQLException。我已经更改了它。“注册”按钮仍然不起任何作用。这也是不正确的:contentValues.putmobile\u number,mobileno;-列名与创建表名mobilenumber不匹配(假设您尚未修改)。因此,您的插入失败-这恰好是一条不会生成toast或任何日志记录的路径。最好在android中使用room数据库。请参阅链接,检查注册活动中是否有任何错误。我觉得在注册活动中有些不对劲,我已经改变了。注册按钮仍然不起任何作用。如果已经安装了应用程序,则从设备/模拟器中删除应用程序,并且不会显示tryAny toast。我检查了Login.db数据库。未存储任何内容。请检查注册活动中是否存在任何错误。我觉得在注册活动中有些不对劲。是的,我尝试卸载,但没有发生任何事情