package com.example.environmentmonitor.entity;
/**
* 用户实体类
*/
public class User {
private String userId;
//用户账号
private String username;
//用户密码
private String password;
public User(String userId,String username, String password) {
this.userId=userId;
this.username=username;
this.password=password;
}
public User() {
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.example.environmentmonitor.database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import androidx.annotation.Nullable;
/**
* SQLiteOpenHelper类介绍
* 定义:SQLiteOpenHelper是一个辅助类
* 作用:管理数据库(创建、增、修、删) & 版本的控制。
*/
public class DatabaseOpenHelper extends SQLiteOpenHelper {
/**
* 构造方法
* @param context 上下文:运行环境
* @ name 数据库名
* @ factory 游标工厂
* @ version 版本号
*/
public DatabaseOpenHelper(@Nullable Context context) {
super(context, DatabaseConstant.DATABASE_MY_APP, null, DatabaseConstant.VERSION_CODE);
Log.d(DatabaseConstant.TAG,"创建数据库");
}
//数据库已经存在时,方法是不会被调用的
/**
* 创建数据库时回调
* (第一次创建数据库时被调用)
* @param sqLiteDatabase
*/
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
Log.d(DatabaseConstant.TAG,"创建数据库时回调");
/*
创建数据库表:t_user
sql语句:
create table t_user (
user_Id int(255) not null AUTO_INCREMENT autoincrement,
username varchar(100) not null,
password varchar(100) not null
)
*/
/**
* id 主键
* username 用户名
* password 用户密码
*/
String userSql="CREATE TABLE "+DatabaseConstant.T_USER+" (\n" +
" user_id VARCHAR(50) not null primary key,\n" +
" username VARCHAR(100) not null,\n" +
" password VARCHAR(100) not null)";
sqLiteDatabase.execSQL(userSql);
}
/**
* 升级数据库时回调
* 至少要升级1才会执行
* @param sqLiteDatabase
* @param i
* @param i1
*/
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
Log.d(DatabaseConstant.TAG,"升级数据库时回调");
}
}
package com.example.environmentmonitor.database;
/**
* 数据库名常量类
*/
public interface DatabaseConstant {
/**
* 环境监测系统数据库
*/
//数据库名称
String DATABASE_MY_APP="monitor_system.db";
//数据库版本号
int VERSION_CODE= 1;
//log常量
String TAG="DatabaseOpenHelper";
//数据库表名
String T_USER="t_user";
}
package com.example.environmentmonitor.dao;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.environmentmonitor.database.DatabaseConstant;
import com.example.environmentmonitor.database.DatabaseOpenHelper;
import com.example.environmentmonitor.entity.User;
import java.sql.PreparedStatement;
import java.util.UUID;
/**
* 操作数据库的增删改查
*/
public class UserDAO {
//把数据库命名成为成员变量
private final DatabaseOpenHelper databaseOpenHelper;
/**
* 在构造方法中创建数据库
*/
public UserDAO(Context context){
/*
创建一个数据库程序的入口
*/
databaseOpenHelper=new DatabaseOpenHelper(context);
//创建数据库
databaseOpenHelper.getWritableDatabase();
}
/**
* 增
*/
public void insert(User user){
SQLiteDatabase sqLiteDatabase=databaseOpenHelper.getWritableDatabase();
//插入t_user数据语句
/*
注意单引号
"INSERT INTO "+DatabaseConstant.T_USER+" (user_id,username,password)" +
"VALUES ('"+user.getUserId()+"','"+user.getUsername()+"','"+user.getPassword()+"')";
*/
String insertSql="INSERT INTO "+ DatabaseConstant.T_USER+" (user_id,username,password)" +
"VALUES ('"+user.getUserId()+"','"+user.getUsername()+"','"+user.getPassword()+"')";
sqLiteDatabase.execSQL(insertSql);
sqLiteDatabase.close();
}
/**
* 验证账号、密码
*/
public User getUserByUsernameAndPassword(String username,String password){
SQLiteDatabase sqLiteDatabase=databaseOpenHelper.getWritableDatabase();
/*SQL
SELECT username,password FROM t_user WHERE username='' AND password=''
*/
String sql="SELECT username,password FROM t_user WHERE username='"+username+"' AND password='"+password+"'";
Cursor cursor=sqLiteDatabase.rawQuery(sql,null);
//定义接收对象
User user=new User();
if(cursor != null && cursor.getCount() > 0){
cursor.moveToFirst();
user.setUsername(cursor.getString(0));
user.setPassword(cursor.getString(1));
sqLiteDatabase.close();
}else {
user=null;
}
return user;
}
/**
* 根据userId查询用户
* cursor就是一个Cursor对象,这个cursor是一个实现了迭代器(def iter())和生成器(yield)的MySQLdb对象,
* cursor.moveToFirst()指向查询结果的第一个位置。
* 一般通过判断cursor.moveToFirst()的值为true或false来确定查询结果是否为空。cursor.moveToNext()是用来做循环的,一般这样来用:while(cursor.moveToNext()){ }
* cursor.moveToPrevious()是指向当前记录的上一个记录,是和moveToNext相对应的;
* cursor.moveToLast()指向查询结果的最后一条记录
*/
public User getUserByUsername(String username){
SQLiteDatabase sqLiteDatabase=databaseOpenHelper.getWritableDatabase();
/*查询SQL:
SELECT user_id AS userId,username,password FROM t_user WHERE username = ''
*/
String sql="SELECT user_id,username,password FROM t_user WHERE username = '"+username+"'";
Cursor cursor=sqLiteDatabase.rawQuery(sql,null);
User user=new User();
if (cursor != null && cursor.getCount() > 0)
{
cursor.moveToFirst();
// columnIndex代表列的索引
user.setUserId(cursor.getString(0));
user.setUsername(cursor.getString(1));
user.setPassword(cursor.getString(2));
sqLiteDatabase.close();
}else {
user=null;
}
return user;
}
/**
* 修改用户密码
* @return
*/
public void modifyUserPassword(User user){
SQLiteDatabase sqLiteDatabase=databaseOpenHelper.getWritableDatabase();
/* SQL语句:
UPDATE t_user SET password='' WHERE user_id=''
*/
String sql="UPDATE t_user SET password='"+user.getPassword()+"' WHERE username='"+user.getUsername()+"'";
sqLiteDatabase.execSQL(sql);
sqLiteDatabase.close();
}
/**
* 删除用户密码
* @return
*/
public void deleteUserByUsername(String username){
SQLiteDatabase sqLiteDatabase=databaseOpenHelper.getWritableDatabase();
/* SQL语句:
UPDATE t_user SET password='' WHERE user_id=''
*/
String sql="DELETE FROM t_user WHERE username="+username;
sqLiteDatabase.execSQL(sql);
sqLiteDatabase.close();
}
}
https://download.csdn.net/download/Tian208/85130863