2014-10-26 34 views
0

我有一个名为MyDb的SQLiteDatabase对象。这个对象可以通过调用insertLocation方法来填充。如何将数据库的所有值发送到输出流

public boolean insertLocation(String time, double latitude, double longitude) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues cV = new ContentValues(); 
     cV.put("time", time); 
     cV.put("latitude", latitude); 
     cV.put("longitude", longitude); 
     db.insert("location", null, cV); 
     return true; 
    } 

现在我想把所有这些数据输出到outputstream,我应该怎么做?

回答

0

返回

PrintWriter printWriter = new PrintWriter(file); 
printWriter.println("time=" + time); 
printWriter.println("latitude=" +latitude); 
printWriter.println("longitude=" + longitude); 

我希望这能帮助之前!

0

是否要在outputStream中发送数据库中的所有条目?

我试图找到更好的方法,但这是我能想出:

public void saveDatabaseAsTextFile() { 

    // Get a cursor of the data 
    Cursor c = doYourQuery(); 
    if (c.moveToFirst()) { 
     try { 
      OutputStreamWriter writer = new OutputStreamWriter(
       openFileOutput("locations.txt", Context.MODE_PRIVATE)); 
      // For each position of the cursor, create a comma 
      // delimited string (with a new line char at the end). 
      String line; 
      do { 
       line = ""; 
       line = line.concat(c.getString(c 
         .getColumnIndex("time")) + ","); 
       line = line.concat(c.getDouble(c 
         .getColumnIndex("latitude")) + ","); 
       line = line.concat(c.getDouble(c 
         .getColumnIndex("longitude")) + "\n"); 
       // send the string to the OutputStreamWriter 
       writer.write(line); 
      } while (c.moveToNext()); 
      // close the OutputStreamWriter 
      writer.close(); 
     } catch (IOException e) { 
      Log.e("Exception", "File write failed: " + e.toString()); 
     } 
    } 
} 

如果你不想写入文件,抛出该字符串在一些其他的OutputStream。

相关问题