手机直播源码,一个简单的Android登录实现
手机直播源码,一个简单的Android登录实现
一、实体类
User
package com.example.login.model;
public class User {
public String account;
public String loginKey;
public String mail;
public User() {
}
public User(String account, String loginKey, String mail) {
this.account = account;
this.loginKey = loginKey;
this.mail = mail;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getLoginKey() {
return loginKey;
}
public void setLoginKey(String loginKey) {
this.loginKey = loginKey;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
@Override
public String toString() {
return "User{" +
"account='" + account + '\'' +
", loginKey='" + loginKey + '\'' +
", mail='" + mail + '\'' +
'}';
}
}
二、数据库操作类
MySQLiteOpenHelper
package com.example.login;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
private SQLiteDatabase db; //声明一个数据库变量db
private static final String DB_NAME="mySQLite.db";
private static final String TABLE_NAME="user";
// public MySQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
// super(context, name, factory, version);
//简化后,仅需传入contex(上下文)一个参数即可创建MySQLiteOpenHelper对象
public MySQLiteOpenHelper(@Nullable Context context) {
super(context, DB_NAME, null, 1);
db=getReadableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建数据库SQL语句
String CREATE_TABLE_SQL= "create table "+TABLE_NAME+" (id Integer primary key autoincrement,account text,login_key text,mail text)";
//创建数据库表
db.execSQL(CREATE_TABLE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
//更新需做的操作
db.execSQL("drop table if exists "+TABLE_NAME);
onCreate(db);
}
//插入用户
public void insert(String account,String loginKey,String mail){
String INSERT_USER_SQL="insert into "+TABLE_NAME+" (account,login_key,mail) values(?,?,?)";
db.execSQL(INSERT_USER_SQL,new Object[]{account,loginKey,mail});
}
//根据账号查找密码
public String queryKeyByAcount(String account){
String QUERY_KEY_BYACOUNT_SQL="select login_key from "+TABLE_NAME+" where account=?";
//rawQuery()⽅法的第⼀个参数为select语句;第⼆个参数为select语句中占位符参数的值,如果select语句没有使⽤占位符,该参数可以设置为null。
Cursor cursor=db.rawQuery(QUERY_KEY_BYACOUNT_SQL,new String[]{account});
//因为账号是唯一的,所以查找的结果有且只有一条,直接将游标的指向为first即可
cursor.moveToFirst();
String loginKey=cursor.getString(0);
//关闭游标
cursor.close();
return loginKey;
}
//在数据库中查找账号是否已经存在
public Integer queryCountByAccount(String account){
Cursor cursor=db.rawQuery("select count(*) from "+TABLE_NAME+" where account=?",new String[]{account});
cursor.moveToFirst();
int count=cursor.getInt(0);
cursor.close();
return count;
}
}
以上就是手机直播源码,一个简单的Android登录实现, 更多内容欢迎关注之后的文章