基本信息
源码名称:android 唐诗宋词 app 完整源码下载
源码大小:1.31M
文件格式:.zip
开发语言:Java
更新时间:2015-05-16
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


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;
		}

	}

}