Android Code Implementation View tables in the SQLite database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.