實現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();



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章