Android SQLite数据库连接实现登录功能攻略
1. 创建数据库和表格
首先,我们需要创建一个SQLite数据库和一个用于存储用户信息的表格。可以在SQLiteOpenHelper
类中实现这一步骤。
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = \"UserDB\";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = \"User\";
private static final String COLUMN_ID = \"id\";
private static final String COLUMN_USERNAME = \"username\";
private static final String COLUMN_PASSWORD = \"password\";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTableQuery = \"CREATE TABLE \" + TABLE_NAME + \"(\"
+ COLUMN_ID + \" INTEGER PRIMARY KEY AUTOINCREMENT, \"
+ COLUMN_USERNAME + \" TEXT, \"
+ COLUMN_PASSWORD + \" TEXT)\";
db.execSQL(createTableQuery);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String dropTableQuery = \"DROP TABLE IF EXISTS \" + TABLE_NAME;
db.execSQL(dropTableQuery);
onCreate(db);
}
}
2. 注册用户
在注册用户时,我们需要将用户提供的用户名和密码插入到数据库表格中。
public class MainActivity extends AppCompatActivity {
private EditText usernameEditText;
private EditText passwordEditText;
private Button registerButton;
private DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameEditText = findViewById(R.id.usernameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
registerButton = findViewById(R.id.registerButton);
databaseHelper = new DatabaseHelper(this);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
SQLiteDatabase db = databaseHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DatabaseHelper.COLUMN_USERNAME, username);
values.put(DatabaseHelper.COLUMN_PASSWORD, password);
db.insert(DatabaseHelper.TABLE_NAME, null, values);
Toast.makeText(MainActivity.this, \"User registered successfully\", Toast.LENGTH_SHORT).show();
}
});
}
}
3. 用户登录
在用户登录时,我们需要查询数据库表格,检查提供的用户名和密码是否匹配。
public class LoginActivity extends AppCompatActivity {
private EditText usernameEditText;
private EditText passwordEditText;
private Button loginButton;
private DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameEditText = findViewById(R.id.usernameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
loginButton = findViewById(R.id.loginButton);
databaseHelper = new DatabaseHelper(this);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
SQLiteDatabase db = databaseHelper.getReadableDatabase();
String[] projection = {DatabaseHelper.COLUMN_USERNAME, DatabaseHelper.COLUMN_PASSWORD};
String selection = DatabaseHelper.COLUMN_USERNAME + \" = ? AND \" + DatabaseHelper.COLUMN_PASSWORD + \" = ?\";
String[] selectionArgs = {username, password};
Cursor cursor = db.query(DatabaseHelper.TABLE_NAME, projection, selection, selectionArgs, null, null, null);
if (cursor.moveToFirst()) {
Toast.makeText(LoginActivity.this, \"Login successful\", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, \"Invalid username or password\", Toast.LENGTH_SHORT).show();
}
cursor.close();
}
});
}
}
以上是实现Android SQLite数据库连接并实现登录功能的完整攻略。在注册用户和用户登录的示例中,我们使用了DatabaseHelper
类来创建数据库和表格,并使用SQLiteDatabase
类执行数据库操作。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android SQLite数据库连接实现登录功能 - Python技术站