有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在SQLite数据库中验证和添加新用户?

我已经创建了一个数据库,并希望注册新用户,以防该用户在数据库中不存在。所以,首先,应用程序检查数据库中是否存在所需的用户名和电子邮件。若并没有,它会将新的用户数据放入数据库。我已经编写了代码,但当我尝试注册新用户时,代码就失效了。 这是我的 主要活动。爪哇

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:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="com.example.rauf.myapplication.Main2Activity"
安卓:background="@drawable/bii3">
<LinearLayout
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:orientation="vertical"
    tools:layout_editor_absoluteY="8dp"
    tools:layout_editor_absoluteX="8dp"
    安卓:paddingLeft="60dp"
    安卓:paddingRight="58dp">
    <LinearLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="600dp"
        安卓:layout_weight="2"
        安卓:gravity="center"
        安卓:orientation="vertical"
        安卓:paddingTop="42dp">
        <ImageView
            安卓:layout_width="150dp"
            安卓:layout_height="150dp"
            安卓:layout_gravity="center"
            安卓:src="@drawable/kamera" />
    </LinearLayout>
    <LinearLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:gravity="center"
        安卓:orientation="vertical"
        安卓:layout_weight="1"
        安卓:paddingTop="35dp">
        <LinearLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:orientation="horizontal"
            安卓:padding="16dp">
            <ImageView
                安卓:id="@+id/imageView"
                安卓:layout_width="20dp"
                安卓:layout_height="20dp"
                安卓:layout_gravity="center"
                安卓:src="@drawable/message" />
            <EditText
                安卓:id="@+id/email"
                安卓:layout_width="match_parent"
                安卓:layout_height="wrap_content"
                安卓:inputType="textEmailAddress"
                安卓:background="#00000000"
                安卓:hint="email address"
                安卓:layout_marginLeft="15dp"/>
        </LinearLayout>
        <LinearLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="1dp"
            安卓:background="#4a5a71">
    </LinearLayout>
        <LinearLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:orientation="horizontal"
            安卓:padding="16dp">
            <ImageView
                安卓:id="@+id/log"
                安卓:layout_width="20dp"
                安卓:layout_height="20dp"
                安卓:layout_gravity="center"
                安卓:src="@drawable/usrusr"/>
            <EditText
                安卓:id="@+id/username"
                安卓:layout_width="match_parent"
                安卓:layout_height="wrap_content"
                安卓:inputType="text"
                安卓:background="#00000000"
                安卓:hint="username"
                安卓:layout_marginLeft="15dp"/>
        </LinearLayout>
        <LinearLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="1dp"
            安卓:background="#4a5a71"></LinearLayout>
        <LinearLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:orientation="horizontal"
            安卓:padding="16dp">
            <ImageView
                安卓:id="@+id/red"
                安卓:layout_width="20dp"
                安卓:layout_height="20dp"
                安卓:layout_gravity="center"
                安卓:src="@drawable/pswrd"/>
            <EditText
                安卓:id="@+id/password"
                安卓:layout_width="match_parent"
                安卓:layout_height="wrap_content"
                安卓:inputType="textPassword"
                安卓:background="#00000000"
                安卓:hint="password"
                安卓:layout_marginLeft="15dp"/>
        </LinearLayout>
        <LinearLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="1dp"
            安卓:background="#4a5a71"></LinearLayout>
        <LinearLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:orientation="horizontal"
            安卓:padding="16dp">
            <ImageView
                安卓:id="@+id/red2"
                安卓:layout_width="20dp"
                安卓:layout_height="20dp"
                安卓:layout_gravity="center"
                安卓:src="@drawable/pswrd"/>
            <EditText
                安卓:id="@+id/passwordconf"
                安卓:layout_width="match_parent"
                安卓:layout_height="wrap_content"
                安卓:inputType="textPassword"
                安卓:background="#00000000"
                安卓:hint="password confirm"
                安卓:layout_marginLeft="15dp"/>
        </LinearLayout>
        <LinearLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="1dp"
            安卓:background="#4a5a71">
        </LinearLayout>
    </LinearLayout>
    <Button
        安卓:id="@+id/button1"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:background="@drawable/button_background2"
        安卓:text="SIGN UP"
        安卓:onClick="clickButton2"
        安卓:clickable="true"
        安卓:layout_marginBottom="20dp"
        安卓:textColor="#ffffff"/>
    <TextView
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_marginTop="20dp"
        安卓:gravity="center"
        安卓:onClick="clickFunction2"
        安卓:layout_marginBottom="10dp"
        安卓:text="Already have an Account ?"
        安卓:textColor="#000000" />
    </LinearLayout>
</LinearLayout>

共 (1) 个答案

  1. # 1 楼答案

    问题1

    您省略了创建表sql中分隔密钥密码密钥电子邮件列的逗号

    因此,你实际上是在说CREATE TABLE users(login TEXT,passsword TEXTemail TEXT)

    也就是说,创建该表时使用了一个名为login的列,其中包含一种文本类型,一个名为password的列,其中包含一种文本电子邮件文本类型,重要的是,没有一个名为email的列

    因此,任何对电子邮件列的引用都会导致在表中找不到该列

    要解决此问题,请执行以下操作:

    db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + KEY_LOGIN + " TEXT," + KEY_PASSWORD + " TEXT" + KEY_EMAIL + " TEXT"+")");

    应为:-

    db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + KEY_LOGIN + " TEXT," + KEY_PASSWORD + " TEXT," + KEY_EMAIL + " TEXT"+")");

    注意!在进行上述更改后重新运行应用程序之前,您应该执行以下操作之一:-

    • 删除应用程序的数据(通过设置)
    • 卸载应用程序(通过设置)
    • 增加数据库版本号(即将public static final int DATABASE_VERSION = 1;更改为public static final int DATABASE_VERSION = 2;

    问题2

    clickButton2方法中,通过SQLiteDatabase database = dbHelper.getWritableDatabase();获得一个可写的SQLIteDatabase实例

    然后调用ValidateUser,然后尝试使用相同的SQLiteDatabase实例插入一行

    然而,当ValidateUser方法关闭数据库时,您将得到一个异常java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase:

    您可以通过以下方法之一轻松解决此问题:-

    • ValidateUser方法中删除database.close()
    • 在插入之前创建/打开SQLiteDatabase的另一个实例,例如在插入之前添加一行,例如database = dbHelper.getWritableDatabase();

    问题3

    如果某个用户已经存在并因此被验证,则该用户将保留在MainActivity中

    您需要决定/确定用于解决此问题的逻辑