Android SQL数据库从特定表加载数据

时间:2016-04-14 12:30:25

标签: android sqlite listview

我在应用程序启动时创建一个表,并从EditText字符串名称添加更多表。

我的第一个数据库表

public static final String CREATE_ERAMPE_TABLE = "create table details" +
        "(id integer primary key, username text, metre text, northing double," +
        "eastings double,identification text, phone text, date text)";

通过

从活动创建其他表格
SQLiteDatabase db = dbHelper.getWritableDatabase();
    newTableName = dbTableName.getText().toString();
    String sql = "create table "+ newTableName+
            "(id integer primary key, username text, metre text, northing text," +
            "eastings text,identification text, phone text, date text)";
    Log.i("CreateDb=", sql);

将数据插入特定的

  String insertTo = metreUsers.getTableName();

    return database.insert(insertTo, null, cvs);

将所有表名列表添加到列表视图中我使用以下代码

 public ArrayList<RegisteredGroups> getRegisteredGroups(){
    ArrayList<RegisteredGroups> arrTblnames = new ArrayList<RegisteredGroups>();
   // database = dbHelper.getWritableDatabase();
    Cursor c = database.rawQuery("SELECT name FROM sqlite_master WHERE type='table' AND name != 'android_metadata' AND name != 'sqlite_sequence'", null);

    while (c.moveToNext()){
        RegisteredGroups grp = new RegisteredGroups();
        grp.setGroupName(c.getString(c.getColumnIndex("name")));
        arrTblnames.add(grp);
        //get count of entries in every single table
        String countQuery = "SELECT * FROM "+c.getString(c.getColumnIndex("name"));
        Cursor cursor = database.rawQuery(countQuery, null);
        int cnt = cursor.getCount();
        grp.setGroupNumber(String.valueOf(cnt));
    }

现在从这里点击listView上的第一项,其中包含数据库表名,我想将该特定表中的数据加载到listview中。

使用

传递表名
rgGroup = (RegisteredGroups)parent.getItemAtPosition(position);
    if (rgGroup != null){
        Bundle arguments = new Bundle();
        FragmentManager frag = getFragmentManager();
        FragmentTransaction trans = frag.beginTransaction();
        arguments.putParcelable("selectedGroup", rgGroup);
        RegisteredUsersList regs = new RegisteredUsersList();
        regs.setArguments(arguments);

1 个答案:

答案 0 :(得分:0)

public ArrayList<String> loadArtist(){

    ArrayList<String> list= new ArrayList<>();
    SQLiteDatabase database=getReadableDatabase();

    Cursor cursor =  database.query(
        ArtistMaster.ArtistDetails.TABLE_NAME,
        new String[] {ArtistMaster.ArtistDetails.COLUMN_ARTIST_NAME},
        null,
        null,
        null,
        null,
        null
    );

  while(cursor.moveToNext()) {
      list.add(cursor.getString(
         cursor.getColumnIndexOrThrow(
             ArtistMaster.ArtistDetails.COLUMN_ARTIST_NAME)));
  }

  return list;

}