java 在 Android 中打印 SQLite 数据库的所有行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27003486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing all rows of a SQLite database in Android
提问by A.Wan
For debugging/checkpointing purposes, I wanted to print out all rows of a SQLite database in Android. I did not find anything that answered this precise question: How can I easily print out all rows of a SQLite database in a human-readable format?
出于调试/检查点的目的,我想在 Android 中打印出 SQLite 数据库的所有行。我没有找到任何可以回答这个确切问题的内容:如何以人类可读的格式轻松打印出 SQLite 数据库的所有行?
回答by A.Wan
The solution I came up with looks like this:
我想出的解决方案如下所示:
public class DbHelper extends SQLiteOpenHelper {
String TAG = "DbHelper";
... // functions omitted
/**
* Helper function that parses a given table into a string
* and returns it for easy printing. The string consists of
* the table name and then each row is iterated through with
* column_name: value pairs printed out.
*
* @param db the database to get the table from
* @param tableName the the name of the table to parse
* @return the table tableName as a string
*/
public String getTableAsString(SQLiteDatabase db, String tableName) {
Log.d(TAG, "getTableAsString called");
String tableString = String.format("Table %s:\n", tableName);
Cursor allRows = db.rawQuery("SELECT * FROM " + tableName, null);
if (allRows.moveToFirst() ){
String[] columnNames = allRows.getColumnNames();
do {
for (String name: columnNames) {
tableString += String.format("%s: %s\n", name,
allRows.getString(allRows.getColumnIndex(name)));
}
tableString += "\n";
} while (allRows.moveToNext());
}
return tableString;
}
}
Hope this helps some of you.
希望这对你们中的一些人有所帮助。
回答by Nadav Y.N
I changed it a bit: separated the Cursor toString (can be used independently), designed as a Table print.
我稍微改了一下:将Cursor toString 分开(可以独立使用),设计成Table 打印。
public String tableToString(SQLiteDatabase db, String tableName) {
Log.d("","tableToString called");
String tableString = String.format("Table %s:\n", tableName);
Cursor allRows = db.rawQuery("SELECT * FROM " + tableName, null);
tableString += cursorToString(allRows);
return tableString;
}
public String cursorToString(Cursor cursor){
String cursorString = "";
if (cursor.moveToFirst() ){
String[] columnNames = cursor.getColumnNames();
for (String name: columnNames)
cursorString += String.format("%s ][ ", name);
cursorString += "\n";
do {
for (String name: columnNames) {
cursorString += String.format("%s ][ ",
cursor.getString(cursor.getColumnIndex(name)));
}
cursorString += "\n";
} while (cursor.moveToNext());
}
return cursorString;
}
回答by Ivan Wooll
Another quicker option would be to use a rooted device and then export the app database to your development machine to be opened with a desktop app capable of reading sqlite dbs. There is also an app on Google play called sqlite debugger which you can use to view app databases but this again requires root.
另一个更快的选择是使用有根设备,然后将应用程序数据库导出到您的开发机器,以使用能够读取 sqlite dbs 的桌面应用程序打开。Google Play 上还有一个名为 sqlite 调试器的应用程序,您可以使用它来查看应用程序数据库,但这同样需要 root。