sqlLite就像个迷你数据库,配套的操作工具还不完善,操作起来不直观。不像oracle、mysql那样有图形化的操作工作。
偶然在网上发现一款操作sqlLite的图形化工具 ---- SQLiteSpy(后附上链接)。如下图:

怎么样!嘿嘿,虽然功能还显简单,但对开发者来说,起码说看到比较直观的界面。
操作步骤很简单,首先导入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; |
008 | public class DBHelper { |
009 |
static private DatabaseHelper mDbHelper; |
010 |
static private SQLiteDatabase mDb; |
012 |
private static final String DATABASE_NAME = "zhyy.db" ; |
014 |
private static final int DATABASE_VERSION = 1 ; |
016 |
private final Context mCtx; |
018 |
private static class DatabaseHelper extends SQLiteOpenHelper { |
020 |
DatabaseHelper(Context context) { |
021 |
super (context, DATABASE_NAME, null , DATABASE_VERSION); |
025 |
public void onCreate(SQLiteDatabase db) { |
029 |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
034 |
public DBHelper(Context ctx) { |
038 |
public DBHelper open() throws SQLException { |
039 |
mDbHelper = new DatabaseHelper(mCtx); |
040 |
mDb = mDbHelper.getWritableDatabase(); |
044 |
public void closeclose() { |
053 |
* initialValues 要插入的列对应值 |
055 |
public long insert(String tableName,ContentValues initialValues) { |
057 |
return mDb.insert(tableName, null , initialValues); |
064 |
* deleteCondition 删除的条件 |
065 |
* deleteArgs 如果deleteCondition中有“?”号,将用此数组中的值替换 |
067 |
public boolean delete(String tableName,String deleteCondition,String[] deleteArgs) { |
069 |
return mDb.delete(tableName, deleteCondition, deleteArgs) > 0 ; |
078 |
* selectArgs 如果selection中有“?”号,将用此数组中的值替换 |
080 |
public boolean update(String tableName,ContentValues initialValues,String selection,String[] selectArgs) { |
081 |
int returnValue = mDb.update(tableName, initialValues, selection, selectArgs); |
083 |
return returnValue > 0 ; |
091 |
* selectArgs 如果selection中有“?”号,将用此数组中的值替换 |
093 |
public Cursor findList(String tableName,String[] columns,String selection,String[] selectionArgs,String groupBy,String having,String orderBy) { |
095 |
return mDb.query(tableName, columns, selection, selectionArgs, groupBy, having, orderBy); |
103 |
* selectArgs 如果selection中有“?”号,将用此数组中的值替换 |
105 |
public Cursor findInfo(String tableName,String[] columns,String selection,String[] selectionArgs,String groupBy,String having,String orderBy,String limit, boolean distinct) throws SQLException { |
107 |
Cursor mCursor = mDb.query(distinct, tableName, columns, selection, selectionArgs, groupBy, having, orderBy, limit); |
110 |
if (mCursor != null ) { |
111 |
mCursor.moveToFirst(); |
122 |
public void execSQL(String sql){ |
132 |
public boolean isTableExist(String tableName){ |
133 |
boolean result = false ; |
134 |
if (tableName == 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 ); |
151 |
} catch (Exception e) { |
159 |
* 判断某张表中是否存在某字段(注,该方法无法判断表是否存在,因此应与isTableExist一起使用) |
164 |
public boolean isColumnExist(String tableName,String columnName){ |
165 |
boolean result = false ; |
166 |
if (tableName == 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 ); |
184 |
} catch (Exception e) { |
好吧,也顺便写一下各种增删查改的sql。
001 | package com.android.mission.test; |
003 | import com.android.mission.util.DBHelper; |
005 | import android.content.ContentValues; |
006 | import android.database.Cursor; |
007 | import android.test.AndroidTestCase; |
008 | import android.util.Log; |
012 | public class testSqlLite extends AndroidTestCase{ |
018 |
public void createTable() throws Exception{ |
019 |
DBHelper dbHelper = new DBHelper( this .getContext()); |
022 |
String deleteSql = "drop table if exists user " ; |
023 |
dbHelper.execSQL(deleteSql); |
026 |
String sql = "CREATE TABLE user (id integer primary key autoincrement, username text, password text)" ; |
027 |
dbHelper.execSQL(sql); |
028 |
dbHelper.closeclose(); |
035 |
public void insert() throws Exception{ |
036 |
DBHelper dbHelper = new DBHelper( this .getContext()); |
039 |
ContentValues values = new ContentValues(); |
041 |
values.put( "username" , "test" ); |
042 |
values.put( "password" , "123456" ); |
044 |
dbHelper.insert( "user" , values); |
046 |
dbHelper.closeclose(); |
053 |
public void update() throws Exception{ |
054 |
DBHelper dbHelper = new DBHelper( this .getContext()); |
056 |
ContentValues initialValues = new ContentValues(); |
057 |
initialValues.put( "username" , "changename" ); |
058 |
dbHelper.update( "user" , initialValues, "id = '1'" , null ); |
060 |
dbHelper.closeclose(); |
068 |
public void delete() throws Exception{ |
069 |
DBHelper dbHelper = new DBHelper( this .getContext()); |
073 |
dbHelper.delete( "user" , "id = '" + testId + "'" , null ); |
075 |
dbHelper.closeclose(); |
083 |
public void addColumn() throws Exception{ |
084 |
DBHelper dbHelper = new DBHelper( this .getContext()); |
087 |
String updateSql = "alter table user add company text" ; |
088 |
dbHelper.execSQL(updateSql); |
095 |
public void selectList() throws Exception{ |
096 |
DBHelper dbHelper = new DBHelper( this .getContext()); |
098 |
Cursor returnCursor = dbHelper.findList( "user" , new String[] { "id" , "username" , "password" }, "username = 'test'" , null , null , null , "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" )); |
110 |
public void selectInfo() throws Exception{ |
111 |
DBHelper dbHelper = new DBHelper( this .getContext()); |
113 |
Cursor returnCursor = dbHelper.findList( "user" , new String[] { "id" , "username" , "password" }, "id = '1'" , null , null , null , "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" )); |