如何将数据库的所有值发送到outputstream [英] How to send all value of database to outputstream

查看:90
本文介绍了如何将数据库的所有值发送到outputstream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为MyDb的SQLiteDatabase对象。这个对象可以通过调用insertLocation方法来填充。



  public  < span class =code-keyword> boolean  insertLocation( String  time, double 纬度,  double 经度){
SQLiteDatabase db = this .getWritableDatabase();
ContentValues cV = new ContentValues();
cV.put( time,time);
cV.put( 纬度,纬度);
cV.put( 经度,经度);
db.insert( location,null,cV);
返回 true;
}





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

解决方案

这完全取决于你想要写它的方式;使用 http://docs.oracle.com/中列出的派生类之一javase / 7 / docs / api / java / io / OutputStream.html [ ^ ]。


您可以将数据写入OutputStream,如下所示:

< pre lang =cs> DataOutputStream output = null ;
尝试 {
output = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream( new 文件( output.dat))));
output.writeUTF(time);
output.writeLong(纬度);
output.writeLong(经度);
} catch (FileNotFoundException e){
// TODO自动生成的捕获块
e.printStackTrace();
} catch (IOException e){
// TODO自动生成的捕获块
e.printStackTrace();
}
最后 {
如果(输出!= null
try {
output.close();
} catch (IOException e){
// TODO自动生成的捕获块
e.printStackTrace();
}
}


I hava a SQLiteDatabase object called MyDb . this object can be filled by calling insertLocation method.

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;
    }



Now i want to put all these data to outputstream , how should i do this?

解决方案

It depends exactly how you want to write it; use one of the derived classes listed at http://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html[^].


You can write the data to OutputStream like this:

DataOutputStream output = null;
        try {
            output = new DataOutputStream(
                    new BufferedOutputStream(
                            new FileOutputStream(new File("output.dat"))));
            output.writeUTF(time);
            output.writeLong(latitude);
            output.writeLong(longitude);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            if (output != null)
                try {
                    output.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }


这篇关于如何将数据库的所有值发送到outputstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