关于Android studio操作SQLite的方法
package com.example.daka1; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import java.util.ArrayList; import java.util.List; /** * 数据库访问对象 * DAO:DATEBASE ACCESS OBJECT * 是控制层于数据库交互的中间层,用于做数据库的增删改查具体实现 * */ public class Thesql { //SQLiteDatabase对象封装了所有SQLite的增删改查语句的操作方法,让开发者直接调用就行 private SQLiteDatabase db; public Thesql(Context context){ //初始化刚刚写的MySQLiteHelper对象 MySQLiteHelper mySQLiteHelper = new MySQLiteHelper(context); //获取sqLiteDatabase对象 db = mySQLiteHelper.getWritableDatabase(); } public boolean adduser(String xuehao,String yonghuming,String shoujihaoma,String banji,String mima) { ContentValues con = new ContentValues(); con.put("xuehao",xuehao); con.put("yonghuming",yonghuming); con.put("shoujihaoma",shoujihaoma); con.put("banji",banji); con.put("mima",mima); con.put("zuichang",0); long i = db.insert("user",null,con); return i>0?true:false; } public boolean xiugai(String xuehao,String yonghuming,String shoujihaoma,String banji,String mima) { ContentValues con = new ContentValues(); con.put("xuehao",xuehao); con.put("yonghuming",yonghuming); con.put("shoujihaoma",shoujihaoma); con.put("banji",banji); con.put("mima",mima); long i = db.update("user",con,"yonghuming=?",new String[]{yonghuming}); return i>0?true:false; } public boolean xiugai(String yonghuming,Userpd userpd) { ContentValues con = new ContentValues(); con.put("xuehao",userpd.xuehao); con.put("yonghuming",userpd.yonghuming); con.put("shoujihaoma",userpd.shoujihaoma); con.put("banji",userpd.banji); con.put("mima",userpd.mima); con.put("zuichang",userpd.zuichang); long i = db.update("user",con,"yonghuming=?",new String[]{yonghuming}); return i>0?true:false; } public Userpd chaxun(String yonghuming) { Cursor cur=db.query("user",null,"yonghuming=?",new String[]{yonghuming},null,null,null); if(cur.getCount()==1) { cur.moveToFirst(); int id= cur.getInt(0); String xuehao=cur.getString(1); //String yonghuming1=cur.getString(2); String shoujihaoma=cur.getString(3); String banji=cur.getString(4); String mima=cur.getString(5); int zuichang= cur.getInt(6); Userpd user1 = new Userpd(); user1.id=id; user1.xuehao=xuehao; user1.yonghuming=yonghuming; user1.shoujihaoma=shoujihaoma; user1.banji=banji; user1.mima=mima; user1.zuichang=zuichang; return user1; } else return null; } public Userpd[] chaxunalluser() { Cursor cur=db.query("user",null,null,null,null,null,null); int n = cur.getCount(); Userpd[] userpds = new Userpd[n]; for(int i=0;i<n;i++) { userpds[i]=new Userpd(); } int i=0; while(cur.moveToNext()) { userpds[i].xuehao=cur.getString(1); userpds[i].yonghuming=cur.getString(2); userpds[i].banji=cur.getString(4); i++; } return userpds; } public boolean ispass (String yonghuming,String password) { Userpd pd = chaxun(yonghuming); if(pd!=null) { if(pd.mima.equals(password)) return true; else return false; } else return false; } public boolean adddakajilu(String riqi,String guanjianzi,String meirizongjie,String user) { ContentValues con = new ContentValues(); con.put("riqi",riqi); con.put("guanjianzi",guanjianzi); con.put("meirizongjie",meirizongjie); con.put("user",user); long i = db.insert("dakajilu",null,con); return i>0?true:false; } public Dakapd dakachaxun(String riqi) { Cursor cur=db.query("dakajilu",null,"riqi=?",new String[]{riqi},null,null,null); if(cur.getCount()==1) { cur.moveToFirst(); int id= cur.getInt(0); //String riqi1=cur.getString(1); String guanjianzi1=cur.getString(2); String meirizongjie1=cur.getString(3); String user1=cur.getString(4); Dakapd dakapd = new Dakapd(); dakapd.id=id; //dakapd.riqi=riqi1; dakapd.guanjianzi=guanjianzi1; dakapd.meirizongjie=meirizongjie1; dakapd.user=user1; return dakapd; } else return null; } public Dakapd dakachaxun(String riqi,String user) { Cursor cur=db.query("dakajilu",null,"riqi=? and user=?",new String[]{riqi,user},null,null,null); if(cur.getCount()==1) { cur.moveToFirst(); int id= cur.getInt(0); //String riqi1=cur.getString(1); String guanjianzi1=cur.getString(2); String meirizongjie1=cur.getString(3); String user1=cur.getString(4); Dakapd dakapd = new Dakapd(); dakapd.id=id; //dakapd.riqi=riqi1; dakapd.guanjianzi=guanjianzi1; dakapd.meirizongjie=meirizongjie1; dakapd.user=user1; return dakapd; } else return null; } public Dakapd[] dakachaxunyonghu(String user) { Cursor cur=db.query("dakajilu",null,"user=?",new String[]{user},null,null,null); if(cur.getCount()!=0&&cur.getCount()!=-1) { int n=Getuserdakacishu(user); Dakapd[] dakapd =new Dakapd[n]; for(int i=0;i<n;i++) { Dakapd dakapd1=new Dakapd(); dakapd[i]=dakapd1; } int i=0; while(cur.moveToNext()) { dakapd[i].riqi=cur.getString(1); dakapd[i].guanjianzi=cur.getString(2); dakapd[i].meirizongjie=cur.getString(3); dakapd[i].user=cur.getString(4); i++; } return dakapd; } else return null; } public Dakapd[] dakachaxunGAndM(String guanjianzi,String meirizongjie) { Cursor cur=db.query("dakajilu",null,"guanjianzi like ? and meirizongjie like ? ",new String[]{"%"+guanjianzi+"%","%"+meirizongjie+"%"},null,null,null); if(cur.getCount()!=0&&cur.getCount()!=-1) { int n=cur.getCount(); Dakapd[] dakapd =new Dakapd[n]; for(int i=0;i<n;i++) { Dakapd dakapd1=new Dakapd(); dakapd[i]=dakapd1; } int i=0; while(cur.moveToNext()) { dakapd[i].riqi=cur.getString(1); dakapd[i].guanjianzi=cur.getString(2); dakapd[i].meirizongjie=cur.getString(3); dakapd[i].user=cur.getString(4); i++; } return dakapd; } else return null; } public String[] Getalluser () { String[] strings=new String[120]; int i=0; Cursor cur=db.query(true,"dakajilu",new String[]{"id","user"},null,null,"user",null,null,null,null); while(cur.moveToNext()) { String user1=cur.getString(1); strings[i]=user1; i++; } return strings; } public int Getuserdakacishu (String user) { Cursor cur=db.query("dakajilu",null,"user=?",new String[]{user},null,null,null); return cur.getCount(); } public List<Userpd> query() { List<Userpd> list = new ArrayList<>(); Cursor cur = db.query("user",null,null,null,null,null,null,null); if(cur!=null) { while(cur.moveToNext()) { Userpd user =new Userpd(); String id = String.valueOf(cur.getInt(0)); list.add(user); } cur.close(); return list; } else return null; } }
标签:总结,cur,yonghuming,con,String,23.3,new,null,21 From: https://www.cnblogs.com/laobing6214/p/17241386.html