我创建了一个数据库,并希望在数据库中缺少该用户的情况下注册新用户。因此,首先,应用程序检查数据库中是否有所需的用户名和电子邮件退出。如果没有,它会将新用户数据放入数据库。我已经编写了代码,但是当我尝试注册新用户时,它会出现问题。 这是我的 MainActivity.java
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>
答案 0 :(得分:1)
您已省略逗号,用于分隔 CREATE TABLE sql中的 KEY_PASSWORD 和 KEY_EMAIL 列。
因此,您实际上是在说CREATE TABLE users(login TEXT,passsword TEXTemail TEXT)
表格是使用名为登录的列创建的,其中包含TEXT类型和名为密码的列,其类型为Textemail TEXT,重要的是没有列名为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;
)在clickButton2
方法中,您可以通过SQLiteDatabase database = dbHelper.getWritableDatabase();
获得可写的SQLIteDatabase实例
然后,您调用ValidateUser
,然后尝试使用相同的SQLiteDatabase实例插入一行。
但是,当ValidateUser
方法关闭数据库时,您将获得异常java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase:
您可以通过以下方式之一轻松解决此问题: -
database.close()
方法ValidateUser
行
database = dbHelper.getWritableDatabase();
如果用户已经存在且经过验证,则他们将保留在MainActivity中。
您需要决定/确定用于解决此问题的逻辑。