1,创建DBHelper
package cn.net.outgoing.db.dbhepler;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class qyDBHelper extends SQLiteOpenHelper {
/**
* VERSION = 1:初始建表结构
*/
private static final int VERSION = 1;// 提交状态增加一个生产功能
/**
* 数据库名字
*/
public final static String db_name = "Student.db";
/**
* 学生单
*/
public static final String tb_student = "student";
public qyDBHelper(Context context) {
super(context, db_name, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
createProduct(db);//创建学生表
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
/**
* name:姓名
* sex:性别
* age:年龄
* phone:手机号
* city:城市
* @param db
*/
private void createProduct(SQLiteDatabase db) {
String sql = "CREATE TABLE " + tb_student
+ " (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20),sex INTEGER,age INTEGER ,phone VARCHAR(20),city VARCHAR(20))";
db.execSQL(sql);
}
}
2,创建操作数据库的方法接口
package cn.net.outgoing.db.inter;
import java.util.List;
import cn.net.outgoing.bean.StudentBean;
/**
* @author zj
* @date 2018/5/7 9:48
* @desc
*/
public interface baseStudentInfoDal {
/**
* 插入学生信息
* @param orderBean
* @return
*/
public void insertStudentInfo(StudentBean orderBean);
/**
* 删除全部
* @return
*/
public void deleteStudentInfo();
/**
* 根据名称删除
* @return
*/
public void deleteStudentInfoByName(String name);
/**
* 修改时检测数据库中是否存在该单标
* @param orderBean
* @return
*/
public void updateStudentInfo(StudentBean orderBean);
/**
* 查询全部学生信息
* @return
*/
public List<StudentBean> queryStudentInfoToAll();
/**
* 根据姓名查询学生信息
* @return
*/
public List<StudentBean> queryStudentInfoByName(String name);
/**
* 插入时检测是否数据库中是否存在
* @param
* @return
*/
public boolean isHasExist(String account);
}
3.创建数据库操作类
package cn.net.outgoing.db.dal;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
import cn.net.outgoing.bean.LabelBean;
import cn.net.outgoing.bean.OrderBean;
import cn.net.outgoing.bean.StudentBean;
import cn.net.outgoing.db.dbhepler.FyswDBHelper;
import cn.net.outgoing.db.dbhepler.qyDBHelper;
import cn.net.outgoing.db.inter.baseProductInfoDal;
import cn.net.outgoing.db.inter.baseStudentInfoDal;
/**
* @author zj
* @date 2018/5/7 9:47
* @desc
*/
public class StudentDal implements baseStudentInfoDal {
private SQLiteDatabase db;
private qyDBHelper helper;
public StudentDal(Context context) {
helper = new qyDBHelper(context);
}
@Override
public void insertStudentInfo(StudentBean studentBean) {
db = helper.getWritableDatabase();
String sql = "insert into " + qyDBHelper.tb_student
+ " (name,sex,age,phone,city) values(?,?,?,?,?)";
db.execSQL(sql, new Object[] { studentBean.getName(),studentBean.getSex(),studentBean.getAge(),studentBean.getPhone(),studentBean.getCity()});
closeDB(db, null);
}
@Override
public void deleteStudentInfo() {
db = helper.getReadableDatabase();
String sql = "delete from " + qyDBHelper.tb_student;
db.execSQL(sql);
closeDB(db, null);
}
@Override
public void deleteStudentInfoByName(String name) {
db = helper.getReadableDatabase();
String sql = "delete from " + qyDBHelper.tb_student
+ " where name=?";
db.execSQL(sql, new Object[] { name });
closeDB(db, null);
}
@Override
public void updateStudentInfo(StudentBean studentBean) {
db = helper.getWritableDatabase();
String sql = "update " + qyDBHelper.tb_student
+ " set name=?,sex=?,age=?,phone=?,city=?";
db.execSQL(sql,
new Object[] { studentBean.getName(),studentBean.getSex(),studentBean.getAge(),studentBean.getCity() });
closeDB(db, null);
}
@Override
public List<StudentBean> queryStudentInfoToAll() {
db = helper.getReadableDatabase();
List<StudentBean> beans = new ArrayList<>();
String sql = "select * from " + qyDBHelper.tb_student+" order by id desc;";//倒序排序
Cursor cursor = db.rawQuery(sql,null);
while (cursor.moveToNext()) {
beans.add(getStudentList(cursor));
}
closeDB(db, cursor);
return beans;
}
@Override
public List<StudentBean> queryStudentInfoByName(String name) {
db = helper.getReadableDatabase();
List<StudentBean> beans = new ArrayList<>();
String sql = "select * from " + qyDBHelper.tb_student+" where account=? ";
Cursor cursor = db.rawQuery(sql,new String[] { name });
while (cursor.moveToNext()) {
beans.add(getStudentList(cursor));
}
closeDB(db, cursor);
return beans;
}
@Override
public boolean isHasExist(String name) {
db = helper.getReadableDatabase();
boolean isExist = false;
String sql = "select count(*) as count from " + qyDBHelper.tb_student + " where name=? ";
Cursor cursor = db.rawQuery(sql, new String[] { name });
if (cursor.moveToNext()) {
int count = cursor.getInt(0);
if (count > 0) {
isExist = true;
}
}
closeDB(db, cursor);
return isExist;
}
private void closeDB(SQLiteDatabase db, Cursor cursor) {
if (cursor != null) {
cursor.close();
}
if (db != null) {
db.close();
}
}
private StudentBean getStudentList(Cursor cursor) {
StudentBean bean = new StudentBean();
bean.setName(cursor.getString(cursor.getColumnIndex("name")));
bean.setSex(cursor.getString(cursor.getColumnIndex("sex")));
bean.setAge(cursor.getInt(cursor.getColumnIndex("age")));
bean.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
bean.setCity(cursor.getString(cursor.getColumnIndex("city")));
return bean;
}
}
4.调用
StudentDal studentDal = new StudentDal(this);
//增加一条
StudentBean bean = new StudentBean();
bean.setName("唐三");
bean.setAge(12);
bean.setSex("男");
bean.setCity("诺丁城");
bean.setPhone("18888888888");
studentDal.insertStudentInfo(bean);
//删出全部
studentDal.deleteStudentInfo();
//删除指定学生
studentDal.deleteStudentInfoByName("唐三");
//修改
bean.setName("唐三");
bean.setAge(18);
bean.setSex("男");
bean.setCity("史莱克");
bean.setPhone("18888888888");
studentDal.updateStudentInfo(bean);
//查
studentDal.queryStudentInfoToAll();//查询全部学生
studentDal.queryStudentInfoByName("唐三");//查询表里所有叫唐三的学生