在Android开始的过程中,有时候需要保存大量相似结构的数据,这个时候需要用到数据库,而Google工程师在内部封装了一个轻量级的数据库——SQLite

1.MySql类 创建表格

MySql界面public class MySql extends SQLiteOpenHelper {    public MySql( Context context) {        super(context, "bw.db", null, 1);    }    //创建DB    @Override    public void onCreate(SQLiteDatabase db) {        db.execSQL("create table kk(id integer primary key autoincrement , news_title text , news_summary text , pic_url text )");    }    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {        //更新数据使用    }}

2.Dao层 数据库的操作(增,查…)

Dao层public class Dao {    private Context context;    private final SQLiteDatabase db;    public Dao(Context context) {        this.context = context;        MySql mySql = new MySql(context);        db = mySql.getWritableDatabase();    }    //添加操作    public long insert(String table, String nullColumnHack, ContentValues values){        return db.insert(table, nullColumnHack, values);    }    //查询操作    public Cursor query( String table, String[] columns,                        String selection, String[] selectionArgs, String groupBy,                        String having, String orderBy, String limit){        return db.query( table, columns, selection, selectionArgs,                groupBy, having, orderBy, limit);    }}

3.使用数据库的时候 直接调用Dao层 使用方法就可以了

添加数据

//添加数据库for (int i = 0 ; i 

查询数据库

//查询数据库            Cursor query = dao.query("kk", null, null,                    null, null, null, null);            if (query.moveToFirst()){                do {                    String news_title = query.getString(query.getColumnIndex("news_title"));                    String news_summary = query.getString(query.getColumnIndex("news_summary"));                    String pic_url = query.getString(query.getColumnIndex("pic_url"));                    list.add(new GsonBean.DataBean("",news_title,news_summary,pic_url));                }while (query.moveToNext());            }            //关闭游标            query.close();

更多相关文章

  1. Android(安卓)studio使用zxing扫一扫
  2. Android(安卓)中单选框或复选框点击其中一个,其余取消操作
  3. delphixe10 android操作 打电话,摄像头,定位等
  4. android 在应用中安装其他程序
  5. Android_ActionBar
  6. Android(安卓)4.2 JellyBean Graphic Component -- SurfaceFling
  7. JNI中的本地方法添加调试信息
  8. android ListView常用知识总结
  9. android4.4 添加以太网ethernet方法

随机推荐

  1. 一道看完答案你会觉得很沙雕的「动态规划
  2. 又一套!微软在 GitHub 新发的 Python 视频
  3. 恶意 Python 库偷 SSH 密钥!请自查是否中
  4. 海象操作符!Python 3.8 的新特性汇总
  5. 干货丨前端chart组件展示DolphinDB数据教
  6. 算法面试经常需要你手写的三个排序算法(Py
  7. LeetCode 实战:「图解」K 个一组翻转链表
  8. 植树节,程序猿种的那些树
  9. 两分钟看完一道数学思想的算法题
  10. 这道算法题太简单?你忽略了时间复杂度的要