0

我的大部分 Android 问题都已从该站点得到解答,但我有一个特殊问题我无法找到。我想使用 textview 从 SQLite 数据库中检索记录的各个列。例如,

欢迎 COLUMN1VALUE,根据我们的记录,您住在 COLUMN2VALUE 并且已经在那里生活了 COLUMN3VALUE 年。

任何人都可以指出我的教程方向或给我一个答案吗?谢谢!

这是代码:

      private void fillData(Uri uri) {
            String[] projection = { TodoTable.COLUMN_COREBELIEF,
                TodoTable.COLUMN_EXPERIENCE1,
                TodoTable.COLUMN_EXPERIENCE2,
                TodoTable.COLUMN_EXPERIENCE3,
                TodoTable.COLUMN_NEWBELIEF,};

        //    String query = "SELECT COLUMN_NEWBELIEF,COLUMN_COREBELIEF,COLUMN_EXPERIENCE1 FROM TodoTable";

            Cursor cursor = getContentResolver().query(uri, projection, null, null, null);



    //          Cursor cursor = db.rawQuery(query, null);
                if (cursor != null) {
                      cursor.moveToFirst();
                String s = String.format("Welcome %s, from our records you are living in %s and have been living there for %s years.", cursor.getString(cursor.getColumnIndex("COLUMN_NEWBELIEF")),  cursor.getString(cursor.getColumnIndex("COLUMN_COREBELIEF")), cursor.getString(cursor.getColumnIndex("COLUMN_EXPERIENCE1")));
                textView1.setText(s);


              }



//            mCoreBelief.setText(cursor.getString(cursor
//                .getColumnIndexOrThrow(TodoTable.COLUMN_COREBELIEF)));
//            mExperience1.setText(cursor.getString(cursor
//                .getColumnIndexOrThrow(TodoTable.COLUMN_EXPERIENCE1)));
//            mExperience2.setText(cursor.getString(cursor
//                    .getColumnIndexOrThrow(TodoTable.COLUMN_EXPERIENCE2)));
//            mExperience3.setText(cursor.getString(cursor
//                    .getColumnIndexOrThrow(TodoTable.COLUMN_EXPERIENCE3)));
//            mNewBelief.setText(cursor.getString(cursor
//                    .getColumnIndexOrThrow(TodoTable.COLUMN_NEWBELIEF)));



              // Always close the cursor
              cursor.close();
            }
4

3 回答 3

1

您可以通过以下代码轻松完成此操作:

 String query = "SELECT col1,col2,col3 FROM mytable";
 Cursor c = db.rawQuery(query, null);
 c.moveToFirst();
 String s = String.format("Welcome %s, from our records you are living in %s and have been living there for %s years.", c.getString(c.getColumnIndex("col1")),  c.getString(c.getColumnIndex("col2")), c.getString(c.getColumnIndex("col3")));
 txtView.setText(s);
于 2013-06-15T18:39:17.833 回答
0

您的 SQLite 数据库是否已经存在,或者您是否需要有关如何在 Android 中管理 SQLite 数据库的整个生命周期的指导?

如果您的数据库已经存在,那么 @hrehman 的回答就足够了,否则 Lar Vogel 在http://www.vogella.com/articles/AndroidSQLite/article.html的教程非常棒。

于 2013-06-15T18:45:58.930 回答
0

固定的!!我将您的代码修改如下:

  private void fillData(Uri uri) {
      TextView textView1 = (TextView) findViewById(R.id.textView1);
        String[] projection = { TodoTable.COLUMN_COREBELIEF,
            TodoTable.COLUMN_EXPERIENCE1,
            TodoTable.COLUMN_EXPERIENCE2,
            TodoTable.COLUMN_EXPERIENCE3,
            TodoTable.COLUMN_NEWBELIEF,};
        Cursor cursor = getContentResolver().query(uri, projection, null, null,
            null);
        if (cursor != null) {
          cursor.moveToFirst();
          String s = String.format("Welcome %s, from our records you are living in %s and have been living there for %s years.", 
                  cursor.getString(cursor.getColumnIndexOrThrow(TodoTable.COLUMN_NEWBELIEF)),  cursor.getString(cursor.getColumnIndexOrThrow(TodoTable.COLUMN_COREBELIEF)), cursor.getString(cursor.getColumnIndexOrThrow(TodoTable.COLUMN_EXPERIENCE1)));
          textView1.setText(s);
                      }

谢谢!

于 2013-06-16T13:12:58.393 回答