欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SQLite数据库使用

程序员文章站 2022-05-15 23:46:32
...

创建数据库

package com.example.yuekaoone.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MHelp extends SQLiteOpenHelper {
    public MHelp(Context context) {
        super(context, "user.db",null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table users(id Integer,name text , img1 text, img2 text, img3 text)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

**

创建dao包

**

package com.example.yuekaoone.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.yuekaoone.bean.Shu;

import java.util.ArrayList;
import java.util.List;

public class Dao {
    //List<Shu> list = new ArrayList<>();
    SQLiteDatabase database;
    public Dao(Context context) {
        MHelp mHelp = new MHelp(context);
        database = mHelp.getReadableDatabase();
    }

   /* public void addAll(List<Shu> datas) {
        for (int i=0;i<datas.size();i++){
            add(datas.get(i));
        }
    }
*/

   /* public void add(Shu data) {
        database.execSQL("insert into users(name,img1,img2,img3) values(?,?,?,?)",new String[]{data.getName(),data.getImg1(),data.getImg2(),data.getImg3()});

    }*/

    public void add(String name, String img1, String img2, String img3) {

        database.execSQL("insert into users(name,img1,img2,img3) values(?,?,?,?)",new String[]{name,img1,img2,img3});

    }

    public int getDataCount() {
        int count = -1;
        Cursor cursor = database.query("users", new String[]{}, null, null, null, null, null);
        if(cursor.moveToNext()) {
            count = cursor.getCount();
        }
        return count;
    }

    public List<Shu> cha() {
        //
        List<Shu> list = new ArrayList<>();
        Cursor cursor = database.rawQuery("select * from users", null);
        while(cursor.moveToNext()){

            String name = cursor.getString(cursor.getColumnIndex("name"));
            String img1 = cursor.getString(cursor.getColumnIndex("img1"));
            String img2 = cursor.getString(cursor.getColumnIndex("img2"));
            String img3 = cursor.getString(cursor.getColumnIndex("img3"));

            Shu shu =new Shu(name,img1,img2,img3);
            list.add(shu);

        }
        cursor.close();
        return list;

    }

    public void delete() {
        database.execSQL("delete from users");
    }
}

在mainactivity创建dao 引用

     //添加到数据库
                    for (int i=0;i<dataBeans.size();i++){
                        dao.add(dataBeans.get(i).getAuthor_name(),dataBeans.get(i).getThumbnail_pic_s(),dataBeans.get(i).getThumbnail_pic_s02(),dataBeans.get(i).getThumbnail_pic_s03());
                    }
``
//查询

 List<Shu> list11 = dao.cha();
            adapter.setdata(Convers.db2response(list11));`
//两个集合转换

package com.example.yuekaoone.bean;

import java.util.ArrayList;
import java.util.List;

public class Convers {

    //db 的数据 -> 网络请求的
    public static List<Bean.DataBean> db2response(List<Shu> datas) {
        List<Bean.DataBean> result = new ArrayList<>();

        for (Shu shu:datas) {
            result.add(new Bean.DataBean(shu.name, shu.img1, shu.img2, shu.img3));
        }

        return result;
    }


    //网络请求数据 -> db数据
    public static List<Shu> response2db(List<Bean.DataBean> datas) {
        List<Shu> result = new ArrayList<>();

        for (Bean.DataBean dataBean:datas) {
            result.add(new Shu(dataBean.getAuthor_name(), dataBean.getThumbnail_pic_s(),
                    dataBean.getThumbnail_pic_s02(), dataBean.getThumbnail_pic_s03()));
        }

        return result;
    }

}



相关标签: sqlite