Objective
Before writing PHP, built-in print_r()
and var_dump()
two functions for the printout of any type of data internal structure, now do the development of Android, found that there is no such a function, for the database view is not convenient, so wrote a look at the database table method code.
Code implementation
ImportJava.util.Arrays;Importandroid.app.Activity;ImportAndroid.database.Cursor;ImportAndroid.database.sqlite.SQLiteDatabase;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public class secondactivity extends Activity { Public Static FinalString TAG ="Debug Info";@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); (Button) Findviewbyid (R.id.btnque)). Setonclicklistener (NewOnclicklistener () {@Override Public void OnClick(View v) {Mydatabasehelper DBHelper =NewMydatabasehelper (secondactivity. This,"Bookstore.db",NULL,1); Sqlitedatabase db = Dbhelper.getwritabledatabase ();//Core area //Read system table Sqlite_masterString sql ="SELECT * from Sqlite_master"; cursor cursor = db.rawquery (SQL,NULL);//Print all column names of the tableLOG.I (TAG, Arrays.tostring (Cursor.getcolumnnames ()));//Print all tables in the current database 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 ()); } } }); }}
Feature Extensions
To see if a table exists
PublicBooleantableisexist(Sqlitedatabase db, String tableName) {Booleanresult =false; cursor cursor =NULL;if(TableName = =NULL){returnResult } 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; } }returnResult;}
See which tables are in the database
publictablesInDB(SQLiteDatabase db){ new ArrayList<String>(); "select name from sqlite_master where type=‘table‘"; null); if (cursor.moveToFirst()) { do { list.add(cursor.getString(0)); while (cursor.moveToNext()); } return list;}
Blog name: Wang Leping Blog
Blog Address: http://blog.lepingde.com
CSDN Blog Address: http://blog.csdn.net/lecepin
Android Code Implementation View tables in the SQLite database