Java 将SQLite表数据导出为XML
我在尝试将SQLite数据库的表导出到XML文件时遇到空指针异常 StackTrace:PasteBin --Java 将SQLite表数据导出为XML,java,android,xml,sqlite,android-sqlite,Java,Android,Xml,Sqlite,Android Sqlite,我在尝试将SQLite数据库的表导出到XML文件时遇到空指针异常 StackTrace:PasteBin --更新2: 我只是不知道传入了什么(在主活动中初始化和调用exportData方法时):DatabaseAssistant DA=newdatabaseassistant(myContext,**此处传入了什么?**);DA.exportData()-- 更新: 我在获得NPE的地方提到的行是:在com.astix.reachout.DatabaseAssistant.exportData
更新2:
我只是不知道传入了什么(在主活动中初始化和调用exportData方法时):
DatabaseAssistant DA=newdatabaseassistant(myContext,**此处传入了什么?**);DA.exportData()代码>
--
更新:
我在获得NPE的地方提到的行是:在com.astix.reachout.DatabaseAssistant.exportData(DatabaseAssistant.java:72
&com.astix.reachout.ReachOutMain$9.onClick(ReachOutMain.java:347)
哪些是\u exporter.startdbeexport(_db.getPath());
可在DA.exportData()中找到
--
我提到。
我的数据库助理代码:
public class DatabaseAssistant
{
public String filNameFullPath;
public long filNameTS;
public String EXPORT_FILE_NAME;
//private static final String EXPORT_FILE_NAME = "/sdcard/datanaexport.xml";
private Context _ctx;
private SQLiteDatabase _db;
private Exporter _exporter;
public String newfilename(){
System.out.println("inside newfilename()");
filNameTS = System.currentTimeMillis();
filNameFullPath = Environment.getExternalStorageDirectory().getPath();
EXPORT_FILE_NAME = filNameFullPath + "/" + filNameTS +".xml";
System.out.println("new file name: " + EXPORT_FILE_NAME);
return EXPORT_FILE_NAME;
}
public DatabaseAssistant( Context ctx, SQLiteDatabase db )
{
_ctx = ctx;
_db = db;
newfilename();
try
{
System.out.println("inside try databaseAssitant() -- file name: " + EXPORT_FILE_NAME);
// 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
{
System.out.println("inside try exportData()");
_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
{
}
}
此处^(以上声明为):
任何建议都是可以接受的。。
谢谢无论是\u exporter
还是\u db
都是null
您的构造函数初始化这两个变量,因此提供给构造函数的mySQLiteDatabase
可能已经是null
。
(尽管如果构造函数中发生任何错误,您将得到一个无效的\u exporter
;但如果只是抑制异常并继续进行,就好像什么都没有发生一样,这是一个坏主意。这里不是这种情况,因为没有来自该异常的堆栈跟踪。)堆栈跟踪中提到的是哪一行?@CL.Hi,谢谢您的回复。我在获得NPE的地方提到的行是:com.astix.reachout.DatabaseAssistant.exportData(DatabaseAssistant.java:72&com.astix.reachout.ReachOutMain$9.onClick(ReachOutMain.java:347),它们是\u exporter.startdbeexport(\u db.getPath())
可在DA.exportData()中找到。
此信息不属于注释,而是属于您的问题。@CL.StackTrace提到了相同的内容。请参阅问题描述中的链接。:|将用此信息更新我的问题。也:)更新请查看我更新的问题。因此有两个怀疑:_exporter,_db。其中一个或两个都为null。您能检查它们吗?+1是的,_db为null。我怀疑这是由于在我的主要活动中传递了不正确的参数,如:/(单击按钮后调用):DatabaseAssistant DA=new DatabaseAssistant(myContext,mySQLiteDatabase);DA.exportData();
//以上声明为:private Context myContext;private SQLiteDatabase mySQLiteDatabase;
实际上根据DatabaseAssistant DA=new DatabaseAssistant(myContext,mySQLiteDatabase);
(续)什么是mySQLiteDatabase);应该是吗?我尝试在DBAdapter类中设置一个对象来处理所有的数据库访问,但没有帮助。此外,我尝试了数据库的名称,但也不起作用。我知道这是一个愚蠢的问题,但我被卡住了:|(这也是我在该答案中发布的注释,但无法得到答复)我只是不知道传递了什么:DatabaseAssistant DA=newdatabaseassistant(myContext,**这里传递了什么?**);DA.exportData();
请参阅。+1从上面的链接可以使用您定义的构造函数获取SQLiteOpenHelper实现的实例。要向数据库写入和读取数据,请调用getWritableDatabase()和getReadableDatabase()它们都返回一个代表数据库的SQLiteDatabase对象,并为SQLite操作提供方法。
DatabaseAssistant DA = new DatabaseAssistant(myContext, mySQLiteDatabase);
DA.exportData();
private Context myContext;
private SQLiteDatabase mySQLiteDatabase;