前端之家收集整理的这篇文章主要介绍了
实现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);
// 判断数据库是否存在这个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();
原文链接:https://www.f2er.com/sqlite/200623.html