AndroidデータベースSQLフレームワークの例
19850 ワード
この3つのファイルは完全なデータベースです.中で需要を修正することができます.
最初のファイル:MyplayerConstants.JAvaはデータベースを定義するいくつかの名前変数です
2番目のファイルJAvaはデータベースの作成です
3番目のファイルJAvaはデータベースの操作です
使用法は呼び出したactivityにmPlayerDBOOperation=new MyPlayerDBOOperation(DirectSeedingActivity.this);次に、mPlayerDBPOperationを呼び出す.QueryMoviePlayed();最後に閉じる:mPlayerDBPOperation.closeDB();
最初のファイル: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 }
2番目のファイル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);
}
*/
}
3番目のファイル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にmPlayerDBOOperation=new MyPlayerDBOOperation(DirectSeedingActivity.this);次に、mPlayerDBPOperationを呼び出す.QueryMoviePlayed();最後に閉じる:mPlayerDBPOperation.closeDB();