android中使用数据库Sqlite

1.先创建一个类MyOpenHelper

package zyl.cn.demo1019.db;

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

/**
 * Created by ${zhaoyalan} on 2017/10/23.
 */

public class MyOpenHelper extends SQLiteOpenHelper {
  public static final String CREATE_BOOK = "create table msg(" +
            //primary key 将id列设为主键    autoincrement表示id列是自增长的
            "id integer primary key autoincrement," +
            "code integer," +
            "text text," +
            "url text)";
    private Context mContext;

    public MyOpenHelper(Context context) {
        super(context, "message", null, 1);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
       db.execSQL(CREATE_BOOK);
        Toast.makeText(mContext,"数据库创建成功",Toast.LENGTH_SHORT).show();

    }

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

    }
} 

2.创建类sqliteManager,里面提供数据库的增删改查方法。

package zyl.cn.demo1019.db;

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

import java.util.ArrayList;
import java.util.List;
import zyl.cn.demo1019.Bean.MessageBean;
/**
 * Created by ${zhaoyalan} on 2017/10/23.
 */

public class SqliteManager {
    private String TABLE = "msg";
    //对SqlitHelper的引用
    private MyOpenHelper sqlitHelper = null;
    private  Context context;
    //数据库具体操作类,其中封装insert、update delete、query等操作
    private SQLiteDatabase sqLiteDatabase = null;

    //上下文对象,在外部初始化此类时传入的Context,保证在是在同一环境下的操作    Context context;
    //构造方法
    public SqliteManager(Context context) {
        this.context = context;
        this.sqlitHelper = new MyOpenHelper(context);
    }

    /**
     * 打开数据库链接
     */
    public void openWriteConnection() {
        this.sqLiteDatabase = sqlitHelper.getWritableDatabase();
    }

    public void openReadConnection() {
        this.sqLiteDatabase = sqlitHelper.getWritableDatabase();
    }

    /**
     * 关闭数据库链接
     */
    public void releaseConnection() {
        if (sqLiteDatabase != null) {
            sqLiteDatabase.close();
        }
        if (sqLiteDatabase != null) {
            sqLiteDatabase.close();
        }
    }

    /**
     * 向数据库中添加数据
     * @param bean
     * @return
     */
     public boolean insert( MessageBean bean){
         openWriteConnection();
         ContentValues values = new ContentValues();
        values.put("code",bean.getCode());
         values.put("text",bean.getText());
         values.put("url",bean.getUri());
         long count = sqLiteDatabase.insert(TABLE, null, values);
         releaseConnection();
         return count > 0 ? true : false;

     }

    /**
     * 删除数据库中的数据
     * @param whereClause
     * @param whereArs
     * @return
     */
     public boolean delete(String whereClause, String[] whereArs){
         openWriteConnection();
        /* String sql = "_id = ?";
         String wheres[] = { String.valueOf(_id) };
         db.delete("person", sql, wheres); // 数据库删除 */
         int flage = sqLiteDatabase.delete(TABLE, whereClause, whereArs);
         releaseConnection();
         return flage > 0 ? true : false;
     }
    /**
     * 查找数据库中的所有数据
     * @return
     */
    public List<MessageBean> findAll(){
        List<MessageBean> list = new ArrayList<>();
        openReadConnection();
        Cursor cursor = sqLiteDatabase.query(TABLE, null, null, null, null, null, null);
        while(cursor.moveToNext()){
            MessageBean bean = new MessageBean();
            bean.setCode(cursor.getInt(1));
            bean.setText(cursor.getString(2));
            bean.setUri(cursor.getString(3));
            list.add(bean);
        }
        cursor.close();
        releaseConnection();
        return list;
    }
    /**
     * 刷新数据库
     * @param values
     * @param whereClause
     * @param whereArs
     * @return
     */
    public boolean updata(ContentValues values,String whereClause, String[] whereArs){
        openWriteConnection();
        int flage = sqLiteDatabase.update(TABLE, values, whereClause, whereArs);
        releaseConnection();
        return flage > 0 ? true : false;
    }
/**
 * 分页查询
 *
 * @param currentPage 当前页
 * @param pageSize 每页显示的记录
 * @return 当前页的记录
 */
public ArrayList<MessageBean> getAllItems(int currentPage, int pageSize) {
    int firstResult = (currentPage - 1) * pageSize;
    //    (currentPage-)
    openWriteConnection();
    //  select * from table limit (start-1)*limit,limit; 其中start是页码,limit是每页显示的条数。
    String sql = "select * from "+TABLE+" limit ?,?";
    Cursor cursor = sqLiteDatabase.rawQuery(
            sql,
            new String[] { String.valueOf(firstResult),
                    String.valueOf(pageSize) });
    ArrayList<MessageBean> items = new ArrayList<MessageBean>();
    int columnCount  = cursor.getColumnCount();
    //sqLiteDatabase.
    while (cursor.moveToNext()) {
        MessageBean bean = new MessageBean();
        bean.setCode(cursor.getInt(1));
        bean.setText(cursor.getString(2));
        bean.setUrl(cursor.getString(3));
        items.add(bean);
    }
    Log.e("order: ","items====  "+items.size());

    //不要关闭数据库
    return items;
}
 }

3.bean类MessageBean

package zyl.cn.demo1019.Bean;

/**
 * Created by ${zhaoyalan} on 2017/10/20.
 */

public class MessageBean {
    public int code;
    public String text;
    public String uri;

    public MessageBean(int code, String text, String uri) {
        this.code = code;
        this.text = text;
        this.uri = uri;
    }

    public int getCode() {

        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public MessageBean() {
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public MessageBean(int code, String text) {
        this.code = code;
        this.text = text;

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值