玩轉Android sqlLite---(附android DB的圖行工具)
sqlLite就像個迷你數據庫,配套的操作工具還不完善,操作起來不直觀。不像oracle、mysql那樣有圖形化的操作工作。
偶然在網上發現一款操作sqlLite的圖形化工具 ---- SQLiteSpy(后附上鏈接)。如下圖:

怎么樣!嘿嘿,雖然功能還顯簡單,但對開發者來說,起碼說看到比較直觀的界面。
操作步驟很簡單,首先導入sqlLite 的DB文件(即File Explorer /data /data/ ),然后進行各種sql操作。
順便寫一下,我常用到的sqlLite操作類,對增刪查改進行了簡單的封裝。
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper {
static private DatabaseHelper mDbHelper;
static private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "zhyy.db";
private static final int DATABASE_VERSION = 1;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public DBHelper(Context ctx) {
this.mCtx = ctx;
}
public DBHelper open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void closeclose() {
mDb.close();
mDbHelper.close();
}
/**
* 插入數據
* 參數:tableName 表名
* initialValues 要插入的列對應值
* */
public long insert(String tableName,ContentValues initialValues) {
return mDb.insert(tableName, null, initialValues);
}
/**
* 刪除數據
* 參數:tableName 表名
* deleteCondition 刪除的條件
* deleteArgs 如果deleteCondition中有“?”號,將用此數組中的值替換
* */
public boolean delete(String tableName,String deleteCondition,String[] deleteArgs) {
return mDb.delete(tableName, deleteCondition, deleteArgs) > 0;
}
/**
* 更新數據
* 參數:tableName 表名
* initialValues 要更新的列
* selection 更新的條件
* selectArgs 如果selection中有“?”號,將用此數組中的值替換
* */
public boolean update(String tableName,ContentValues initialValues,String selection,String[] selectArgs) {
int returnValue = mDb.update(tableName, initialValues, selection, selectArgs);
return returnValue > 0;
}
/**
* 取得一個列表
* 參數:tableName 表名
* columns 返回的列
* selection 查詢條件
* selectArgs 如果selection中有“?”號,將用此數組中的值替換
* */
public Cursor findList(String tableName,String[] columns,String selection,String[] selectionArgs,String groupBy,String having,String orderBy) {
return mDb.query(tableName, columns, selection, selectionArgs, groupBy, having, orderBy);
}
/**
* 取得單行記錄
* 參數:tableName 表名
* columns 返回的列
* selection 查詢條件
* selectArgs 如果selection中有“?”號,將用此數組中的值替換
* */
public Cursor findInfo(String tableName,String[] columns,String selection,String[] selectionArgs,String groupBy,String having,String orderBy,String limit,boolean distinct) throws SQLException {
Cursor mCursor = mDb.query(distinct, tableName, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
/**
* 執行sql
* 參數:sql 要執行的sql
* */
public void execSQL(String sql){
mDb.execSQL(sql);
}
/**
* 判斷某張表是否存在
* @param tabName 表名
* @return
*/
public boolean isTableExist(String tableName){
boolean result = false;
if(tableName == null){
return false;
}
try {
Cursor cursor = null;
String sql = "select count(1) as c from sqlite_master where type ='table' and name ='"+tableName.trim()+"' ";
cursor = mDb.rawQuery(sql, null);
if(cursor.moveToNext()){
int count = cursor.getInt(0);
if(count>0){
result = true;
}
}
cursor.close();
} catch (Exception e) {
// TODO: handle exception
}
return result;
}
/**
* 判斷某張表中是否存在某字段(注,該方法無法判斷表是否存在,因此應與isTableExist一起使用)
*
* @param tabName 表名
* @return
*/
public boolean isColumnExist(String tableName,String columnName){
boolean result = false;
if(tableName == null){
return false;
}
try {
Cursor cursor = null;
String sql = "select count(1) as c from sqlite_master where type ='table' and name ='"+tableName.trim()+"' and sql like '%"+ columnName.trim() +"%'" ;
cursor = mDb.rawQuery(sql, null);
if(cursor.moveToNext()){
int count = cursor.getInt(0);
if(count>0){
result = true;
}
}
cursor.close();
} catch (Exception e) {
// TODO: handle exception
}
return result;
}
}好吧,也順便寫一下各種增刪查改的sql。
package com.android.mission.test;
import com.android.mission.util.DBHelper;
import android.content.ContentValues;
import android.database.Cursor;
import android.test.AndroidTestCase;
import android.util.Log;
/**
* 單元測試操作sqlLite的各種sql
*/
public class testSqlLite extends AndroidTestCase{
/**
* 創建表
* @throws Exception
*/
public void createTable() throws Exception{
DBHelper dbHelper = new DBHelper(this.getContext());
dbHelper.open();
String deleteSql = "drop table if exists user ";
dbHelper.execSQL(deleteSql);
//id是自動增長的主鍵,username和 password為字段名, text為字段的類型
String sql = "CREATE TABLE user (id integer primary key autoincrement, username text, password text)";
dbHelper.execSQL(sql);
dbHelper.closeclose();
}
/**
* 插入數據
* @throws Exception
*/
public void insert() throws Exception{
DBHelper dbHelper = new DBHelper(this.getContext());
dbHelper.open();
ContentValues values = new ContentValues(); //相當于map
values.put("username", "test");
values.put("password", "123456");
dbHelper.insert("user", values);
dbHelper.closeclose();
}
/**
* 更新數據
* @throws Exception
*/
public void update() throws Exception{
DBHelper dbHelper = new DBHelper(this.getContext());
dbHelper.open();
ContentValues initialValues = new ContentValues();
initialValues.put("username", "changename"); //更新的字段和值
dbHelper.update("user", initialValues, "id = '1'", null); //第三個參數為 條件語句
dbHelper.closeclose();
}
/**
* 刪除數據
* @throws Exception
*/
public void delete() throws Exception{
DBHelper dbHelper = new DBHelper(this.getContext());
dbHelper.open();
String testId = "1";
dbHelper.delete("user", "id = '"+ testId +"'", null);
dbHelper.closeclose();
}
/**
* 增加字段
* @throws Exception
*/
public void addColumn() throws Exception{
DBHelper dbHelper = new DBHelper(this.getContext());
dbHelper.open();
String updateSql = "alter table user add company text";
dbHelper.execSQL(updateSql);
}
/**
* 查詢列表
* @throws Exception
*/
public void selectList()throws Exception{
DBHelper dbHelper = new DBHelper(this.getContext());
dbHelper.open();
Cursor returnCursor = dbHelper.findList("user",new String[] {"id","username", "password"}, "username = 'test'", null,null, null, "id desc");
while(returnCursor.moveToNext()){
String id = returnCursor.getString(returnCursor.getColumnIndexOrThrow("id"));
String username = returnCursor.getString(returnCursor.getColumnIndexOrThrow("username"));
String password = returnCursor.getString(returnCursor.getColumnIndexOrThrow("password"));
}
}
/**
* 某條信息
* @throws Exception
*/
public void selectInfo()throws Exception{
DBHelper dbHelper = new DBHelper(this.getContext());
dbHelper.open();
Cursor returnCursor = dbHelper.findList("user",new String[] {"id","username", "password"}, "id = '1'", null,null, null, "id desc");
if (returnCursor.getCount() > 0) {
returnCursor.moveToFirst();
String id = returnCursor.getString(returnCursor.getColumnIndexOrThrow("id"));
String username = returnCursor.getString(returnCursor.getColumnIndexOrThrow("username"));
String password = returnCursor.getString(returnCursor.getColumnIndexOrThrow("password"));
}
}
}
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!