2011-05-05 39 views
0

我已经创建了下面给出的complied语句。现在我的问题是如何获得查询的结果集。 这是我的代码:从Android预编译语句中获得结果集

DataBaseHelper dbHelper=new DataBaseHelper(context); 
dbHelper.createDataBase(); 
dbHelper.openDataBase(); 
SQLiteDatabase db = dbHelper.getWritableDatabase(); 
SQLiteStatement st=db.compileStatement("select taskid from task where taskdate=?"); 
st.bindString(1,"2011/09/05"); 
st.execute(); 

这个工程没有任何错误。但是我想要给定查询的结果集。请帮助..

回答

2

结果集不可用,至少现在,在sqlite中。这一切完全取决于您想要从ResultSet或ResultSetMetaData等获得的信息,但是还有其他方法可以获得几乎相同的信息。

你可以得到有关在表中的列,分别用作好像它是一个SELECT的详细信息,以及有关列的信息也会出现:

pragma table_info(myTable) ; 

更多信息请参见http://www.sqlite.org/pragma.html#pragma_table_info

如果您需要关于特定SELECT的信息,您可以从生成的光标中获取信息。请参阅http://developer.android.com/reference/android/database/Cursor.html

例如,如果您需要列的数据类型,可以在更新版本的Android中使用getType()方法,或使用一系列“get”函数来确定至少是哪种类型是可读的,与这个可怕的代码:

  Cursor curs = db.rawQuery(sqlStr, null); 
      int numberOfColumns = curs.getColumnCount(); 
      String []colNames = new String[numberOfColumns]; 
      String []colTypes = new String[numberOfColumns]; 
      for(int iCol=1; iCol<=numberOfColumns; iCol++) { 
       colNames[iCol-1] = curs.getColumnName(iCol-1); 
       colTypes[iCol-1] = null; //curs.getType(iCol); 
      } 
      while(curs.moveToNext()) { 
       // this code assumes that the first row has the same data types 
       // as the rest of the rows 
       for(int iCol=1; iCol<=numberOfColumns; iCol++) { 
        String colName = colNames[iCol-1]; 
        String colType = colTypes[iCol-1]; 
        if(colType==null) { 
         // determine column type 
         try { 
          curs.getString(iCol-1); 
          colType = colTypes[iCol-1] = "text"; 
         } catch (Exception ignore) { 
          try { 
           curs.getLong(iCol-1); 
           colType = colTypes[iCol-1] = "integer"; 
          } catch (Exception ignore1) { 
           try { 
            curs.getFloat(iCol-1); 
            colType = colTypes[iCol-1] = "real"; 
           } catch (Exception ignore2) { 
            try { 
             curs.getBlob(iCol-1); 
             colType = colTypes[iCol-1] = "blob"; 
            } catch (Exception ignore3) { 
             colType = colTypes[iCol-1] = "other"; 
            } 
           } 
          } 
         } 
        } 
        if("text".equals(colType)) { 
         ... curs.getString(iCol-1); 
        } else 
        if("real".equals(colType)) { 
         ... curs.getDouble(iCol-1); 
        } else 
        if("integer".equals(colType)) { 
         ... curs.getInt(iCol-1); 
        } else { // unknown type 
         ... colType+"-"+curs.getString(iCol-1); 
        } 
       } 
      } 

其他信息可用类似的方式,根据您的需要。

+0

我试过这段代码,getString适用于每一列,所以一切都以字符串形式加载。 – bgolson 2013-02-19 21:20:24

相关问题