日期:2014-05-16  浏览次数:20465 次

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);
  }
 }
}