安卓简单的操作数据库

安卓本身自带数据,一般用于保存一些数据。操作数据库首先要创建,其次是增删改查。看过诸多网友的介绍,现简单的总结如下:(再次感谢大家的分享)代码奉上:

package com.myapplication.sql;

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

/**
 * Created by lake
 * 此类的功能:第三页的缓存数据的库
 */
public class SQLiteHelper extends SQLiteOpenHelper {

    private final static String DATABASE_NAME = "Library";
    private final static int DATABASE_VERSION = 1;
    private final static String TABLE_NAME = "msg";

    //创建数据库,里面添加了3个参数,分别是:Msgone VARCHAR类型,30长度当然这了可以自定义
    //Msgtwo VARCHAR(20)   Msgthree VARCHAR(30))  NOT NULL不能为空
    String sql = "CREATE TABLE " + TABLE_NAME
            + "(_id INTEGER PRIMARY KEY,"
            + " Msgone VARCHAR(30)  NOT NULL,"
            + " Msgtwo VARCHAR(20),"
            + " Msgthree VARCHAR(30))";
    //构造函数,创建数据库
    public SQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    //建表
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
        db.execSQL(sql);
        onCreate(db);
    }

    //获取游标
    public Cursor select() {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
        return cursor;
    }

    //插入一条记录
    public long insert(String msg1,String msg2,String msg3 ) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("Msgone", msg1);
        cv.put("Msgtwo", msg2);
        cv.put("Msgthree", msg3);
        long row = db.insert(TABLE_NAME, null, cv);
        return row;
    }

    //根据条件查询
    public Cursor query(String[] args) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM "+TABLE_NAME+" WHERE BookName LIKE ?", args);
        return cursor;
    }

    //删除记录
    public void delete(int id) {
        SQLiteDatabase db = this.getWritableDatabase();
        String where ="_id = ?";
        String[] whereValue = { Integer.toString(id) };
        db.delete(TABLE_NAME, where, whereValue);
    }

    //更新记录
    public void update(int id, String msg1,String msg2,String msg3 ) {
        SQLiteDatabase db = this.getWritableDatabase();
        String where = "_id = ?";
        String[] whereValue = { Integer.toString(id) };
        ContentValues cv = new ContentValues();
        cv.put("Msgone", msg1);
        cv.put("Msgtwo", msg2);
        cv.put("Msgthree", msg3);
        db.update(TABLE_NAME, cv, where, whereValue);
    }
}

好,数据库创建完毕,剩下的就是如何使用了

但你网络请求到数据之后,可以定义一个方法:

addRec(数据);
这个方法如果每次网络请求都要调用的话,为了防止数据库里的数据重复,需要根据自己的情况删除数据。切记切记切记!!!

一般来说,这个数据就是一个实体类,我们早addRec的方法里循环它

for (int i = 0; i < movies.size(); i++) {
    //这里只是添加了一个数据,如果需要多个数据,那么在创建数据库表的时候可以增加字段。
    helper.insert(movies.get(i).getThumbnail(), "a", "b");
    //重新加载数据
    cursor.requery();
}

然后我们在没有网络的时候,也需要展示数据,就可以从数据库里获取了

先判断数据库里是否有数据,否则你懂得。

这里我保存了2个数据,所以只取2个

if (cursor.moveToNext()){
    cursor.moveToPosition(0);
    String picone = cursor.getString(1);

    cursor.moveToPosition(1);
    String pictwo = cursor.getString(1);
    //把数据放到实体类里
    movies1.add(new Movies.ResultBean(picone));
    movies1.add(new Movies.ResultBean(pictwo));
    //展示图片,你也可以做别的
    rv.setAdapter(new RVAdapter(getActivity(), movies1));
}

数据库基本方法:

1,获取某行的某个值:其中0表示第几行,默认是从第0行开始,1表示第几个,从第一个开始

cursor.moveToPosition(0);          

cursor.getString(1);

cursor.isClosed();//如果为TRUE表示该游标已关闭

cursor.close();//关闭游标,且释放资源

cursor.getColumnCount();//返回所有列的总数

cursor.getColumnNames();//返回一个字符串数组的列名,即将列名全部返回到一个字符串数组中

cursor.getCount();//返回Cursor中的行数

cursor.moveToFirst();//移动光标到第一行

cursor.moveToLast();//移动光标到最后一行

cursor.moveToNext();//移动光标到下一行

cursor.moveToPrevious();//移动光标到上一行

int columnIndex = 0;

int position = 0;

cursor.getColumnName(columnIndex);//从给定的索引返回列名

cursor.moveToPosition(position);//移动光标到给定位置

2,循环

while(cursor.moveToNext()){

//moveToNext()移动光标到下一行

    HashMap<String,String> HM = new HashMap<String,String>();

    String id = cursor.getString(cursor.getColumnIndex("cityid"));

    String name = cursor.getString(cursor.getColumnIndex("cityname"));

    HM.put("cityid", id);

    HM.put("cityname", name);

    AL.add(HM);

}

3,判读是否为空

if (cursor.moveToFirst() == false)

{

Toast.makeText(mContext, "您的表中无数据!!!", Toast.LENGTH_SHORT).show();

}

发布了95 篇原创文章 · 获赞 17 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_30299243/article/details/94588856
今日推荐