Android SQLite數(shù)據(jù)庫中的表詳解
Android SQLite數(shù)據(jù)庫
前言
以前寫PHP的時(shí)候,內(nèi)置了print_r()和var_dump()兩個(gè)函數(shù)用于打印輸出任意類型的數(shù)據(jù)內(nèi)部結(jié)構(gòu),現(xiàn)在做Android的開發(fā),發(fā)現(xiàn)并沒有這種類似的函數(shù),對(duì)于數(shù)據(jù)庫的查看很不方便,于是就寫了一下查看數(shù)據(jù)庫表的方法代碼。
代碼實(shí)現(xiàn)
import java.util.Arrays;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SecondActivity extends Activity {
public static final String TAG = "Debug Info";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((Button)findViewById(R.id.btnQue)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MyDatabaseHelper dbhelper = new MyDatabaseHelper(SecondActivity.this, "BookStore.db", null, 1);
SQLiteDatabase db = dbhelper.getWritableDatabase();
//核心區(qū)
//讀取系統(tǒng)表 sqlite_master
String sql = "select * from sqlite_master";
Cursor cursor = db.rawQuery(sql, null);
//打印表的所有列名
Log.i(TAG, Arrays.toString(cursor.getColumnNames()));
//打印當(dāng)前數(shù)據(jù)庫中的所有表
if (cursor.moveToFirst()) {
do {
String str = "";
for (String item : cursor.getColumnNames()) {
str += item + ": " + cursor.getString(cursor.getColumnIndex(item)) + "\n";
}
Log.i(TAG, str);
} while (cursor.moveToNext());
}
}
});
}
}
功能擴(kuò)展
查看表是否存在
public Boolean tableIsExist(SQLiteDatabase db, String tableName){
boolean result = false;
Cursor cursor = null;
if(tableName == null){
return result;
}
String sql = "select count(*) from sqlite_master where type ='table' and name ='"+tableName.trim()+"'";
cursor = db.rawQuery(sql, null);
if(cursor.moveToNext()){
if(cursor.getInt(0) > 0){
result = true;
}
}
return result;
}
查看數(shù)據(jù)庫中有哪些表
public ArrayList<String> tablesInDB(SQLiteDatabase db){
ArrayList<String> list = new ArrayList<String>();
String sql = "select name from sqlite_master where type='table'";
Cursor cursor = db.rawQuery(sql, null);
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
} while (cursor.moveToNext());
}
return list;
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android使用SQLite數(shù)據(jù)庫的示例
- ASP.net與SQLite數(shù)據(jù)庫通過js和ashx交互(連接和操作)
- SQLite3中文編碼 Python的實(shí)現(xiàn)
- 詳解Python 數(shù)據(jù)庫 (sqlite3)應(yīng)用
- c++獲取sqlite3數(shù)據(jù)庫表中所有字段的方法小結(jié)
- Python解析excel文件存入sqlite數(shù)據(jù)庫的方法
- c#幾種數(shù)據(jù)庫的大數(shù)據(jù)批量插入(SqlServer、Oracle、SQLite和MySql)
- Python Sqlite3以字典形式返回查詢結(jié)果的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)SQLite添加、更新及刪除行的方法
- SQLite Delete詳解及實(shí)例代碼
相關(guān)文章
Android 吸入動(dòng)畫效果實(shí)現(xiàn)分解
如何在Android上面實(shí)現(xiàn)一個(gè)類似的效果。先看看我實(shí)現(xiàn)的效果圖,我們利用了Canvas.drawBitmapMesh()方法,具體實(shí)現(xiàn)代碼如下,感興趣的朋友可以參考下哈2013-06-06
RxJava+Retrofit+OkHttp實(shí)現(xiàn)多文件下載之?dāng)帱c(diǎn)續(xù)傳
本篇文章主要介紹了RxJava+Retrofit+OkHttp實(shí)現(xiàn)多文件下載之?dāng)帱c(diǎn)續(xù)傳,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
如何在Android Studio下進(jìn)行NDK開發(fā)
這篇文章主要介紹了如何在Android Studio下進(jìn)行NDK開發(fā),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Android 中IntentFilter的匹配規(guī)則實(shí)例詳解
這篇文章主要介紹了Android 中IntentFilter的匹配規(guī)則實(shí)例詳解的相關(guān)資料,希望通過本文大家能了解掌握IntentFilter的匹配規(guī)則問題,需要的朋友可以參考下2017-09-09
淺析Android手機(jī)衛(wèi)士自定義控件的屬性
這篇文章主要介紹了淺析Android手機(jī)衛(wèi)士自定義控件的屬性,本文介紹的非常詳細(xì)具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-04-04

