我创建了一个应用程序,该应用程序在sqlite数据库中记录一系列经度和纬度值,并将其在MapActivity上显示为彩色轨道.
现在,我希望能够以某种方式导出此数据(最好是导出到文件),以便用户可以将值上传到显示Google Map API的网站.
我的问题是:将数据(以及文件格式:GPX,XML,CSV)导出到Android设备上的SD卡的最快方法是什么.
非常感谢.
解决方法:
确定,只需在您的AndroID项目中添加此类(并进行相应的修改)
public class DatabaseAssistant{ private static final String EXPORT_file_name = "/sdcard/datanaexport.xml"; private Context _ctx; private sqliteDatabase _db; private Exporter _exporter; public DatabaseAssistant( Context ctx, sqliteDatabase db ) { _ctx = ctx; _db = db; try { // create a file on the sdcard to export the // database contents to file myfile = new file( EXPORT_file_name ); myfile.createNewfile(); fileOutputStream fOut = new fileOutputStream(myfile); bufferedoutputstream bos = new bufferedoutputstream( fOut ); _exporter = new Exporter( bos ); } catch (fileNotFoundException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } } public voID exportData( ) { log( "Exporting Data" ); try { _exporter.startDbExport( _db.getPath() ); // get the tables out of the given sqlite database String sql = "SELECT * FROM sqlite_master"; Cursor cur = _db.rawquery( sql, new String[0] ); Log.d("db", "show tables, cur size " + cur.getCount() ); cur.movetoFirst(); String tablename; while ( cur.getposition() < cur.getCount() ) { tablename = cur.getString( cur.getColumnIndex( "name" ) ); log( "table name " + tablename ); // don't process these two tables since they are used // for Metadata if ( ! tablename.equals( "androID_Metadata" ) && ! tablename.equals( "sqlite_sequence" ) ) { exporttable( tablename ); } cur.movetoNext(); } _exporter.endDbExport(); _exporter.close(); } catch (IOException e) { e.printstacktrace(); } } private voID exporttable( String tablename ) throws IOException { _exporter.starttable(tablename); // get everything from the table String sql = "select * from " + tablename; Cursor cur = _db.rawquery( sql, new String[0] ); int numcols = cur.getColumnCount(); log( "Start exporting table " + tablename );// // logging// for( int IDx = 0; IDx < numcols; IDx++ )// {// log( "column " + cur.getColumnname(IDx) );// } cur.movetoFirst(); // move through the table, creating rows // and adding each column with name and value // to the row while( cur.getposition() < cur.getCount() ) { _exporter.startRow(); String name; String val; for( int IDx = 0; IDx < numcols; IDx++ ) { name = cur.getColumnname(IDx); val = cur.getString( IDx ); log( "col '" + name + "' -- val '" + val + "'" ); _exporter.addColumn( name, val ); } _exporter.endRow(); cur.movetoNext(); } cur.close(); _exporter.endtable(); } private voID log( String msg ) { Log.d( "DatabaseAssistant", msg ); } class Exporter { private static final String CLOSING_WITH_TICK = "'>"; private static final String START_DB = "<export-database name='"; private static final String END_DB = "</export-database>"; private static final String START_table = "<table name='"; private static final String END_table = "</table>"; private static final String START_ROW = "<row>"; private static final String END_ROW = "</row>"; private static final String START_Col = "<col name='"; private static final String END_Col = "</col>"; private bufferedoutputstream _bos; public Exporter() throws fileNotFoundException { this( new bufferedoutputstream( _ctx.openfileOutput( EXPORT_file_name, Context.MODE_WORLD_READABLE ) ) ); } public Exporter( bufferedoutputstream bos ) { _bos = bos; } public voID close() throws IOException { if ( _bos != null ) { _bos.close(); } } public voID startDbExport( String dbname ) throws IOException { String stg = START_DB + dbname + CLOSING_WITH_TICK; _bos.write( stg.getBytes() ); } public voID endDbExport() throws IOException { _bos.write( END_DB.getBytes() ); } public voID starttable( String tablename ) throws IOException { String stg = START_table + tablename + CLOSING_WITH_TICK; _bos.write( stg.getBytes() ); } public voID endtable() throws IOException { _bos.write( END_table.getBytes() ); } public voID startRow() throws IOException { _bos.write( START_ROW.getBytes() ); } public voID endRow() throws IOException { _bos.write( END_ROW.getBytes() ); } public voID addColumn( String name, String val ) throws IOException { String stg = START_Col + name + CLOSING_WITH_TICK + val + END_Col; _bos.write( stg.getBytes() ); } } class importer { }}
不可靠的DatabaseAssistant类
DatabaseAssistant DA = new DatabaseAssistant(myContext, MysqLiteDatabase);
您要导出数据吗???所以…
DA.exportData();
;)希望有帮助!
Jorgesys
总结以上是内存溢出为你收集整理的java-将存储在SQLite数据库中的经度和纬度数据导出到文件中的最简单方法,以便可以通过网站将其导入Google Map API?全部内容,希望文章能够帮你解决java-将存储在SQLite数据库中的经度和纬度数据导出到文件中的最简单方法,以便可以通过网站将其导入Google Map API?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)