Android studio连接sqlite实现登录注册的代码的详细讲解
时间: 2024-05-05 10:16:47 浏览: 110
Android Studio连接SQLite数据库的登录注册实现
5星 · 资源好评率100%
首先,建立一个Android Studio项目,然后在项目的build.gradle中添加以下依赖项:
```
dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'com.squareup.retrofit2:retrofit:2.6.1'
implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
}
```
然后,创建一个SQLiteOpenHelper类来创建和管理数据库。在此类中,您可以定义数据库名称,表名称和列名称,以及实现创建表和升级表的方法。
```java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = DatabaseHelper.class.getSimpleName();
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "users.db";
private static final String TABLE_USERS = "users";
private static final String COLUMN_USER_ID = "_id";
private static final String COLUMN_USER_NAME = "name";
private static final String COLUMN_USER_EMAIL = "email";
private static final String COLUMN_USER_PASSWORD = "password";
private static final String CREATE_TABLE_USERS = "CREATE TABLE " + TABLE_USERS + "("
+ COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_USER_NAME + " TEXT,"
+ COLUMN_USER_EMAIL + " TEXT,"
+ COLUMN_USER_PASSWORD + " TEXT"
+ ")";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_USERS);
Log.d(TAG, "Database tables created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
onCreate(db);
}
public long addUser(String name, String email, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_USER_NAME, name);
values.put(COLUMN_USER_EMAIL, email);
values.put(COLUMN_USER_PASSWORD, password);
long id = db.insert(TABLE_USERS, null, values);
db.close();
Log.d(TAG, "New user inserted into database: " + id);
return id;
}
public boolean checkUser(String email, String password) {
String[] columns = { COLUMN_USER_ID };
SQLiteDatabase db = this.getReadableDatabase();
String selection = COLUMN_USER_EMAIL + " = ?" + " AND " + COLUMN_USER_PASSWORD + " = ?";
String[] selectionArgs = { email, password };
Cursor cursor = db.query(TABLE_USERS,
columns,
selection,
selectionArgs,
null,
null,
null);
int cursorCount = cursor.getCount();
cursor.close();
db.close();
if (cursorCount > 0) {
return true;
}
return false;
}
}
```
在这个类中,我们定义了一个名为`users.db`的数据库,以及一个名为`users`的表。表中有4列:`_id`,`name`,`email`和`password`。我们还实现了两个方法:`addUser()`和`checkUser()`。`addUser()`方法用于将新用户添加到数据库中,`checkUser()`方法用于验证用户是否存在于数据库中。
接下来,我们将在MainActivity中实现登录和注册功能。我们在`onCreate()`方法中初始化数据库助手,然后在登录按钮上设置一个点击监听器,该监听器获取输入的电子邮件和密码,然后使用`checkUser()`方法验证用户是否存在于数据库中。
```java
public class MainActivity extends AppCompatActivity {
private EditText mEmailEditText;
private EditText mPasswordEditText;
private Button mLoginButton;
private Button mRegisterButton;
private DatabaseHelper mDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabaseHelper = new DatabaseHelper(this);
mEmailEditText = findViewById(R.id.email_edit_text);
mPasswordEditText = findViewById(R.id.password_edit_text);
mLoginButton = findViewById(R.id.login_button);
mRegisterButton = findViewById(R.id.register_button);
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = mEmailEditText.getText().toString();
String password = mPasswordEditText.getText().toString();
if (mDatabaseHelper.checkUser(email, password)) {
Toast.makeText(getApplicationContext(), "Login successful", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Invalid email or password", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onDestroy() {
mDatabaseHelper.close();
super.onDestroy();
}
}
```
接下来,我们将在MainActivity中实现注册功能。我们在“注册”按钮上设置一个点击监听器,该监听器获取输入的名称,电子邮件和密码,然后使用`addUser()`方法将新用户添加到数据库中。
```java
mRegisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = mNameEditText.getText().toString();
String email = mEmailEditText.getText().toString();
String password = mPasswordEditText.getText().toString();
long id = mDatabaseHelper.addUser(name, email, password);
if (id > 0) {
Toast.makeText(getApplicationContext(), "Registration successful", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Registration failed", Toast.LENGTH_SHORT).show();
}
}
});
```
这就是连接SQLite实现登录和注册的代码的详细讲解。
阅读全文