我在java应用程序中使用sqlite3数据库。如何以编程方式获取数据库中所有表的名称?我也尝试过谷歌和 stackoverflow,但目前还找不到简单的解决方案。这是我与 PHP 一起使用的 MySqli 代码,我正在 Java-Sqlite3 中尝试类似的方法
mysqli_select_db($conn, $database);
$res = mysqli_query($conn, "SHOW TABLES FROM " . $database . "");
while($cRow = mysqli_fetch_array($res, MYSQL_NUM)) {
$namm=$cRow[0];
$query = "SELECT COUNT(*) FROM " . $namm . "";
$result = mysqli_query($conn,$query);
$rows = mysqli_fetch_row($result);
$namk = $rows[0];
echo "<li type='square' style='margin-bottom:3px;'>" . $cRow[0] . " (" . $namk . ")</li>";
}
mysqli_close($conn);
您可以使用 JDBC 的内置 API 来探索数据库元数据:
try (Connection conn = DriverManager.getConnection("jdbc:sqlite:sample.db")) {
ResultSet rs = conn.getMetaData().getTables(null, null, null, null));
while (rs.next()) {
System.out.println(rs.getString("TABLE_NAME"));
}
}
ArrayList<String> tableNames = new ArrayList<String>();
SqlHelper sqlHelper = new SqlHelper(this, "MYSQLITE.db", null, 1);
SQLiteDatabase sqliteDatabase = sqlHelper.getWritableDatabase();
Cursor cursor = sqliteDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
while(cursor.moveToNext()){
String tableName = cursor.getString(0);
if(tableName.equals("android_metadata")){
continue;
}else{
tableNames.add(tableName);
}
}