实现sqlite数据库保存数据

首先是一个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, null, null);
				// 判断数据库是否存在这个ID 的视频
				if (cursor != null && cursor.moveToFirst()) {
					// 如果存在 则修改日期
					ContentValues values = new ContentValues();
					values.put(RecentlyWatchDB.TIME, Utils.getCurrentDateStr());
					db.update(
							RecentlyWatchDB.STUDENT_TABLE,
							values,
							RecentlyWatchDB.MID + "=?",
							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, null, values);
				}
				db.close();

在使用的地方取出来

		/**
		 * 取出来
		 */
		SQLiteDatabase db = RecentlyWatchDB.db();
		
		List<VideoSQL> persons = null;
		Cursor cursor = db.query(true, RecentlyWatchDB.STUDENT_TABLE, null, null,
				null, null, null, "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();



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值