List the Tables in an SQLite Database (SQLite Show Tables SQL 條列搜尋所有資料表)

List the Tables in an SQLite Database (SQLite Show Tables SQL 條列搜尋所有資料表)

List the Tables in an SQLite Database (SQLite Show Tables SQL 條列搜尋所有資料表)


資料來源: https://database.guide/2-ways-to-list-tables-in-sqlite-database/


C#實測過可用SQL語法 (PS.但是在SQLiteDatabaseBrowserPortable會失敗)

SELECT name FROM sqlite_schema 
WHERE type IN ('table','view') 
AND name NOT LIKE 'sqlite_%'
ORDER BY 1;


應用: C#清空DB所有資料函數

        public static void DBBackupAndClean()//做一次實體DB檔案備份,並清空所有資料表資料
        {
            String DBName = "vpos";
            String StrSourceFilePath = FileLib.path + DBName + ".db";
            String StrDestFilePath = FileLib.path + DBName + DateTime.Now.ToString("_yyyyMMddHHmmss") + ".db";

            File.Copy(StrSourceFilePath, StrDestFilePath, true);

            String SQL = "SELECT name FROM sqlite_schema WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' ORDER BY 1;";//https://database.guide/2-ways-to-list-tables-in-sqlite-database/
            DataTable DataTableBuf = GetDataTable(SQL);
            if((DataTableBuf != null)&& (DataTableBuf.Rows.Count > 0))
            {
                for(int i = 0; i < DataTableBuf.Rows.Count; i++)
                {
                    SQL = String.Format("DELETE FROM {0};", DataTableBuf.Rows[i][0].ToString());
                    SQLiteInsertUpdateDelete(SQL);
                }
            }
        }

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *