实现sqlite数据库保存数据
发布时间:2020-12-12 20:04:01 所属栏目:百科 来源:网络整理
导读:首先是一个DB工具类 package com.shiyou.lxbd.db;import java.io.File;import java.io.IOException;import com.shiyou.lxbd.utils.Utils;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;/** * 数据库类 最近观看 * @author
首先是一个DB工具类package com.shiyou.lxbd.db; import java.io.File; import java.io.IOException; import com.shiyou.lxbd.utils.Utils; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; /** * 数据库类 最近观看 * @author Administrator * */ public class RecentlyWatchDB { private static final String DB_NAME = "lxbd.db";// 数据库名 public static final String STUDENT_TABLE = "lx";// 表名 public static final String _ID = "_id";// id public static final String MID = "mid";// id public static final String TITLE = "title";// 标题 public static final String PLOGO = "plogo";// 图片地址 public static final String LINKS = "links"; // 视频链接 public static final String DURATION = "duration"; // 时长 public static final String INFO = "info"; // 简介 public static final String SCORE = "score"; // 评分 public static final String VIP = "vip"; // 是否vip标识 public static final String TIME = "time";// 时间 //创建表的sql private static final String CREATE_TABLE = "create table " + STUDENT_TABLE + " ( " + _ID + " Integer primary key autoincrement," + MID + " text," + TITLE + " text," + PLOGO + " text," + LINKS + " text," + DURATION + " text," + INFO + " text," + SCORE + " Integer," + VIP + " text," + TIME + " text)"; //判断表是否存在系统数据库中的sql private static String sql = "select count(*) as c from Sqlite_master where type ='table' and name ='" + STUDENT_TABLE + "' "; /** * 创建数据库和表 * @return SQLiteDatabase对象 */ public static SQLiteDatabase db() { boolean result = false; Cursor cursor = null; SQLiteDatabase db = null; //获取sdCard路径,设置数据库保存路径 String dbPath = Utils.getSdCard() + "/lxbd"; File dbp = new File(dbPath); File dbf = new File(dbPath + "/" + DB_NAME); //判断目录是否存在,不存在则创建 if (!dbp.exists()) { dbp.mkdir(); } // 数据库文件是否创建成功 boolean isFileCreateSuccess = false; if (!dbf.exists()) { try { isFileCreateSuccess = dbf.createNewFile(); } catch (IOException ioex) { } } else { isFileCreateSuccess = true; } if (isFileCreateSuccess) { //判断系统数据库中是否存在某个表 db = SQLiteDatabase.openOrCreateDatabase(dbf,null); cursor = db.rawQuery(sql,null); if (cursor.moveToNext()) { int count = cursor.getInt(0); if (count > 0) { result = true; } } //如果不存在表则创建表 if (!result) { db.execSQL(CREATE_TABLE); } } return db; } /** * 关闭数据库 */ public static void closeDB(SQLiteDatabase db) { db.close(); } } 以下的操作主要看自己要实现的功能来定,最主要的是上面的RecentlyWatchDB类
写入数据到数据库/** * 放进去写入到数据库 */ SQLiteDatabase db = RecentlyWatchDB.db(); Cursor cursor = db.query(RecentlyWatchDB.STUDENT_TABLE,null,RecentlyWatchDB.MID + "=?",new String[] { videoInfoList .get(position).getId() },null); // 判断数据库是否存在这个ID 的视频 if (cursor != null && cursor.moveToFirst()) { // 如果存在 则修改日期 ContentValues values = new ContentValues(); values.put(RecentlyWatchDB.TIME,Utils.getCurrentDateStr()); db.update( RecentlyWatchDB.STUDENT_TABLE,values,new String[] { videoInfoList.get(position).getId() }); } else { // 如果不存在 则新添加一条 ContentValues values = new ContentValues(); values.put(RecentlyWatchDB.MID,videoInfoList.get(position) .getId()); values.put(RecentlyWatchDB.TITLE,videoInfoList.get(position) .getTitle()); values.put(RecentlyWatchDB.DURATION,videoInfoList.get(position).getDuration()); values.put(RecentlyWatchDB.INFO,videoInfoList.get(position) .getInfo()); values.put(RecentlyWatchDB.SCORE,videoInfoList.get(position) .getScore()); values.put(RecentlyWatchDB.PLOGO,videoInfoList.get(position) .getPlogo()); values.put(RecentlyWatchDB.TIME,Utils.getCurrentDateStr()); db.insert(RecentlyWatchDB.STUDENT_TABLE,values); } db.close(); 在使用的地方取出来/** * 取出来 */ SQLiteDatabase db = RecentlyWatchDB.db(); List<VideoSQL> persons = null; Cursor cursor = db.query(true,RecentlyWatchDB.STUDENT_TABLE,"time desc",null); if (cursor != null) { persons = new ArrayList<VideoSQL>(); while (cursor.moveToNext()) { VideoSQL vs = new VideoSQL(); String id = cursor.getString(cursor .getColumnIndex(RecentlyWatchDB.MID)); String title = cursor.getString(cursor .getColumnIndex(RecentlyWatchDB.TITLE)); String plogo = cursor.getString(cursor .getColumnIndex(RecentlyWatchDB.PLOGO)); String duration = cursor.getString(cursor .getColumnIndex(RecentlyWatchDB.DURATION)); String info = cursor.getString(cursor .getColumnIndex(RecentlyWatchDB.INFO)); int score = cursor.getInt(cursor .getColumnIndex(RecentlyWatchDB.SCORE)); String time = cursor.getString(cursor .getColumnIndex(RecentlyWatchDB.TIME)); vs.setId(id); vs.setTitle(title); vs.setPlogo(plogo); vs.setDuration(duration); vs.setInfo(info); vs.setScore(score); vs.setTime(time); persons.add(vs); } } if (persons.size() >= 20) { for (int i = 0; i < 20; i++) { Video video = new Video(); video.setId(persons.get(i).getId()); video.setTitle(persons.get(i).getTitle()); video.setPlogo(persons.get(i).getPlogo()); video.setDuration(persons.get(i).getDuration()); video.setInfo(persons.get(i).getInfo()); video.setScore(persons.get(i).getScore()); historyvideoList.add(video); } String timeDelete = persons.get(19).getTime(); db.delete(RecentlyWatchDB.STUDENT_TABLE,RecentlyWatchDB.TIME + "<?",new String[] { timeDelete }); } else { for (int i = 0; i < persons.size(); i++) { Video video = new Video(); video.setId(persons.get(i).getId()); video.setTitle(persons.get(i).getTitle()); video.setPlogo(persons.get(i).getPlogo()); video.setDuration(persons.get(i).getDuration()); video.setInfo(persons.get(i).getInfo()); video.setScore(persons.get(i).getScore()); historyvideoList.add(video); } } db.close(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- oracle (+)是什么意思
- Cocos2D-X 项目发布到Android平台(二)配置安装Android SD
- swift3 – 如何在Swift 3中使用pow()并获得Int
- Ruby – 设置键数组中嵌套哈希的值
- 一个学习flash RIA不错的地方
- ruby-on-rails – Rails 4.2:渴望加载与STI的has_many关系
- c# – 循环访问对象的属性并获取DateTime类型的值
- 使用Oracle客户端11部署.NET应用程序所需的最少安装程序是什
- Newtonsoft.Json.DLL 解析Json数据
- ruby-on-rails – M.Hartl的Rails教程7.4.1中错误的参数数量