Android ----------数据库的创建与使用(CRUD)

本文用到SQLite数据库一款轻量级数据库
使用是继承于SQLiteOpenHelper抽象类
首先创建一个Helper(自定义)类用于继承SQLiteOpenHelper抽象类
public class Helper extends SQLiteOpenHelper
然后他有三个必须实现的方法
public class Helper extends SQLiteOpenHelper {
public static final String TableName = “ToDoList”;//表名
public static final String ID = “ID”;
public static final String Content = “Content”;
private static final String DataBase = “todolist.db”;//数据库名
private static final SQLiteDatabase.CursorFactory Factory = null;//游标
public static final int version = 1;//版本号

public Helper(@Nullable Context context) {
    super(context, DataBase, Factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
    // 操作数据库,建表
    String sql = "create table " + TableName + "("+ID+" varchar(20) primary key, "+Content+" varchar(20));";
    db.execSQL(sql);
}

//用于新版本和就版本有所差异时,用于更新数据库
//比如新版本需求更大,需要加一个金钱的列进去,然后可以在此函数当作进行添加
//此并没有实现旧与新的迭代
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists "+TableName);
onCreate(db);
}

}
/*分割线/
创一个DAO类用于执行CRUD
public class Dao {
public static final String TAG = “DataBase”;
private Helper helper ;
private SQLiteDatabase DB;
public Dao(Context context)
{
helper = new Helper(context);
}
public void Insert(DataBase dataBase){
DB = helper.getReadableDatabase();
if (DB.isOpen())
{
ContentValues values = new ContentValues();
values.put(Helper.ID,dataBase.getId());
values.put(Helper.Content,dataBase.getContent());
long RowId = DB.insert(Helper.TableName,null,values);
if(RowId == -1)
Log.i(TAG, “数据插入失败!”);
else
Log.i(TAG, “数据插入成功!”+RowId);
}
}
//根据序号删除数据
//当前id为暂不为序号
public void Delete(String id){
DB = helper.getReadableDatabase();
if (DB.isOpen()){
String whereClause = “ID = ?”;
String[] whereArgs = {id + “”};
int count = DB.delete(Helper.TableName, whereClause, whereArgs);
if (count > 0)
Log.i(TAG, "删除了: " + count + “行”);
else
Log.i(“todolist”, “数据未删除!”);
DB.close(); // 数据库关闭
}
}
public void Update(String id ,String Content){
DB = helper.getWritableDatabase();
if(DB.isOpen()) { // 如果数据库打开, 执行添加的操作
ContentValues values = new ContentValues();
values.put(“ID”,id);
values.put(“Content”, Content);
int count = DB.update(Helper.TableName, values, “_ID = ?”, new String[]{id + “”});
if (count > 0)
Log.i(TAG, "修改了: " + count + “行”);
else
Log.i(“todolist”, “数据未删除!”);
DB.close(); // 数据库关闭
}
}
public String Query(String id){
DB = helper.getReadableDatabase();
//selection查询子句的条件,可以使用主键查询
String[] columns = {“ID”, “Content”}; // 需要的列
String selection = “ID = ?”;
String[] selectionArgs = {id + “”};
Cursor cursor = DB.query(Helper.TableName,columns,selection,selectionArgs,null,null,null);
if (cursor.moveToFirst())
{
if(cursor != null && cursor.moveToFirst()) {
//String _id = cursor.getString(0);
String Content = cursor.getString(1);
DB.close();
return Content;
}
cursor.close();
DB.close();
}
return null;
}
public List QueryAll() {
DB = helper.getReadableDatabase(); // 获得一个只读的数据库对象
if(DB.isOpen()) {
String[] columns = {“ID”, “Content”}; // 需要的列
String selection = null; // 选择条件, 给null查询所有
String[] selectionArgs = null; // 选择条件的参数, 会把选择条件中的? 替换成数据中的值
String groupBy = null; // 分组语句 group by name
String having = null; // 过滤语句
String orderBy = null; // 排序
Cursor cursor = DB.query(Helper.TableName, columns, selection, selectionArgs, groupBy, having, orderBy);
String id;
String Content;
if(cursor != null && cursor.getCount() > 0) {
List DataBaseList = new ArrayList();
while(cursor.moveToNext()) { // 向下移一位, 知道最后一位, 不可以往下移动了, 停止.
id = cursor.getString(0);
Content = cursor.getString(1);
DataBaseList.add(new DataBase(id, Content));
}
DB.close();
return DataBaseList;
}
DB.close();
}
return null;
}
}
/*分割线/
然后在需要引用的地方引用就没有了
比如在Mainacticity中
首先实例Dao类
Dao dao = new Dao(MainActivity.this);
dao.QueryAll();//其他功能均可以实现
如果要查看数据库中的内容
可以采用IDEA在sqlite查看
如果有其他软件,先把数据库文件保存到桌面然后使用其他软件查看
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/News53231323/article/details/113480146