Java 如何在SQLite数据库中验证和添加新用户?
我已经创建了一个数据库,并希望注册新用户,以防该用户在数据库中不存在。所以,首先,应用程序检查数据库中是否存在所需的用户名和电子邮件。若并没有,它会将新的用户数据放入数据库。我已经编写了代码,但当我尝试注册新用户时,代码就失效了。 这是我的 MainActivity.javaJava 如何在SQLite数据库中验证和添加新用户?,java,android,database,sqlite,validation,Java,Android,Database,Sqlite,Validation,我已经创建了一个数据库,并希望注册新用户,以防该用户在数据库中不存在。所以,首先,应用程序检查数据库中是否存在所需的用户名和电子邮件。若并没有,它会将新的用户数据放入数据库。我已经编写了代码,但当我尝试注册新用户时,代码就失效了。 这是我的 MainActivity.java public class MainActivity extends AppCompatActivity { DBHelper dbHelper; EditText email, username, pass
public class MainActivity extends AppCompatActivity {
DBHelper dbHelper;
EditText email, username, password, passwordconf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
getSupportActionBar().hide();
dbHelper = new DBHelper(this);
}
public void clickButton2(View view) throws Exception {
SQLiteDatabase database = dbHelper.getWritableDatabase();
email = (EditText) findViewById(R.id.email);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
String userName = username.getText().toString();
String userEmail = email.getText().toString();
if (!ValidateUser(userName, userEmail)) {
ContentValues contentValues = new ContentValues();
contentValues.put(DBHelper.KEY_LOGIN,
username.getText().toString());
contentValues.put(DBHelper.KEY_EMAIL,
email.getText().toString());
contentValues.put(DBHelper.KEY_PASSWORD,
password.getText().toString());
database.insert(DBHelper.TABLE_NAME, null, contentValues);
Toast.makeText(this, "you have registered successfully!",
Toast.LENGTH_SHORT).show();
Intent intent2 = new Intent(Main2Activity.this,
Main3Activity.class);
startActivity(intent2);
}
}
public boolean ValidateUser(String userName, String userEmail) {
SQLiteDatabase database = dbHelper.getWritableDatabase();
Cursor cursor = database.query(DBHelper.TABLE_NAME, null,
DBHelper.KEY_LOGIN + "=? OR " + DBHelper.KEY_EMAIL + "=?",
new String[]{userName, userEmail},
null, null, null);
int i = cursor.getCount();
database.close();
cursor.close();
if(i>0){
return true;
}else{
return false;
}
}
}
我的数据库助手DBHelper.java
public class DBHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "Login_register";
public static final String TABLE_NAME = "users";
public static final String KEY_LOGIN = "login";
public static final String KEY_PASSWORD = "passsword";
public static final String KEY_EMAIL = "email";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + KEY_LOGIN + " TEXT," +
KEY_PASSWORD + " TEXT" + KEY_EMAIL + " TEXT"+")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
我的布局XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rauf.myapplication.Main2Activity"
android:background="@drawable/bii3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp"
android:paddingLeft="60dp"
android:paddingRight="58dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="600dp"
android:layout_weight="2"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="42dp">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:src="@drawable/kamera" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:layout_weight="1"
android:paddingTop="35dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:src="@drawable/message" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:background="#00000000"
android:hint="email address"
android:layout_marginLeft="15dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#4a5a71">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:id="@+id/log"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:src="@drawable/usrusr"/>
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:background="#00000000"
android:hint="username"
android:layout_marginLeft="15dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#4a5a71"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:id="@+id/red"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:src="@drawable/pswrd"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:background="#00000000"
android:hint="password"
android:layout_marginLeft="15dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#4a5a71"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:id="@+id/red2"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:src="@drawable/pswrd"/>
<EditText
android:id="@+id/passwordconf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:background="#00000000"
android:hint="password confirm"
android:layout_marginLeft="15dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#4a5a71">
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_background2"
android:text="SIGN UP"
android:onClick="clickButton2"
android:clickable="true"
android:layout_marginBottom="20dp"
android:textColor="#ffffff"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:onClick="clickFunction2"
android:layout_marginBottom="10dp"
android:text="Already have an Account ?"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
第1期
您省略了创建表sql中分隔键密码和键电子邮件列的逗号
因此,您实际上是在说创建表用户(登录文本、密码文本和电子邮件文本)
也就是说,创建该表时使用了一个名为login的列,其中包含一种文本类型,一个名为password的列,其中包含一种文本电子邮件文本类型,重要的是,没有一个名为email的列
因此,对列email的任何引用都将导致在表中找不到该列
要解决此问题,请执行以下操作:-
db.execSQL(“创建表”+表名+”(“+KEY\u登录名+”文本,”+
按键密码+“文本”+按键电子邮件+“文本+”)代码>
应该是:-
db.execSQL(“创建表”+表名+”(“+KEY\u登录名+”文本,”+
密码+文本+电子邮件+文本“+”)代码>
注意!在进行上述更改后重新运行应用程序之前,您应该执行以下操作之一:-
- 删除应用程序的数据(通过设置)
- 卸载应用程序(通过设置)
- 增加数据库版本号(即将
public static final int Database_Version=1;
更改为public static final int Database_Version=2;
)
问题2
在clickButton2
方法中,您可以通过SQLIteDatabase=dbHelper.getwriteabledatabase()获得一个可写的SQLIteDatabase实例代码>
然后调用ValidateUser
,然后尝试使用相同的SQLiteDatabase实例插入一行
但是,当ValidateUser
方法关闭数据库时,您将得到一个异常java.lang.IllegalStateException:尝试重新打开已关闭的对象:SQLiteDatabase:
您可以通过以下方法之一轻松解决此问题:-
- 从
ValidateUser
方法中删除database.close()
行
- 在插入之前创建/打开SQLiteDatabase的另一个实例,例如在插入之前添加一行,例如
database=dbHelper.getWritableDatabase()代码>
问题3
如果某个用户已经存在并因此被验证,则该用户将保留在MainActivity中
您需要决定/确定用于解决此问题的逻辑。假设它停止运行意味着它崩溃,那么您应该从日志中包括stacktrace。如果您不确定stacktrace是如何和/或什么,这可能会很有用。非常感谢。我已经解决了指定的问题和应用程序的工作,因为我想。我真的很感激。