实现sqlite数据库保存数据

实现sqlite数据库保存数据,第1张

概述首先是一个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.SQLiteData 首先是一个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();
总结

以上是内存溢出为你收集整理的实现sqlite数据库保存数据全部内容,希望文章能够帮你解决实现sqlite数据库保存数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1172588.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存