【问题标题】:Access the data from database and put them into String[]访问数据库中的数据并将它们放入 String[]
【发布时间】:2023-03-28 19:12:01
【问题描述】:

我在数据库中建立了一个表,现在我想访问某个列中的所有值。最后,我想将数据放入byte[]。 我的部分代码:

db.execSQL("create table thing(id integer primary key" +
            " autoincrement, name varchar(20))");

List<Integer> all = new ArrayList<Integer>();       
String sql = " SELECT id from " + DB_NAME;
Cursor result = this.db.rawQuery(sql, null);

for (result.moveToFirst(); result.isAfterLast(); result.moveToNext()) {
    all.add(result.getInt(0));          
}

String[] fstr = (String[]) all.toArray();
for (String bstr : fstr) {
    byte[] bbs = bstr.getBytes();
}

【问题讨论】:

    标签: android string sqlite list


    【解决方案1】:

    使用此代码

    String[] fstr = new String[result.getCount()] ; 
    
        do {
        int posi = result.getPosition();
         fstr[posi] =result.getString(0);                           
        }while (result.moveToNext());
    

    【讨论】:

      【解决方案2】:

      试试这个

      Cursor c=this.db.rawQuery(sql, null);
      if(c.getCount()>0)
      {
         for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
         {
           String str=c.getInt(0));// or c.getString();
         }
      }
      
      else
      {
        Log.d(" Null value in cursor ", " null ");
      }
      c.close();
      dbhelper.close();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-01
        • 1970-01-01
        • 2012-06-16
        • 1970-01-01
        • 2011-09-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多