package com.example.myapplication3.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.myapplication3.model.UserInfo;
import java.util.ArrayList;
import java.util.List;
public class DBManager {
private DBHelper helper;
private SQLiteDatabase db;
public DBManager(Context context) {
helper=new DBHelper(context);
db=helper.getWritableDatabase();
}
//注册用户
public void insert(UserInfo info){
ContentValues values=new ContentValues();
values.put("name",info.getName());
values.put("pwd",info.getPwd());
db.insert("user_info",null,values);
}
//查询全部用户名
public List
Cursor cursor=db.query("user_info",null,null,null,null,null,null);
List
if (cursor.getCount()!=0){
cursor.moveToFirst();
do {
UserInfo info=new UserInfo(cursor.getString(1),cursor.getString(2));
lists.add(info);
}while (cursor.moveToNext());
return lists;
}
return lists;
}
//判断数据库中此用户是否已存在
public int find(String name){
Cursor cursor=db.query("user_info",null,"name=?",new String[]{name},null,null,null);
if (cursor.getCount()!=0){
return 1;
}
return 0;
}
//查询单个用户
public UserInfo findUser(String name){
Cursor cursor=db.query("user_info",null,"name=?",new String[]{name},null,null,null);
if (cursor.getCount()!=0){
cursor.moveToFirst();
UserInfo info=new UserInfo();
info.setName(cursor.getString(cursor.getColumnIndex("name")));
info.setPwd(cursor.getString(cursor.getColumnIndex("pwd")));
return info;
}
return null;
}
public void closeDB(){
if (db!=null){
db.close();
}
}
public static Boolean insertLog(Context context, String title, String content, String time) {
Boolean isSuccess = false;
DBHelper dbHelper = new DBHelper(context);
SQLiteDatabase dbWrite = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("title", title);
values.put("time", time);
values.put("content", content);
values.put("state",0);
if (dbWrite.insert("info", null, values) != -1) {
isSuccess = true;
}
dbWrite.close();
dbHelper.close();
return isSuccess;
}