代码改变世界

android 数据库SQL 框架例子

2012-11-10 17:42  Rudrj2  阅读(614)  评论(0编辑  收藏  举报

这个三个文件是一个完整的的数据库。可以在里面修改需求。

第一个文件:MyplayerConstants.java 是定义数据库的一些名称变量

 1 /**
 2  * 设置常量的类
 3  * 
 4  * @author lihuikun
 5  * 2012-7-30
 6  */
 7 public class MyplayerConstants {
 8  
 9  /** DB相关 */
10  // DB 名称
11  public static final String DB_NAME = "myplayer.db" ;
12  // 已经播放的电影列表
13  public static final String TABLE_PLAYERED_NAME = "playered_table" ;
14  // 主键
15  public static final String PK_ID = "_id" ;
16  //电影的入口网址
17  public static final String MOVIE_HTTP = "movie_http" ;
18  //电影名称
19  //public static final String MOVIE_NAME = "movie_name" ;
20  //当前播放位置
21  public static final String PLAY_POSITION = "play_position" ;
22  //是否播放完
23  public static final String END_PLAY = "end_play" ;
24  // DB version
25  public static final int DB_VERSION = 1;
26 } 

 

第二个文件  MyPlayerDBManage.java 是创建数据库

 

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
/**
 * 创建播放器的数据库
 * @author lihuikun
 * 
 * 2012-7-30
 */
public class MyPlayerDBManage  extends SQLiteOpenHelper {
 private Context mContext ;
 
 public MyPlayerDBManage(Context context, String name,
   CursorFactory factory, int version) {
  super(context, name, factory, version);
  // TODO Auto-generated constructor stub
  
  mContext = context;
 }
 
 /**
  * 创建数据库表
  */
 @Override
 public void onCreate(SQLiteDatabase db) {
  // TODO Auto-generated method stub 
  db.execSQL("create table "+MyplayerConstants.TABLE_PLAYERED_NAME
    +"("+MyplayerConstants.PK_ID +" integer primary key autoincrement, " +
      MyplayerConstants.MOVIE_HTTP +" text, "+
     // MyplayerConstants.MOVIE_NAME +" text, "+
      MyplayerConstants.PLAY_POSITION +" text, " +
      MyplayerConstants.END_PLAY +" text)" );
  //   initData(db);
 }
 /**
  * 数据库版本更新时重建
  */
 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  // TODO Auto-generated method stub  
  db.execSQL("drop table if EXISTS "+MyplayerConstants.TABLE_PLAYERED_NAME+";");
  onCreate(db);
 }
 
  /**
  * 初始化插入的数据
  */ 
 /*
 private void initData(SQLiteDatabase db){
  // 全部应用
  String sql = "INSERT INTO "+MyplayerConstants.TABLE_PLAYERED_NAME+"(_id,movie_http,movie_name,play_position,end_play)values(1,'"+
      mContext.getResources().getString(R.string.use_all_package)+"','0','0','0')";
  db.execSQL(sql);
 }
 */ 
}  

第三个文件MyPlayerDBOperation.java 是数据库的操作

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
/**
 * 电影播放数据库操作*/
public class MyPlayerDBOperation {
 
 
 // DB辅助类
 private MyPlayerDBManage dbManege = null ;
 // 数据库对象
 private SQLiteDatabase myPlayerDB = null ;
 // 应用上下文
 private Context mContext ;
 
