- 浏览: 337201 次
文章分类
最新评论
-
luokery:
你把正常的代码放到try里面不就好了, 异常的时候就catch ...
巧用while循环避免执行除了异常处理的的另一个处理机制 -
java_doom:
这个行号根据什么来的 为什么不是连续的?
别拿Hadoop map key当id使 -
mixer_b:
...
写博客的那些事儿 -
flyfx:
谢谢谢谢
java break,continue,return 使用。【借鉴与改编】 -
andilyliao:
比起青鸟 我更鄙视大学。
北大青鸟就让你们这么鄙视吗.
重载了很多方法,总有一个是你想要的。
package cn.edu.xtu.tilepuzzle.model;
import cn.edu.xtu.tilepuzzle.GameDB;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class ClassSQLite {
Context context;// 应用环境上下文 Activity 是其子类
public ClassSQLite(Context _context) {
this.context = _context;
}
/**
* 打开数据库
*
* @DBName 数据库名字
*/
public SQLiteDatabase openDBByName(String DBName) {
SQLiteDatabase db;
//deleteDBByName(DBName);
db = context.openOrCreateDatabase(DBName, Context.MODE_PRIVATE, null);
return db;
}
/**
* 删除数据库
*
* @DBName 数据库名字
*/
public boolean deleteDBByName(String DBName) {
context.deleteDatabase(DBName);
Log.d("DB", "had deleted database:" + DBName);
return false;
}
/**
* 删除数据库中表的数据
* @DBName 数据库名字
* @TableName 表名字
*/
public boolean deleteTableByDBName(String DBName, String TableName) {
SQLiteDatabase dbDatabase = openDBByName(DBName);
dbDatabase.delete(TableName, null, null);
close(dbDatabase);
// checkDBByName(DBName);
// db.delete(table, whereClause,
// whereArgs).deleteDatabase(GameDB.DATABASE_TILEPUZZLE_NAME);
Log.d("DB", "had deleted table:" + DBName + "->" + TableName);
return false;
}
/**
* 检查数据库中的表是否存在
*
* @DBName 数据库名字
* @TableName 表名字
*/
public boolean checkTableIsExistByName(String DBName, String TableName) {
SQLiteDatabase db_ = openDBByName(DBName);
String sqlString = "";
if (TableName.equals(GameDB.TABLE_TILEPUZZLE_GAMEDATA)) {
sqlString = "CREATE TABLE " + TableName + GameDB.GAMEDATA_STRUCTURE;
} else if (TableName.equals(GameDB.TABLE_TILEPUZZLE_PEOPLEINFO)) {
sqlString = "CREATE TABLE " + TableName + GameDB.PEOPLEINFO_STRUCTURE;
} else
;
try {
db_.execSQL(sqlString);
Log.d("DB", "Create Table:" + TableName);
if (TableName.equals(GameDB.TABLE_TILEPUZZLE_GAMEDATA))
this.insertDataToTableByTableName(db_, TableName, GameDB.gameSetData);
return true;
} catch (Exception e) {
Log.d("DB", TableName + " is exists.");
if (TableName.equals(GameDB.TABLE_TILEPUZZLE_GAMEDATA)) {
Cursor cursor_ = getDataByTableName(db_, TableName);
if (cursor_ == null || cursor_.getCount() == 0)
insertDataToTableByTableName(db_, TableName, GameDB.gameSetData);
}
} finally {
close(db_);
}
return false;
}
/**
* 该方法已重载 保存数据到数据库的游戏配置参数表中
*
* @DBName 数据库名字
* @TableName 表名字
* @gameSetData_ 游戏数据
*
*/
public boolean insertDataToTableByTableName(SQLiteDatabase db_, String TableName, String[] gameSetData_) {
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 insertDataToTableByTableName(SQLiteDatabase db_, String TableName, String[][] peopleInfo) {
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(TableName, 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 insertDataToTableByTableName(SQLiteDatabase db_, String TableName, String peopleName, int time) {
/*String sql = "";
try {
sql = "insert into " + TableName + " values('" + peopleName + "'," + time + ")";
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_);
}*/
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 表名字
* @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 表名字
* @peopleName 个人名字
* @time 游戏用的时间
*/
public boolean updateTableByName(String DBName, String TableName, String peopleName, int time) {
SQLiteDatabase db_ = openDBByName(DBName);
ContentValues values = new ContentValues();
values.put("NAME", peopleName);
values.put("TIME", time);
db_.update(TableName, values, "NAME='" + peopleName + "'", null);
close(db_);
return false;
}
/**
* 更新数据库中的个人信息
*
* @DBName 数据库名字
* @TableName 表名字
* @peopleName 个人名字
* @time 游戏用的时间
*/
public boolean updateTableByName(SQLiteDatabase db_, String TableName, String peopleName, int time) {
ContentValues values = new ContentValues();
values.put("NAME", peopleName);
values.put("TIME", time);
db_.update(TableName, values, "NAME='" + peopleName + "';", null);
close(db_);
return false;
}
/**
* 更新数据库中的游戏配置参数
*
* @DBName 数据库名字
* @TableName 表名字
* @gameData 游戏参数
*/
public boolean updateTableByName(String DBName, String TableName, String gameData[]) {
SQLiteDatabase db_ = openDBByName(DBName);
ContentValues values = new ContentValues();
values.put("orgImageString", gameData[GameDB.IndexInGameSetDatat_orgImageString]);
values.put("reversed", gameData[GameDB.IndexInGameSetDatat_reversed]);
values.put("funny", gameData[GameDB.IndexInGameSetDatat_funny]);
values.put("addString", gameData[GameDB.IndexInGameSetDatat_addString]);
values.put("hard", gameData[GameDB.IndexInGameSetDatat_hard]);
values.put("rows", gameData[GameDB.IndexInGameSetDatat_rows]);
values.put("columns", gameData[GameDB.IndexInGameSetDatat_columns]);
try {
db_.update(TableName, values, null, null);
Log.d("DB", "更新游戏信息成功:" + gameData);
return true;
} catch (Exception e) {
Log.d("DB", "更新游戏信息失败:" + gameData);
}finally{
close(db_);
}
return false;
}
/**
* 更新数据库中的游戏配置参数
*
* @DBName 数据库名字
* @TableName 表名字
* @gameData 游戏参数
*/
public boolean updateTableByName(SQLiteDatabase db_, String TableName, String gameData[]) {
//SQLiteDatabase db_ = openDBByName(DBName);
ContentValues values = new ContentValues();
values.put("orgImageString", gameData[GameDB.IndexInGameSetDatat_orgImageString]);
values.put("reversed", gameData[GameDB.IndexInGameSetDatat_reversed]);
values.put("funny", gameData[GameDB.IndexInGameSetDatat_funny]);
values.put("addString", gameData[GameDB.IndexInGameSetDatat_addString]);
values.put("hard", gameData[GameDB.IndexInGameSetDatat_hard]);
values.put("rows", gameData[GameDB.IndexInGameSetDatat_rows]);
values.put("columns", gameData[GameDB.IndexInGameSetDatat_columns]);
try {
Log.d("DB", "开始更新游戏信息:" +TableName);
//db_.delete(TableName, "orgImageString=?", null);
//db_.execSQL("delete from "+TableName+"where orgImageString != null;");
//Log.d("DB", "删除表信息成功:" +TableName);
//db_.insert(TableName, null, values);
db_.update(TableName, values, null,null);
Log.d("DB", "更新游戏信息成功->" +TableName+":"+ gameData.toString());
return true;
} catch (Exception e) {
Log.d("DB", "更新游戏信息失败->" +TableName);
}finally{
close(db_);
}
return false;
}
/**
* 通过用户名查询玩家信息记录
* @Cursor Cursor
* @peopleName 玩家名字
* @return String 指向结果记录的指针,类似于JDBC 的 ResultSet
*/
public String[] getPeopleInfoFromCursor(Cursor cursor, String peopleName) {
int mycount = cursor.getCount();
if (mycount == 0)
return null;
String[] peopleInfo = new String[2];
// 要利用所有的返回值得,我们需要把指针移到第一行上
cursor.moveToFirst();
// 同样,如果你要移动到返回值的某行上,你可以做如下操作
// cursor.moveToPosition(position);
int i = 0;
for (i = 0; i < mycount; i++) {
if (cursor.getString(1).endsWith(peopleName)) {
peopleInfo[0] = cursor.getString(0);
peopleInfo[1] = cursor.getString(1);
break;
}
// 获取某个值myCursor.getString();
cursor.moveToNext();
// myCursor.moveToPrevious();
}
cursor.close();
if (i >= mycount)
return null;
return peopleInfo;
}
/**
* 获取所有玩家信息
* @Cursor Cursor
* @return String[][]
*/
public String[][] getPeopleInfoFromCursor(Cursor cursor) {
int mycount = cursor.getCount();
if (mycount == 0)
return null;
String[][] peopleInfo = new String[mycount][2];
// 要利用所有的返回值得,我们需要把指针移到第一行上
cursor.moveToFirst();
// 同样,如果你要移动到返回值的某行上,你可以做如下操作
// cursor.moveToPosition(position);
int i = 0;
for (i = 0; i < mycount; i++) {
peopleInfo[i][0] = cursor.getString(cursor.getColumnIndex("NAME"));
peopleInfo[i][1] = cursor.getString(cursor.getColumnIndex("TIME"));
// 获取某个值myCursor.getString();
cursor.moveToNext();
// myCursor.moveToPrevious();
}
cursor.close();
return peopleInfo;
}
/**
* 获取游戏参数信息
* @Cursor Cursor
* @return String[]
*/
public String[] getGameDataFromCursor(Cursor cursor) {
int mycount = cursor.getCount();
if (mycount == 0)
return null;
String[] gameData = new String[GameDB.gameSetData.length];
// 要利用所有的返回值得,我们需要把指针移到第一行上
cursor.moveToFirst();
//"orgImageString", "reversed", "funny", "addStringg", "hard", "rows","columns"
int orgImageString = cursor.getColumnIndex(GameDB.gameSetDataName[GameDB.IndexInGameSetDatat_orgImageString]);
gameData[GameDB.IndexInGameSetDatat_orgImageString]=cursor.getString(orgImageString);
Log.d("DB", orgImageString+"->orgImageString:"+cursor.getString(orgImageString));
int reversed = cursor.getColumnIndex(GameDB.gameSetDataName[GameDB.IndexInGameSetDatat_reversed]);
gameData[GameDB.IndexInGameSetDatat_reversed]=cursor.getString(reversed);
Log.d("DB", reversed+"->reversed:"+cursor.getString(reversed));
int funny = cursor.getColumnIndex(GameDB.gameSetDataName[GameDB.IndexInGameSetDatat_funny]);
gameData[GameDB.IndexInGameSetDatat_funny]=cursor.getString(funny);
Log.d("DB", funny+"->funny:"+cursor.getString(funny));
int addString = cursor.getColumnIndex(GameDB.gameSetDataName[GameDB.IndexInGameSetDatat_addString]);
gameData[GameDB.IndexInGameSetDatat_addString]=cursor.getString(addString);
Log.d("DB", addString+"->addString:"+cursor.getString(addString));
int hard = cursor.getColumnIndex(GameDB.gameSetDataName[GameDB.IndexInGameSetDatat_hard]);
gameData[GameDB.IndexInGameSetDatat_hard]=cursor.getString(hard);
Log.d("DB", hard+"->hard:"+cursor.getString(hard));
int rows = cursor.getColumnIndex(GameDB.gameSetDataName[GameDB.IndexInGameSetDatat_rows]);
gameData[GameDB.IndexInGameSetDatat_rows]=cursor.getString(rows);
Log.d("DB", rows+"->rows:"+cursor.getString(rows));
int columns = cursor.getColumnIndex(GameDB.gameSetDataName[GameDB.IndexInGameSetDatat_columns]);
gameData[GameDB.IndexInGameSetDatat_columns]=cursor.getString(columns);
Log.d("DB", columns+"->columns:"+cursor.getString(columns));
cursor.close();
return gameData;
}
/**
* 删除数据库中表的个人信息
* @DBName 数据库名字
* @TableName 表名字
* @peopleName 个人名字
*/
public boolean deletePeopleInfoByPeopleName(String DBName, String TableName, String peopleName) {
SQLiteDatabase db_ = openDBByName(DBName);
db_.delete(TableName, "NAME='" + peopleName + "'", null);
close(db_);
return false;
}
/**
* 删除数据库中表的个人信息
* @db_ 数据库
* @TableName 表名字
* @peopleName 个人名字
*/
public boolean deletePeopleInfoByPeopleName(SQLiteDatabase db_, String TableName, String peopleName) {
db_.delete(TableName, "NAME='" + peopleName + "'", null);
close(db_);
return false;
}
public void close(SQLiteDatabase db_) {
db_.close();
}
public void SqliteTest() {
Cursor cursor = getDataByTableName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_GAMEDATA);
String gameData[]=getGameDataFromCursor(cursor);
String resultString="";
for(int i=0;i<7;i++){
resultString+=gameData[i]+";";
}
Log.d("DB", "SqliteTest:查询->TABLE_TILEPUZZLE_GAMEDATA:"+resultString);
String temp[]=GameDB.gameSetData;
temp[1]="Y";
temp[2]="Y";
temp[3]="Y";
temp[4]="Y";
resultString="";
for(int i=0;i<7;i++)
resultString+=temp[i]+";";
Log.d("DB", "SqliteTest:更改游戏参数为->TABLE_TILEPUZZLE_GAMEDATA:"+resultString);
updateTableByName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_GAMEDATA, temp);
cursor = getDataByTableName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_GAMEDATA);
gameData=getGameDataFromCursor(cursor);
resultString="";
for(int i=0;i<7;i++){
resultString+=gameData[i]+";";
}
Log.d("DB", "SqliteTest:更新后->TABLE_TILEPUZZLE_GAMEDATA:"+resultString);
//Log.d("DB", "SqliteTest:删除后->TABLE_TILEPUZZLE_GAMEDATA:"+resultString);
String tempP[][]=new String[2][2];
tempP[0][0]="LIUYAOHUA";
tempP[0][1]="100";
tempP[1][0]="DAIQING";
tempP[1][1]="500";
insertDataToTableByTableName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_PEOPLEINFO, tempP);
Log.d("DB", "SqliteTest:插入->TABLE_TILEPUZZLE_PEOPLEINFO:"+tempP);
cursor = getDataByTableName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_PEOPLEINFO);
String peopleInfo[][]=getPeopleInfoFromCursor(cursor);
for(int i=0;i<peopleInfo.length;i++){
Log.d("DB", "SqliteTest:查询->TABLE_TILEPUZZLE_PEOPLEINFO:"+peopleInfo[i][0]+" : "+peopleInfo[i][1]);
}
updateTableByName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_PEOPLEINFO, "LIUYAOHUA", 50);
updateTableByName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_PEOPLEINFO, "DAIQING", 50);
Cursor cursorUP = getDataByTableName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_PEOPLEINFO);
String peopleInfoUP[][]=getPeopleInfoFromCursor(cursorUP);
for(int i=0;i<peopleInfoUP.length;i++){
Log.d("DB", "SqliteTest:更新后->TABLE_TILEPUZZLE_PEOPLEINFO:"+peopleInfoUP[i][0]+" : "+peopleInfoUP[i][1]);
}
deletePeopleInfoByPeopleName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_PEOPLEINFO,"DAIQING");
Cursor cursorD = getDataByTableName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_PEOPLEINFO);
String peopleInfoD[][]=getPeopleInfoFromCursor(cursorD);
for(int i=0;i<peopleInfoD.length;i++){
Log.d("DB", "SqliteTest:删除后->TABLE_TILEPUZZLE_PEOPLEINFO:"+peopleInfoD[i][0]+" : "+peopleInfoD[i][1]);
}
insertDataToTableByTableName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_PEOPLEINFO,"DAIQING",100);
cursor = getDataByTableName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_PEOPLEINFO);
peopleInfo=getPeopleInfoFromCursor(cursor);
for(int i=0;i<peopleInfo.length;i++){
Log.d("DB", "SqliteTest:删除再次增加用户信息查询->TABLE_TILEPUZZLE_PEOPLEINFO:"+peopleInfo[i][0]+" : "+peopleInfo[i][1]);
}
}
}
package cn.edu.xtu.tilepuzzle.model;
import cn.edu.xtu.tilepuzzle.GameDB;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class ClassSQLite {
Context context;// 应用环境上下文 Activity 是其子类
public ClassSQLite(Context _context) {
this.context = _context;
}
/**
* 打开数据库
*
* @DBName 数据库名字
*/
public SQLiteDatabase openDBByName(String DBName) {
SQLiteDatabase db;
//deleteDBByName(DBName);
db = context.openOrCreateDatabase(DBName, Context.MODE_PRIVATE, null);
return db;
}
/**
* 删除数据库
*
* @DBName 数据库名字
*/
public boolean deleteDBByName(String DBName) {
context.deleteDatabase(DBName);
Log.d("DB", "had deleted database:" + DBName);
return false;
}
/**
* 删除数据库中表的数据
* @DBName 数据库名字
* @TableName 表名字
*/
public boolean deleteTableByDBName(String DBName, String TableName) {
SQLiteDatabase dbDatabase = openDBByName(DBName);
dbDatabase.delete(TableName, null, null);
close(dbDatabase);
// checkDBByName(DBName);
// db.delete(table, whereClause,
// whereArgs).deleteDatabase(GameDB.DATABASE_TILEPUZZLE_NAME);
Log.d("DB", "had deleted table:" + DBName + "->" + TableName);
return false;
}
/**
* 检查数据库中的表是否存在
*
* @DBName 数据库名字
* @TableName 表名字
*/
public boolean checkTableIsExistByName(String DBName, String TableName) {
SQLiteDatabase db_ = openDBByName(DBName);
String sqlString = "";
if (TableName.equals(GameDB.TABLE_TILEPUZZLE_GAMEDATA)) {
sqlString = "CREATE TABLE " + TableName + GameDB.GAMEDATA_STRUCTURE;
} else if (TableName.equals(GameDB.TABLE_TILEPUZZLE_PEOPLEINFO)) {
sqlString = "CREATE TABLE " + TableName + GameDB.PEOPLEINFO_STRUCTURE;
} else
;
try {
db_.execSQL(sqlString);
Log.d("DB", "Create Table:" + TableName);
if (TableName.equals(GameDB.TABLE_TILEPUZZLE_GAMEDATA))
this.insertDataToTableByTableName(db_, TableName, GameDB.gameSetData);
return true;
} catch (Exception e) {
Log.d("DB", TableName + " is exists.");
if (TableName.equals(GameDB.TABLE_TILEPUZZLE_GAMEDATA)) {
Cursor cursor_ = getDataByTableName(db_, TableName);
if (cursor_ == null || cursor_.getCount() == 0)
insertDataToTableByTableName(db_, TableName, GameDB.gameSetData);
}
} finally {
close(db_);
}
return false;
}
/**
* 该方法已重载 保存数据到数据库的游戏配置参数表中
*
* @DBName 数据库名字
* @TableName 表名字
* @gameSetData_ 游戏数据
*
*/
public boolean insertDataToTableByTableName(SQLiteDatabase db_, String TableName, String[] gameSetData_) {
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 insertDataToTableByTableName(SQLiteDatabase db_, String TableName, String[][] peopleInfo) {
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(TableName, 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 insertDataToTableByTableName(SQLiteDatabase db_, String TableName, String peopleName, int time) {
/*String sql = "";
try {
sql = "insert into " + TableName + " values('" + peopleName + "'," + time + ")";
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_);
}*/
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 表名字
* @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 表名字
* @peopleName 个人名字
* @time 游戏用的时间
*/
public boolean updateTableByName(String DBName, String TableName, String peopleName, int time) {
SQLiteDatabase db_ = openDBByName(DBName);
ContentValues values = new ContentValues();
values.put("NAME", peopleName);
values.put("TIME", time);
db_.update(TableName, values, "NAME='" + peopleName + "'", null);
close(db_);
return false;
}
/**
* 更新数据库中的个人信息
*
* @DBName 数据库名字
* @TableName 表名字
* @peopleName 个人名字
* @time 游戏用的时间
*/
public boolean updateTableByName(SQLiteDatabase db_, String TableName, String peopleName, int time) {
ContentValues values = new ContentValues();
values.put("NAME", peopleName);
values.put("TIME", time);
db_.update(TableName, values, "NAME='" + peopleName + "';", null);
close(db_);
return false;
}
/**
* 更新数据库中的游戏配置参数
*
* @DBName 数据库名字
* @TableName 表名字
* @gameData 游戏参数
*/
public boolean updateTableByName(String DBName, String TableName, String gameData[]) {
SQLiteDatabase db_ = openDBByName(DBName);
ContentValues values = new ContentValues();
values.put("orgImageString", gameData[GameDB.IndexInGameSetDatat_orgImageString]);
values.put("reversed", gameData[GameDB.IndexInGameSetDatat_reversed]);
values.put("funny", gameData[GameDB.IndexInGameSetDatat_funny]);
values.put("addString", gameData[GameDB.IndexInGameSetDatat_addString]);
values.put("hard", gameData[GameDB.IndexInGameSetDatat_hard]);
values.put("rows", gameData[GameDB.IndexInGameSetDatat_rows]);
values.put("columns", gameData[GameDB.IndexInGameSetDatat_columns]);
try {
db_.update(TableName, values, null, null);
Log.d("DB", "更新游戏信息成功:" + gameData);
return true;
} catch (Exception e) {
Log.d("DB", "更新游戏信息失败:" + gameData);
}finally{
close(db_);
}
return false;
}
/**
* 更新数据库中的游戏配置参数
*
* @DBName 数据库名字
* @TableName 表名字
* @gameData 游戏参数
*/
public boolean updateTableByName(SQLiteDatabase db_, String TableName, String gameData[]) {
//SQLiteDatabase db_ = openDBByName(DBName);
ContentValues values = new ContentValues();
values.put("orgImageString", gameData[GameDB.IndexInGameSetDatat_orgImageString]);
values.put("reversed", gameData[GameDB.IndexInGameSetDatat_reversed]);
values.put("funny", gameData[GameDB.IndexInGameSetDatat_funny]);
values.put("addString", gameData[GameDB.IndexInGameSetDatat_addString]);
values.put("hard", gameData[GameDB.IndexInGameSetDatat_hard]);
values.put("rows", gameData[GameDB.IndexInGameSetDatat_rows]);
values.put("columns", gameData[GameDB.IndexInGameSetDatat_columns]);
try {
Log.d("DB", "开始更新游戏信息:" +TableName);
//db_.delete(TableName, "orgImageString=?", null);
//db_.execSQL("delete from "+TableName+"where orgImageString != null;");
//Log.d("DB", "删除表信息成功:" +TableName);
//db_.insert(TableName, null, values);
db_.update(TableName, values, null,null);
Log.d("DB", "更新游戏信息成功->" +TableName+":"+ gameData.toString());
return true;
} catch (Exception e) {
Log.d("DB", "更新游戏信息失败->" +TableName);
}finally{
close(db_);
}
return false;
}
/**
* 通过用户名查询玩家信息记录
* @Cursor Cursor
* @peopleName 玩家名字
* @return String 指向结果记录的指针,类似于JDBC 的 ResultSet
*/
public String[] getPeopleInfoFromCursor(Cursor cursor, String peopleName) {
int mycount = cursor.getCount();
if (mycount == 0)
return null;
String[] peopleInfo = new String[2];
// 要利用所有的返回值得,我们需要把指针移到第一行上
cursor.moveToFirst();
// 同样,如果你要移动到返回值的某行上,你可以做如下操作
// cursor.moveToPosition(position);
int i = 0;
for (i = 0; i < mycount; i++) {
if (cursor.getString(1).endsWith(peopleName)) {
peopleInfo[0] = cursor.getString(0);
peopleInfo[1] = cursor.getString(1);
break;
}
// 获取某个值myCursor.getString();
cursor.moveToNext();
// myCursor.moveToPrevious();
}
cursor.close();
if (i >= mycount)
return null;
return peopleInfo;
}
/**
* 获取所有玩家信息
* @Cursor Cursor
* @return String[][]
*/
public String[][] getPeopleInfoFromCursor(Cursor cursor) {
int mycount = cursor.getCount();
if (mycount == 0)
return null;
String[][] peopleInfo = new String[mycount][2];
// 要利用所有的返回值得,我们需要把指针移到第一行上
cursor.moveToFirst();
// 同样,如果你要移动到返回值的某行上,你可以做如下操作
// cursor.moveToPosition(position);
int i = 0;
for (i = 0; i < mycount; i++) {
peopleInfo[i][0] = cursor.getString(cursor.getColumnIndex("NAME"));
peopleInfo[i][1] = cursor.getString(cursor.getColumnIndex("TIME"));
// 获取某个值myCursor.getString();
cursor.moveToNext();
// myCursor.moveToPrevious();
}
cursor.close();
return peopleInfo;
}
/**
* 获取游戏参数信息
* @Cursor Cursor
* @return String[]
*/
public String[] getGameDataFromCursor(Cursor cursor) {
int mycount = cursor.getCount();
if (mycount == 0)
return null;
String[] gameData = new String[GameDB.gameSetData.length];
// 要利用所有的返回值得,我们需要把指针移到第一行上
cursor.moveToFirst();
//"orgImageString", "reversed", "funny", "addStringg", "hard", "rows","columns"
int orgImageString = cursor.getColumnIndex(GameDB.gameSetDataName[GameDB.IndexInGameSetDatat_orgImageString]);
gameData[GameDB.IndexInGameSetDatat_orgImageString]=cursor.getString(orgImageString);
Log.d("DB", orgImageString+"->orgImageString:"+cursor.getString(orgImageString));
int reversed = cursor.getColumnIndex(GameDB.gameSetDataName[GameDB.IndexInGameSetDatat_reversed]);
gameData[GameDB.IndexInGameSetDatat_reversed]=cursor.getString(reversed);
Log.d("DB", reversed+"->reversed:"+cursor.getString(reversed));
int funny = cursor.getColumnIndex(GameDB.gameSetDataName[GameDB.IndexInGameSetDatat_funny]);
gameData[GameDB.IndexInGameSetDatat_funny]=cursor.getString(funny);
Log.d("DB", funny+"->funny:"+cursor.getString(funny));
int addString = cursor.getColumnIndex(GameDB.gameSetDataName[GameDB.IndexInGameSetDatat_addString]);
gameData[GameDB.IndexInGameSetDatat_addString]=cursor.getString(addString);
Log.d("DB", addString+"->addString:"+cursor.getString(addString));
int hard = cursor.getColumnIndex(GameDB.gameSetDataName[GameDB.IndexInGameSetDatat_hard]);
gameData[GameDB.IndexInGameSetDatat_hard]=cursor.getString(hard);
Log.d("DB", hard+"->hard:"+cursor.getString(hard));
int rows = cursor.getColumnIndex(GameDB.gameSetDataName[GameDB.IndexInGameSetDatat_rows]);
gameData[GameDB.IndexInGameSetDatat_rows]=cursor.getString(rows);
Log.d("DB", rows+"->rows:"+cursor.getString(rows));
int columns = cursor.getColumnIndex(GameDB.gameSetDataName[GameDB.IndexInGameSetDatat_columns]);
gameData[GameDB.IndexInGameSetDatat_columns]=cursor.getString(columns);
Log.d("DB", columns+"->columns:"+cursor.getString(columns));
cursor.close();
return gameData;
}
/**
* 删除数据库中表的个人信息
* @DBName 数据库名字
* @TableName 表名字
* @peopleName 个人名字
*/
public boolean deletePeopleInfoByPeopleName(String DBName, String TableName, String peopleName) {
SQLiteDatabase db_ = openDBByName(DBName);
db_.delete(TableName, "NAME='" + peopleName + "'", null);
close(db_);
return false;
}
/**
* 删除数据库中表的个人信息
* @db_ 数据库
* @TableName 表名字
* @peopleName 个人名字
*/
public boolean deletePeopleInfoByPeopleName(SQLiteDatabase db_, String TableName, String peopleName) {
db_.delete(TableName, "NAME='" + peopleName + "'", null);
close(db_);
return false;
}
public void close(SQLiteDatabase db_) {
db_.close();
}
public void SqliteTest() {
Cursor cursor = getDataByTableName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_GAMEDATA);
String gameData[]=getGameDataFromCursor(cursor);
String resultString="";
for(int i=0;i<7;i++){
resultString+=gameData[i]+";";
}
Log.d("DB", "SqliteTest:查询->TABLE_TILEPUZZLE_GAMEDATA:"+resultString);
String temp[]=GameDB.gameSetData;
temp[1]="Y";
temp[2]="Y";
temp[3]="Y";
temp[4]="Y";
resultString="";
for(int i=0;i<7;i++)
resultString+=temp[i]+";";
Log.d("DB", "SqliteTest:更改游戏参数为->TABLE_TILEPUZZLE_GAMEDATA:"+resultString);
updateTableByName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_GAMEDATA, temp);
cursor = getDataByTableName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_GAMEDATA);
gameData=getGameDataFromCursor(cursor);
resultString="";
for(int i=0;i<7;i++){
resultString+=gameData[i]+";";
}
Log.d("DB", "SqliteTest:更新后->TABLE_TILEPUZZLE_GAMEDATA:"+resultString);
//Log.d("DB", "SqliteTest:删除后->TABLE_TILEPUZZLE_GAMEDATA:"+resultString);
String tempP[][]=new String[2][2];
tempP[0][0]="LIUYAOHUA";
tempP[0][1]="100";
tempP[1][0]="DAIQING";
tempP[1][1]="500";
insertDataToTableByTableName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_PEOPLEINFO, tempP);
Log.d("DB", "SqliteTest:插入->TABLE_TILEPUZZLE_PEOPLEINFO:"+tempP);
cursor = getDataByTableName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_PEOPLEINFO);
String peopleInfo[][]=getPeopleInfoFromCursor(cursor);
for(int i=0;i<peopleInfo.length;i++){
Log.d("DB", "SqliteTest:查询->TABLE_TILEPUZZLE_PEOPLEINFO:"+peopleInfo[i][0]+" : "+peopleInfo[i][1]);
}
updateTableByName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_PEOPLEINFO, "LIUYAOHUA", 50);
updateTableByName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_PEOPLEINFO, "DAIQING", 50);
Cursor cursorUP = getDataByTableName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_PEOPLEINFO);
String peopleInfoUP[][]=getPeopleInfoFromCursor(cursorUP);
for(int i=0;i<peopleInfoUP.length;i++){
Log.d("DB", "SqliteTest:更新后->TABLE_TILEPUZZLE_PEOPLEINFO:"+peopleInfoUP[i][0]+" : "+peopleInfoUP[i][1]);
}
deletePeopleInfoByPeopleName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_PEOPLEINFO,"DAIQING");
Cursor cursorD = getDataByTableName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_PEOPLEINFO);
String peopleInfoD[][]=getPeopleInfoFromCursor(cursorD);
for(int i=0;i<peopleInfoD.length;i++){
Log.d("DB", "SqliteTest:删除后->TABLE_TILEPUZZLE_PEOPLEINFO:"+peopleInfoD[i][0]+" : "+peopleInfoD[i][1]);
}
insertDataToTableByTableName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_PEOPLEINFO,"DAIQING",100);
cursor = getDataByTableName(openDBByName(GameDB.DATABASE_TILEPUZZLE_NAME), GameDB.TABLE_TILEPUZZLE_PEOPLEINFO);
peopleInfo=getPeopleInfoFromCursor(cursor);
for(int i=0;i<peopleInfo.length;i++){
Log.d("DB", "SqliteTest:删除再次增加用户信息查询->TABLE_TILEPUZZLE_PEOPLEINFO:"+peopleInfo[i][0]+" : "+peopleInfo[i][1]);
}
}
}
发表评论
-
Android游戏源码合集(主要是AndEngine和Libgdx的)
2012-04-26 00:37 1111最近在网络上看到有网友抱怨Android游戏源码找不到,所以小 ... -
Android闹钟最终版【android源码闹钟解析】
2012-04-26 00:36 2582我以前写了个复杂闹钟的demo,参见Android闹钟【复杂版 ... -
android中常用的弹出提示框
2012-04-24 21:37 1040我们在平时做开发的时 ... -
Android 蓝牙编程基础
2012-04-15 23:52 1674在移动设备上,联网的方式很多,也存在了很大的差异,包括速度,有 ...
相关推荐
本案例将详细介绍如何在Android应用中使用SQLite进行增(Create)、删(Delete)、改(Update)和查(Read)操作。 1. **创建SQLite数据库** 在Android中,我们需要创建一个继承自`SQLiteOpenHelper`的类,这个类...
"Android之SQLite实现增删改查"这个标题表明了本文将介绍如何在Android平台中使用SQLite数据库来实现增删改查操作。 描述解释 "这是学习及开发SQlite的一个很好的例子,含有其增删改查的所有功能附有完整的代码可以...
相当简单Android SQLite数据库增删改查,绝对适合新手,包您一看就会, 1.这是我照着网上的资料和帮助手册写的; 2.数据库的增删改查都涉及到了; 3.导入项目,运行,然后 在终端进入数据库,命令为 adb shell cd ...
以上就是Android中使用SQLite数据库进行增删改查的基本操作,实际应用中可能需要根据需求进行更复杂的查询和事务处理。了解这些基础,可以帮助开发者高效地管理和操作Android应用中的数据。在`AndroidSqliteTest`...
根据Android数据存储的内容,综合应用SharedPreferences和SQLite数据库实现一个用户信息管理系统,强化对SharedPreferences的理解的使用,熟练掌握SQLite的操作。 要求: 1. 巩固Android应用开发工具(Eclipse或者...
以上就是Android中对SQLite数据库进行增删改查的基本操作和注意事项。通过这些方法,开发者能够灵活地管理应用程序的数据存储。在实际项目中,还可以结合ContentProvider和LiveData等组件,构建更复杂的数据库交互...
本篇文章将详细解析标题和描述中提到的两个关键文件:SQLiteManager.java和DBSOpenHelper.java,以及它们在实现SQLite数据库增删改查功能中的作用。 1. **SQLiteManager**: 这个类通常是对SQLite数据库操作的封装,...
本教程将详细介绍如何在Android中连接SQLite数据库,并执行基本的增删改查(CRUD)操作以及事务处理。 首先,我们需要创建一个SQLite数据库。在Android中,我们通常通过扩展`SQLiteOpenHelper`类来实现。这个类提供...
在这个“【Android】Sqlite数据库增删改查(修改版)”项目中,我们将探讨如何在Android应用中使用SQLite进行基本的数据操作:增加、删除、修改和查询。 首先,我们要了解SQLite在Android中的集成。每个Android应用...
本示例将详细介绍如何在Android应用中实现SQLite数据库的增删改查功能。 首先,我们需要在Android项目中创建一个SQLite数据库。这通常通过创建一个SQLiteOpenHelper的子类来完成。SQLiteOpenHelper负责创建和升级...
在实验过程中,学生通过创建一个简单的记事本应用,实现了使用 SQLite 进行数据的增删改查,以及利用 `SharedPreferences` 存储简单的配置信息。这样的实践有助于理解这些数据存储方式在实际开发中的应用,并加深了...
本教程将通过一个名为"SqliteTestDemo"的示例项目,深入讲解如何在Android中进行SQLite数据库的增、删、改、查操作。 首先,我们需要创建一个SQLite数据库。在Android中,我们通常会创建一个`SQLiteOpenHelper`的...
Android+SQlite 简单的《学生信息管理系统》(实现基本增删改查) 此代码中还有与其相对应的apk文件(在SIMS/bin目录中),大家可先行放手机上看一下效果。 希望对初学者有一定的帮助。(本人自己编写)
本示例主要讲解如何在Android应用中进行SQLite数据库的增删改查操作,这对于任何需要持久化数据的应用都是必不可少的。 1. **创建SQLite数据库** 在Android中,我们通常通过`SQLiteOpenHelper`类来创建和管理...
Android管理信息系统: 包括SQLite 数据库的增删改查,ListView的刷新,弹出对话框提示操作 ListView列表的优化 [注:本内容来自网络,在此分享仅为帮助有需要的网友,如果侵犯了您的权利,麻烦联系我,我会第一时间...
本项目“Android简单数据库操作增删改查项目实现”旨在介绍如何在Android应用中使用SQLite进行基本的数据操作,包括添加(Insert)、查询(Query)、更新(Update)和删除(Delete)。 1. 数据库初始化: 在...
本教程将深入讲解如何在Android中使用SQLite数据库进行基本的增删改查操作。 首先,我们需要在Android项目中创建一个SQLite数据库。这通常通过继承`SQLiteOpenHelper`类来完成。`SQLiteOpenHelper`有两个关键的方法...
在Android开发中,数据存储是不可或缺的一部分,XUtils是一个强大的工具库,它为开发者提供了方便的数据操作接口,包括对SQLite数据库的增删改查以及数据库版本管理。本篇将详细介绍如何利用XUtils进行数据库操作...
Android Stdio开发的一个应用,运用Sqlite数据库和listview实现在前端进行对Sqlite的增删改查,数据库中的数据显示在Listview上,在Editetext上输入关键字会查询带有关键字的数据,点击Listview的每一行都可以对该行...
在本示例"android基本SQLite数据库操作的增删改查demo"中,开发者创建了一个用于管理停车位查询的应用,其中涉及到了对SQLite数据库的基础操作:增加数据(Add)、查询数据(Query)、删除数据(Delete)以及修改...