每日总结 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){ } }