今天又系统学习了安卓开发数据库Sqlite的相关知识,相关api操作数据库的具体写法:
学习视频https://www.bilibili.com/video/BV1wt411n7pz?from=search&seid=5602890916190660679
按照up主的视频一步步掌握其使用方法
package com.example.myapplicationgb; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class DatabaseHelper extends SQLiteOpenHelper { public static final String COST_MONEY = "cost_money"; public static final String COST_DATE = "cost_date"; public static final String COST_TITLE = "cost_title"; public static final String FAMILY_BILL = "family_bill"; public DatabaseHelper(@Nullable Context context) { super(context, "family_bill", null, 2); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table if not exists family_bill(" + "id integer primary key, " + "cost_title varchar, "+ "cost_date varchar, "+ "cost_money varchar)"); } public void insertCost(CostBean costBean){ SQLiteDatabase database = getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put(COST_TITLE,costBean.costTitle); cv.put(COST_DATE,costBean.costDate); cv.put(COST_MONEY,costBean.costMoney); database.insert(FAMILY_BILL,null,cv); } public Cursor getAllCostData(){ SQLiteDatabase database = getWritableDatabase(); return database.query(FAMILY_BILL,null,null,null,null,null,"COST_DATE ASC"); } public void deleteOne(CostBean costBean) { SQLiteDatabase database=getWritableDatabase(); database.delete(FAMILY_BILL,"COST_TITLE = ? and COST_MONEY = ? and COST_DATE = ?", new String[]{""+costBean.costTitle,""+costBean.costMoney,""+costBean.costDate}); } public Cursor selectlist(String s){ SQLiteDatabase database =getWritableDatabase(); // String sql = "select * from "+ FAMILY_BILL +"where cost_date = '%"+s+"%' "; // database.execSQL(sql); // database.close(); return database.query(FAMILY_BILL,null,"cost_date LIKE ?",new String[]{"%"+s+"%"},null,null,"COST_DATE ASC"); } public void deleteAllData(){ SQLiteDatabase database = getWritableDatabase(); database.delete(FAMILY_BILL,null,null); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
我在团队中的任务是编写可视化显示数据。