1. 程式人生 > >Android--遍歷SQLite資料庫下的所有表名

Android--遍歷SQLite資料庫下的所有表名

package cn.mrzhu.foreignkey;  
  
import android.app.Activity;  
import android.content.Context;  
import android.database.Cursor;  
import android.database.sqlite.SQLiteDatabase;  
import android.os.Bundle;  
import android.util.Log;  
  
public class ForeignKeyActivity extends Activity {  
 String[] arr = {"table1", "table2", "table3"};  
 /** Called when the activity is first created. */  
 @Override  
 public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.main);  
  //建立資料庫  
  SQLiteDatabase db = openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);  
  for(int i = 0; i < 3; ++i){  
   //迴圈建立三張表  
   db.execSQL("create table'" + arr[i] + "'(id varchar(20), name varchar(20))");  
  }  
  Cursor cursor = db.rawQuery("select name from sqlite_master where type='table' order by name", null);  
  while(cursor.moveToNext()){  
   //遍歷出表名  
   String name = cursor.getString(0);  
   Log.i("System.out", name);  
  }  
 }  
}