 /**
  * 构造方法
  * @param c 上下文
  */
 public MyPlayerDBOperation(Context c){
  mContext = c ;
  dbManege = new MyPlayerDBManage(c, MyplayerConstants.DB_NAME, null, MyplayerConstants.DB_VERSION);
  myPlayerDB = dbManege.getWritableDatabase();
 }
 
 
 /**
  * 删除数据库 
  */
 public void DeleteDataBase()
 {
  mContext.deleteDatabase(MyplayerConstants.DB_NAME);
 }
 /**
  * 删除一个表 
  */
 public void DeleteTable()
 {
  myPlayerDB.execSQL("DROP TABLE " + MyplayerConstants.TABLE_PLAYERED_NAME);
 }
    /**
     * 查询已经播放列表*/ 
 public int  QueryMoviePlayed(String vomie_http)
 {
  int position = 0;
  
  Cursor cur = myPlayerDB.query(MyplayerConstants.TABLE_PLAYERED_NAME, 
    new String[] {MyplayerConstants.PLAY_POSITION},MyplayerConstants.MOVIE_HTTP +" = ?" ,
    new String[]{vomie_http}, null, null, null);
  
  if(cur!=null && cur.getCount()>0)
  {
   cur.moveToFirst();    
   position = Integer.parseInt(cur.getString(cur.getColumnIndex(MyplayerConstants.PLAY_POSITION)));
   System.out.println("sql="+cur.getColumnIndex(MyplayerConstants.MOVIE_HTTP));
  }
  if(cur != null)
   cur.close();
  
  return position;
 }
  /**
     * 插入新电影到列表或更新已经播放列表*/
 public void InterMoviePlayed(String vomie_http,/*String vomie_name,*/int seek,String isend)
 {
  //isend 的值为'0'或'1',当isend = '0'时 表示还没有播放完,所以要记录播放位置 。
  //当为'1'时,表示播放完。要删除数据库里的这个数据。
  if(isend.equals("0"))
  {
   String movieID = null;
   // 先查询再新增
   Cursor cur = myPlayerDB.query(MyplayerConstants.TABLE_PLAYERED_NAME, 
     new String[] {MyplayerConstants.PK_ID},MyplayerConstants.MOVIE_HTTP +" = ?" ,
     new String[]{vomie_http}, null, null, null);
   
   if(cur!=null && cur.getCount()>0)
   {
    cur.moveToFirst();   
    movieID =  cur.getString(cur.getColumnIndex(MyplayerConstants.PK_ID));
    
    ContentValues value = new ContentValues(); 
    value.put(MyplayerConstants.PLAY_POSITION,  Integer.toString(seek));
    myPlayerDB.update(MyplayerConstants.TABLE_PLAYERED_NAME, value, MyplayerConstants.PK_ID+"=? ", new String[]{movieID});
   } 
   else
   {
    // DB没有相关记录,新增一条
    ContentValues value = new ContentValues();
    value.put(MyplayerConstants.MOVIE_HTTP, vomie_http);
   // value.put(MyplayerConstants.MOVIE_NAME, vomie_name);
    value.put(MyplayerConstants.PLAY_POSITION, Integer.toString(seek));
    value.put(MyplayerConstants.END_PLAY, isend);
    myPlayerDB.insert(MyplayerConstants.TABLE_PLAYERED_NAME, MyplayerConstants.PK_ID, value);
   }
   
   if(cur != null)
    cur.close();
  }
  else
  {
   DeleteData(vomie_http);
  }
  
 }
 /**
  * 从表中删除指定的一条数据 */
 public void DeleteData(String vomie_http)
 {
  
  String endplay = null;
  String movieID = null;
  Cursor cur = myPlayerDB.query(MyplayerConstants.TABLE_PLAYERED_NAME, 
    new String[] {MyplayerConstants.PK_ID, MyplayerConstants.END_PLAY},MyplayerConstants.MOVIE_HTTP +" = ?" ,
    new String[]{vomie_http}, null, null, null);
  
  if(cur!=null && cur.getCount()>0)
  {
   cur.moveToFirst();
   endplay =  cur.getString(cur.getColumnIndex(MyplayerConstants.END_PLAY));
   movieID =  cur.getString(cur.getColumnIndex(MyplayerConstants.PK_ID));
   
   myPlayerDB.execSQL("DELETE FROM " + MyplayerConstants.TABLE_PLAYERED_NAME + " WHERE _id=" + movieID);
   
  } 
  if(cur != null)
   cur.close();
   
 }
 /**
  * 关闭数据库
  */
  public void closeDB()
  {
 
   if (myPlayerDB != null && myPlayerDB.isOpen())  
    myPlayerDB.close();
   
   if (dbManege != null)  
    dbManege.close();     
  
  }
 
} 

用法在调用的activity里 mPlayerDBOperation =  new MyPlayerDBOperation(DirectSeedingActivity.this);
然后调用:mPlayerDBOperation.QueryMoviePlayed();
最后关闭:mPlayerDBOperation.closeDB();