实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
package com.gqq.tangpoem;
import java.lang.annotation.Retention;
import java.security.PublicKey;
import java.util.*;
import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.util.*;
public class DataDb {
public static final String DATADB_NAME = "tangshi.db";
private static final String DATA_TABLE_NAME = "tangsong";
private SQLiteDatabase db;
private String path;
public DataDb(Context context, String path) {
this.path = path;
db = context.openOrCreateDatabase(path, Context.MODE_PRIVATE, null);
}
public void closeDB() {
if (null != db)
db.close();
}
public void openDB() {
if (null != db)
SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.CREATE_IF_NECESSARY);
}
public int[] getMaxMinId() {
String sql = "select max(id) as max, min(id) as min from tangsong where `status`=1;";
int[] ids = { 0, 0 };
Cursor c = db.rawQuery(sql, null);
if (c.moveToNext()) {
ids[0] = c.getInt(c.getColumnIndex("max"));
ids[1] = c.getInt(c.getColumnIndex("min"));
}
// db.close();
return ids;
}
public Poem getPoem(int id) {
int[] ids = getMaxMinId();
// 鐗╂瀬蹇呭弽锛屽鏋滃埌浜嗘渶澶х殑id锛屽垯鍥炲埌鏈�灏忕殑
if (id >= ids[0])
id = ids[0];
else if (id <= ids[1]) {
id = ids[1];
}
try {
String sql = "SELECT * from " DATA_TABLE_NAME " where `status`=1 and id=" id;
Log.d(MainActivity.TAG_DATABASE, sql);
Cursor c = db.rawQuery(sql, null);
return genPoem(c);
} catch (Exception e) {
e.printStackTrace();
db.close();
return null;
}
}
public Poem getNextPoem(int id) {
int[] ids = getMaxMinId();
// 鐗╂瀬蹇呭弽锛屽鏋滃埌浜嗘渶澶х殑id锛屽垯鍥炲埌鏈�灏忕殑
if (id >= ids[0])
return getPoem(ids[1]);
try {
String sql = "SELECT * from " DATA_TABLE_NAME " where `status`=1 and id>" id " order by id limit 1;";
Log.d(MainActivity.TAG_DATABASE, sql);
Cursor c = db.rawQuery(sql, null);
return genPoem(c);
} catch (Exception e) {
e.printStackTrace();
db.close();
return null;
}
}
public Poem getPrePoem(int id) {
int[] ids = getMaxMinId();
if (id <= ids[1])
return getPoem(ids[0]);
try {
String sql = "SELECT * from " DATA_TABLE_NAME " where `status`=1 and id<" id " order by id desc limit 1;";
Log.d(MainActivity.TAG_DATABASE, sql);
Cursor c = db.rawQuery(sql, null);
return genPoem(c);
} catch (Exception e) {
e.printStackTrace();
db.close();
return null;
}
}
/**
* 鏍规嵁浼犲叆鐨勬父鏍囩敓鎴愪竴涓狿oem瀵硅薄
*
* @param c
* 浼犲叆鐨勬父鏍�
* @return Poem瀵硅薄
*/
private Poem genPoem(Cursor c) {
Poem shiCi = null;
if (c.moveToNext()) {
int newId = c.getInt(c.getColumnIndex("id"));
int type = c.getInt(c.getColumnIndex("type"));
PoemType pType = Common.getType(type);
String author = c.getString(c.getColumnIndex("author"));
String cipai = c.getString(c.getColumnIndex("cipai"));
String title = c.getString(c.getColumnIndex("title"));
String content = c.getString(c.getColumnIndex("content"));
String comment = c.getString(c.getColumnIndex("comment"));
shiCi = new Poem(newId, pType, author, cipai, title, content, comment);
}
db.close();
return shiCi;
}
/**
* 鍙栧緱鎵�鏈夌殑璇楄瘝鏂�
*
* @return 璇楄瘝鍒楄〃
*/
public List<Poem> getAll() {
String sql = "SELECT * from " DATA_TABLE_NAME " where `status` = 1;";
return getPoemFromSql(sql);
}
/**
* 鍙栧緱鎵�鏈夌殑璇�
*
* @return 璇楄瘝鍒楄〃
*/
public List<Poem> getPoems() {
String sql = "SELECT * from " DATA_TABLE_NAME " where `type`=0 and `status` = 1;";
return getPoemFromSql(sql);
}
/**
* 鍙栧緱鎵�鏈夌殑璇�
*
* @return 璇楄瘝鍒楄〃
*/
public List<Poem> getSongs() {
String sql = "SELECT * from " DATA_TABLE_NAME " where `type`=1 and `status` = 1;";
return getPoemFromSql(sql);
}
/**
* 鍙栧緱鎵�鏈夌殑鏂�
*
* @return 璇楄瘝鍒楄〃
*/
public List<Poem> getEssays() {
String sql = "SELECT * from " DATA_TABLE_NAME " where `type`=9 and `status` = 1;";
return getPoemFromSql(sql);
}
/**
* 鍙栧緱鎵�鏈夌殑宸茬粡鍒犻櫎鐨勮瘲璇�
*
* @return 璇楄瘝鍒楄〃
*/
public List<Poem> getAllDeletedPoems() {
String sql = "SELECT * from " DATA_TABLE_NAME " where `status` = 99;";
return getPoemFromSql(sql);
}
/**
* 鍙栧緱鎵�鏈夌殑鐔熸倝鐨勭殑璇楄瘝
*
* @return 璇楄瘝鍒楄〃
*/
public List<Poem> getAllFamiliarPoems() {
String sql = "SELECT * from " DATA_TABLE_NAME " where `status` = 0;";
return getPoemFromSql(sql);
}
private List<Poem> getPoemFromSql(String sql) {
List<Poem> list = new ArrayList<Poem>();
Cursor c = db.rawQuery(sql, null);
while (c.moveToNext()) {
int id = c.getInt(c.getColumnIndex("id"));
int type = c.getInt(c.getColumnIndex("type"));
PoemType pType = Common.getType(type);
String author = c.getString(c.getColumnIndex("author"));
String cipai = c.getString(c.getColumnIndex("cipai"));
String title = c.getString(c.getColumnIndex("title"));
String content = c.getString(c.getColumnIndex("content"));
String comment = c.getString(c.getColumnIndex("comment"));
Poem shiCi = new Poem(id, pType, author, cipai, title, content, comment);
list.add(shiCi);
}
db.close();
return list;
}
/**
* 鎻掑叆涓�棣栨柊璇�
*
* @param type
* 绫诲瀷锛�0锛氳瘲锛�1锛氳瘝
* @param author
* 浣滆��
* @param title
* 棰樼洰
* @param cipai
* 璇嶇墝鍚�
* @param content
* 璇楄瘝鍐呭
* @return 鏄惁鎻掑叆鎴愬姛
*/
public int insertPoem(int type, String author, String title, String cipai, String content) {
String sql = "insert into " DATA_TABLE_NAME " (`status`, type, author,cipai,title, comment, content) values (1, " type ", '" author
"',";
sql = "'" cipai "',";
sql = "'" title "',";
sql = "'" "鏆傛椂鏃犳敞閲婏紒" "',";
sql = "'" content "');";
Log.d("Sql", sql);
try {
db.execSQL(sql);
} catch (Exception e) {
db.close();
e.printStackTrace();
return 0;
}
int[] ids = getMaxMinId();
db.close();
return ids[0];
}
/**
* 淇敼涓�棣栬瘲璇�
*
* @param type
* 绫诲瀷锛�0锛氳瘲锛�1锛氳瘝
* @param author
* 浣滆��
* @param title
* 棰樼洰
* @param cipai
* 璇嶇墝鍚�
* @param content
* 璇楄瘝鍐呭
* @return 鏄惁鎻掑叆鎴愬姛
*/
public boolean updatePoem(int id, int type, String author, String title, String cipai, String content) {
String sql = "update " DATA_TABLE_NAME " set `type`=" type ", author='" author;
sql = "', title='" title;
sql = "', cipai='" cipai;
sql = "', content='" content;
sql = "' where id=" id;
Log.d("Sql", sql);
try {
db.execSQL(sql);
db.close();
return true;
} catch (Exception e) {
db.close();
e.printStackTrace();
return false;
}
}
/**
* 鏇存柊璇楄瘝鐨勮瘎璁�
*
* @param id
* id
* @param cm
* 璇勮
* @return
*/
public boolean updatePoemComment(int id, String cm) {
String sql = "update " DATA_TABLE_NAME " set `comment`='" cm;
sql = "' where id=" id;
Log.d("Sql", sql);
try {
db.execSQL(sql);
db.close();
return true;
} catch (Exception e) {
db.close();
e.printStackTrace();
return false;
}
}
/**
* 鍒犻櫎涓�棣栬瘲璇�
*
* @param id
* 璇楄瘝id
* @return
*/
public boolean delPoem(int id) {
String sql = "update " DATA_TABLE_NAME " set `status` = 99 where id = " id;
Log.d(MainActivity.TAG_DATABASE, sql);
try {
db.execSQL(sql);
db.close();
return true;
} catch (Exception e) {
db.close();
e.printStackTrace();
return false;
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论