每日总结 3.12
今天学习了查询的代码:
package com.example.xx.db; import android.annotation.SuppressLint; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import androidx.lifecycle.ViewModelProvider; import java.util.ArrayList; import java.util.List; public class DBManager { private static SQLiteDatabase db; public static void initDB(Context context){ DBOpenHelper dbOpenHelper= new DBOpenHelper(context);
package com.example.xx.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; import com.example.xx.R; public class DBOpenHelper extends SQLiteOpenHelper { public DBOpenHelper(@Nullable Context context) { super(context,"tally.db", null, 1); } //创建数据库的方法只有项目第一次运行时,会被调用 @Override public void onCreate(SQLiteDatabase db) { String sql="create table typetb (id integer primary key autoincrement,typename varchar(10), imageid integer,simageid integer,kind integer)"; db.execSQL(sql); insertType(db); //创建记账表 String sqls="create table accounttb(id integer primary key autoincrement,typename varchar(10),simageid integer,beizhu varchar(80),money float,time varchar(60),year integer,month integer,day integer,kind integer)"; db.execSQL(sqls); } private void insertType(SQLiteDatabase db) { //向表中插入元素 String sql="insert into typetb(typename,imageid,simageid,kind) values(?,?,?,?)"; db.execSQL(sql,new Object[]{"其他", R.mipmap.sandian,R.mipmap.sandian,0 }); db.execSQL(sql,new Object[]{"任务", R.mipmap.renwu,R.mipmap.renwu,1 }); } //数据库版本更新时会调用此方法 @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }
db=dbOpenHelper.getWritableDatabase(); } public static List<TypeBean> getTypelist(int kind){ List<TypeBean> list = new ArrayList<>(); //读取数据库数据 String sql="select * from typetb where kind= "+kind; Cursor cursor = db.rawQuery(sql, null); while (cursor.moveToNext()){ @SuppressLint("Range") String typename= cursor.getString(cursor.getColumnIndex("typename")); @SuppressLint("Range") int imageid=cursor.getInt(cursor.getColumnIndex("imageid")); @SuppressLint("Range") int simageid=cursor.getInt(cursor.getColumnIndex("simageid")); @SuppressLint("Range") int kind1=cursor.getInt(cursor.getColumnIndex("kind")); @SuppressLint("Range") int id=cursor.getInt(cursor.getColumnIndex("id")); TypeBean typeBean= new TypeBean(id,typename,imageid,simageid,kind1); list.add(typeBean); } return list; } public static void insertitemToAccounttb(AccouBean bean){ } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通