玩转Android sqlLite---(附android DB的图行工具)

本文介绍了如何使用SQLLite作为迷你数据库进行基本操作,并通过一个示例类展示了封装的增删查改SQL方法。此外,文章提及了一款图形化工具SQLiteSpy,用于更直观地管理和操作SQLLite数据库。

http://www.open-open.com/lib/view/open1328191244984.html


sqlLite就像个迷你数据库,配套的操作工具还不完善,操作起来不直观。不像oracle、mysql那样有图形化的操作工作。

 

偶然在网上发现一款操作sqlLite的图形化工具  ----  SQLiteSpy(后附上链接)。如下图:

玩转Android sqlLite---(附android DB的图行工具)

 

 怎么样!嘿嘿,虽然功能还显简单,但对开发者来说,起码说看到比较直观的界面。

 

操作步骤很简单,首先导入sqlLite 的DB文件(即File Explorer   /data /data/   ),然后进行各种sql操作。

 

顺便写一下,我常用到的sqlLite操作类,对增删查改进行了简单的封装。

001 import android.content.ContentValues;
002 import android.content.Context;
003 import android.database.Cursor;
004 import android.database.SQLException;
005 import android.database.sqlite.SQLiteDatabase;
006 import android.database.sqlite.SQLiteOpenHelper;
007  
008 public class DBHelper {
009     static private DatabaseHelper mDbHelper;
010     static private SQLiteDatabase mDb;
011  
012     private static final String DATABASE_NAME = "zhyy.db";
013      
014     private static final int DATABASE_VERSION = 1;
015  
016     private final Context mCtx;
017  
018     private static class DatabaseHelper extends SQLiteOpenHelper {
019  
020         DatabaseHelper(Context context) {
021             super(context, DATABASE_NAME, null, DATABASE_VERSION);
022         }
023  
024         @Override
025         public void onCreate(SQLiteDatabase db) {
026              
027         }
028         @Override
029         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
030              
031         }
032     }
033  
034     public DBHelper(Context ctx) {
035         this.mCtx = ctx;
036     }
037  
038     public DBHelper open() throws SQLException {
039         mDbHelper = new DatabaseHelper(mCtx);
040         mDb = mDbHelper.getWritableDatabase();
041         return this;
042     }
043  
044     public void closeclose() {
045          
046         mDb.close();
047         mDbHelper.close();
048     }
049  
050     /**
051      * 插入数据
052      * 参数:tableName 表名
053      * initialValues 要插入的列对应值
054      *   */
055     public long insert(String tableName,ContentValues initialValues) {
056          
057         return mDb.insert(tableName, null, initialValues);
058     }
059  
060      
061     /**
062      * 删除数据
063      * 参数:tableName 表名
064      * deleteCondition 删除的条件
065      * deleteArgs 如果deleteCondition中有“?”号,将用此数组中的值替换
066      *   */
067     public booleandelete(String tableName,String deleteCondition,String[] deleteArgs) {
068          
069         return mDb.delete(tableName, deleteCondition, deleteArgs) > 0;
070     }
071      
072      
073     /**
074      * 更新数据
075      * 参数:tableName 表名
076      * initialValues 要更新的列
077      * selection 更新的条件
078      * selectArgs 如果selection中有“?”号,将用此数组中的值替换
079      *   */
080     public booleanupdate(String tableName,ContentValues initialValues,String selection,String[] selectArgs) {
081         int returnValue = mDb.update(tableName, initialValues, selection, selectArgs);
082          
083         return  returnValue > 0;
084     }
085  
086     /**
087      * 取得一个列表
088      * 参数:tableName 表名
089      * columns 返回的列
090      * selection 查询条件
091      * selectArgs 如果selection中有“?”号,将用此数组中的值替换
092      *   */
093     publicCursor findList(String tableName,String[] columns,String selection,String[] selectionArgs,String groupBy,String having,String orderBy) {
094  
095         returnmDb.query(tableName, columns, selection, selectionArgs, groupBy, having, orderBy);
096     }
097  
098     /**
099      * 取得单行记录
100      * 参数:tableName 表名
101      * columns 返回的列
102      * selection 查询条件
103      * selectArgs 如果selection中有“?”号,将用此数组中的值替换
104      *   */
105     publicCursor findInfo(String tableName,String[] columns,String selection,String[] selectionArgs,String groupBy,String having,String orderBy,String limit,booleandistinct) throws SQLException {
106  
107         Cursor mCursor = mDb.query(distinct, tableName, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
108          
109          
110         if (mCursor != null) {
111             mCursor.moveToFirst();
112         }
113         return mCursor;
114  
115     }
116  
117     /**
118      * 执行sql
119      * 参数:sql 要执行的sql
120       
121      *   */
122     public void execSQL(String sql){
123         mDb.execSQL(sql);
124          
125     }
126      
127     /**
128         * 判断某张表是否存在
129         * @param tabName 表名
130         * @return
131         */
132     public boolean isTableExist(String tableName){
133                boolean result = false;
134                if(tableName == null){
135                        return false;
136                }
137                
138                try {
139                    Cursor cursor = null;
140                        String sql = "select count(1) as c from sqlite_master where type ='table' and name ='"+tableName.trim()+"' ";
141                        cursor = mDb.rawQuery(sql, null);
142                        if(cursor.moveToNext()){
143                                int count = cursor.getInt(0);
144                                if(count>0){
145                                        result = true;
146                                }
147                        }
148                         
149                         
150                        cursor.close();
151                catch (Exception e) {
152                        // TODO: handle exception
153                }               
154                return result;
155        }
156           
157          
158             /**
159             * 判断某张表中是否存在某字段(注,该方法无法判断表是否存在,因此应与isTableExist一起使用)
160             *
161             * @param tabName 表名
162             * @return
163             */
164              public boolean isColumnExist(String tableName,String columnName){
165                    boolean result = false;
166                    if(tableName == null){
167                            return false;
168                    }
169                   
170                    
171                    try {
172                        Cursor cursor = null;
173                            String sql = "select count(1) as c from sqlite_master where type ='table' and name ='"+tableName.trim()+"' and sql like '%"+ columnName.trim() +"%'";
174                            cursor = mDb.rawQuery(sql, null);
175                            if(cursor.moveToNext()){
176                                    int count = cursor.getInt(0);
177                                    if(count>0){
178                                            result = true;
179                                    }
180                            }
181                             
182                             
183                            cursor.close();
184                    catch (Exception e) {
185                            // TODO: handle exception
186                    }               
187                    return result;
188            }
189               
190               
191               
192              
193  
194      
195 }
好吧,也顺便写一下各种增删查改的sql。
001 package com.android.mission.test;
002  
003 import com.android.mission.util.DBHelper;
004  
005 import android.content.ContentValues;
006 import android.database.Cursor;
007 import android.test.AndroidTestCase;
008 import android.util.Log;
009 /**
010  * 单元测试操作sqlLite的各种sql
011  */
012 public class testSqlLite extends AndroidTestCase{
013      
014     /**
015      * 创建表
016      * @throws Exception
017      */
018     public void createTable() throws Exception{
019         DBHelper dbHelper = new DBHelper(this.getContext());
020         dbHelper.open();
021          
022         String deleteSql = "drop table if exists user ";  
023         dbHelper.execSQL(deleteSql);
024          
025          //id是自动增长的主键,username和 password为字段名, text为字段的类型
026         String sql = "CREATE TABLE user (id integer primary key autoincrement, username text, password text)"
027         dbHelper.execSQL(sql);
028         dbHelper.closeclose();
029     }
030      
031     /**
032      * 插入数据
033      * @throws Exception
034      */
035     public void insert() throws Exception{
036         DBHelper dbHelper = new DBHelper(this.getContext());
037         dbHelper.open();
038          
039         ContentValues values =  new ContentValues();  //相当于map
040          
041         values.put("username""test");
042         values.put("password""123456");
043  
044         dbHelper.insert("user", values);
045          
046         dbHelper.closeclose();
047     }
048      
049     /**
050      * 更新数据
051      * @throws Exception
052      */
053     public void update() throws Exception{
054         DBHelper dbHelper = new DBHelper(this.getContext());
055         dbHelper.open();
056         ContentValues initialValues = new ContentValues();
057         initialValues.put("username""changename");  //更新的字段和值
058         dbHelper.update("user", initialValues, "id = '1'"null);   //第三个参数为 条件语句
059          
060         dbHelper.closeclose();
061     }
062      
063      
064     /**
065      * 删除数据
066      * @throws Exception
067      */
068     public void delete() throws Exception{
069         DBHelper dbHelper = new DBHelper(this.getContext());
070         dbHelper.open();
071          
072         String testId = "1";
073         dbHelper.delete("user""id = '"+ testId +"'"null);
074          
075         dbHelper.closeclose();
076     }
077      
078      
079     /**
080      * 增加字段
081      * @throws Exception
082      */
083     public void addColumn() throws Exception{
084         DBHelper dbHelper = new DBHelper(this.getContext());
085         dbHelper.open();
086          
087         String updateSql = "alter table user add company text";
088         dbHelper.execSQL(updateSql);
089     }
090      
091     /**
092      * 查询列表
093      * @throws Exception
094      */
095     public void selectList()throws Exception{
096         DBHelper dbHelper = new DBHelper(this.getContext());
097         dbHelper.open();
098         Cursor returnCursor = dbHelper.findList("user",newString[] {"id","username""password"}, "username = 'test'"null,nullnull"id desc");
099         while(returnCursor.moveToNext()){
100             String id = returnCursor.getString(returnCursor.getColumnIndexOrThrow("id"));
101             String username = returnCursor.getString(returnCursor.getColumnIndexOrThrow("username"));
102             String password = returnCursor.getString(returnCursor.getColumnIndexOrThrow("password"));
103         }
104     }
105      
106     /**
107      * 某条信息
108      * @throws Exception
109      */
110     public void selectInfo()throws Exception{
111         DBHelper dbHelper = new DBHelper(this.getContext());
112         dbHelper.open();
113         Cursor returnCursor = dbHelper.findList("user",newString[] {"id","username""password"}, "id = '1'"null,nullnull"id desc");
114         if (returnCursor.getCount() > 0) {
115             returnCursor.moveToFirst();
116             String id = returnCursor.getString(returnCursor.getColumnIndexOrThrow("id"));
117             String username = returnCursor.getString(returnCursor.getColumnIndexOrThrow("username"));
118             String password = returnCursor.getString(returnCursor.getColumnIndexOrThrow("password"));
119         }
120     }
121 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值