package cn.edu.xtu.tilepuzzle;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class ClassSQLite {
private static Context context;// 应用环境上下文 Activity 是其子类
private final static String DBName=ClassGameDB.DATABASE_TILEPUZZLE_NAME;
private final static String userTableName=ClassGameDB.TABLE_TILEPUZZLE_PEOPLEINFO;
private final static String gameSetTableName=ClassGameDB.TABLE_TILEPUZZLE_GAMEDATA;
public ClassSQLite(Context _context) {
context = _context;
}
/**
* 打开数据库
*
* @DBName 数据库名字
*/
public SQLiteDatabase openDBByName(String DBName) {
SQLiteDatabase db;
//deleteDBByName(DBName);
db = context.openOrCreateDatabase(DBName, Context.MODE_PRIVATE, null);
return db;
}
/**
* 打开数据库
*
* @DBName 数据库名字
*/
public void createDBByName(String DBName) {
SQLiteDatabase db;
//deleteDBByName(DBName);
db = context.openOrCreateDatabase(DBName, Context.MODE_PRIVATE, null);
db.close();
}
/**
* 删除数据库
*
* @DBName 数据库名字
*/
public boolean deleteDBByName(String DBName) {
context.deleteDatabase(DBName);
Log.d("DB", "had deleted database:" + DBName);
return false;
}
/**
* 删除数据库中表的数据
* @TableName 表名字
*/
public boolean deleteTableByName(String TableName) {
SQLiteDatabase dbDatabase = openDBByName(DBName);
dbDatabase.delete(TableName, null, null);
close(dbDatabase);
Log.d("DB", "had deleted table:" + DBName + "->" + TableName);
return false;
}
public boolean isExistTableByName(String TableName) {
SQLiteDatabase db_ = openDBByName(DBName);
String sqlString = "select * from " + TableName;
try {
db_.execSQL(sqlString);
//Log.d("DB", "Create Table:" + TableName);
return true;
} catch (Exception e) {
//Log.d("DB", TableName + " is exists.");
return false;
} finally {
close(db_);
}
}
/**
* 检查数据库中的表是否存在
* @TableName 表名字
*/
public boolean checkTableIsExistByName(String TableName) {
SQLiteDatabase db_ = openDBByName(DBName);
String sqlString = "";
if (TableName.equals(ClassGameDB.TABLE_TILEPUZZLE_GAMEDATA)) {
sqlString = "CREATE TABLE " + TableName + ClassGameDB.GAMEDATA_STRUCTURE;
} else if (TableName.equals(ClassGameDB.TABLE_TILEPUZZLE_PEOPLEINFO)) {
sqlString = "CREATE TABLE " + TableName + ClassGameDB.PEOPLEINFO_STRUCTURE;
} else
;
try {
db_.execSQL(sqlString);
//Log.d("DB", "Create Table:" + TableName);
if (TableName.equals(ClassGameDB.TABLE_TILEPUZZLE_GAMEDATA))
this.addGameData( ClassGameDB.gameSetData);
return true;
} catch (Exception e) {
//Log.d("DB", TableName + " is exists.");
if (TableName.equals(ClassGameDB.TABLE_TILEPUZZLE_GAMEDATA)) {
Cursor cursor_ = getCursorByTableName(TableName);
if (cursor_ == null || cursor_.getCount() == 0)
addGameData(ClassGameDB.gameSetData);
}
return false;
} finally {
close(db_);
}
}
/**
* 在数据库中创建表
* @TableName 表名字
*/
public boolean createTableByName(String TableName){
SQLiteDatabase db_ = openDBByName(DBName);
String sqlString = "";
if (TableName.equals(ClassGameDB.TABLE_TILEPUZZLE_GAMEDATA)) {
sqlString = "CREATE TABLE " + TableName + ClassGameDB.GAMEDATA_STRUCTURE;
} else if (TableName.equals(ClassGameDB.TABLE_TILEPUZZLE_PEOPLEINFO)) {
sqlString = "CREATE TABLE " + TableName + ClassGameDB.PEOPLEINFO_STRUCTURE;
} else
;
try {
db_.execSQL(sqlString);
//Log.d("DB ", TableName + " had created.");
return true;
} catch (Exception e) {
//Log.d("DB ", TableName + " is exists. don't create.");
return false;
} finally {
close(db_);
}
}
/**
* 该方法已重载 保存数据到数据库的游戏配置参数表中
* @gameSetData_ 游戏数据
*
*/
public boolean addGameData(String[] gameSetData_) {
SQLiteDatabase db_=openDBByName(DBName);
String TableName=gameSetTableName;
String sql = "insert into " + TableName + " values(";
int i = 0;
for (i = 0; i < gameSetData_.length - 1; i++)
sql += "'" + gameSetData_[i] + "'" + ",";
sql += "'" + gameSetData_[i] + "');";
Log.d("DB", sql);
try {
db_.execSQL(sql);
//Log.d("DB", "insert Table ok");
return true;
} catch (Exception e) {
//Log.d("DB", "insert Table err: " + sql);
return false;
} finally {
close(db_);
}
}
/**
* 该方法已重载 保存数据到数据库的游戏玩家表中
*
* @DBName 数据库名字
* @TableName 表名字
* @peopleInfo 个人数据
*/
public boolean addUserInfo(String[][] peopleInfo) {
SQLiteDatabase db_=openDBByName(DBName);
ContentValues values=new ContentValues();
try {
for (int i = 0; i < peopleInfo.length; i++) {
values=new ContentValues();
values.put("NAME", peopleInfo[i][0]);
values.put("TIME", peopleInfo[i][1]);
db_.insert(userTableName, null, values);
}
Log.d("DB", "insert Table ok");
return true;
} catch (Exception e) {
Log.d("DB", "insert Table err: ???");
return false;
} finally {
close(db_);
}
}
/**
* 该方法已重载 保存数据到数据库的玩家信息表中
*
* @DBName 数据库名字
* @TableName 表名字
* @peopleName 个人名字
* @time 游戏用的时间
*/
/*public boolean addUserInfoTable(SQLiteDatabase db_, String TableName, String peopleName, int time) {
ContentValues values = new ContentValues();
try {
values.put("NAME", peopleName);
values.put("TIME", time);
db_.insert(TableName, null, values);
return true;
} catch (Exception e) {
Log.d("DB", "insert Table err:?? ");
return false;
} finally {
close(db_);
}
}
*/
/**
* 该方法已重载 保存数据到数据库的玩家信息表中
*
* @DBName 数据库名字
* @TableName 表名字
* @peopleName 个人名字
* @time 游戏用的时间
*/
public boolean addUserInfo(String peopleName, long time) {
SQLiteDatabase db_=openDBByName(DBName);
ContentValues values = new ContentValues();
try {
values.put("NAME", peopleName);
values.put("TIME", time);
db_.insert(userTableName, null, values);
return true;
} catch (Exception e) {
Log.d("DB", "insert Table err:?? ");
return false;
} finally {
close(db_);
}
}
/**
* 获取数据库的表中数据
* @DBName 数据库名字
* @TableName 表名字
* @return 返回Cursor
*/
/*public Cursor getDataByTableName(SQLiteDatabase db_, String TableName) {
// Cursor cursor = db_.query(TableName, null, null, null, null, null, null);
String sql = "";
if (TableName.equals(GameDB.TABLE_TILEPUZZLE_GAMEDATA))
sql = "select * from " + TableName+";";
else if (TableName.equals(GameDB.TABLE_TILEPUZZLE_PEOPLEINFO))
sql = "select * from " + TableName + " order by TIME ASC"+";";
Cursor cursor = db_.rawQuery(sql, null);
Log.d("DB", "cursor count:"+cursor.getCount());
int mycount = cursor.getCount();
close(db_);
if (mycount == 0)
return null;
else
return cursor;
}*/
/**
* 获取数据库的表中数据
* @DBName 数据库名字
* @TableName 表名字
* @return 返回Cursor
*/
public Cursor getCursorByTableName(String TableName) {
SQLiteDatabase db_=openDBByName(DBName) ;
// Cursor cursor = db_.query(TableName, null, null, null, null, null, null);
String sql = "";
if (TableName.equals(ClassGameDB.TABLE_TILEPUZZLE_GAMEDATA))
sql = "select * from " + TableName+";";
else if (TableName.equals(ClassGameDB.TABLE_TILEPUZZLE_PEOPLEINFO))
sql = "select * from " + TableName + " order by TIME ASC"+";";
Cursor cursor = db_.rawQuery(sql, null);
Log.d("DB", "cursor count:"+cursor.getCount());
int mycount = cursor.getCount();
close(db_);
if (mycount == 0)
return null;
else
return cursor;
}