zoukankan      html  css  js  c++  java
  • How do I list all tables/indices contained in an SQLite database

    How do I list all tables/indices contained in an SQLite database

    If you are running the sqlite3 command-line access program you can type ".tables" to get a list of all tables. Or you can type ".schema" to see the complete database schema including all tables and indices. Either of these commands can be followed by a LIKE pattern that will restrict the tables that are displayed.

    From within a C/C++ program (or a script using Tcl/Ruby/Perl/Python bindings) you can get access to table and index names by doing a SELECT on a special table named "SQLITE_MASTER". Every SQLite database has an SQLITE_MASTER table that defines the schema for the database. The SQLITE_MASTER table looks like this:

    CREATE TABLE sqlite_master (
      type TEXT,
      name TEXT,
      tbl_name TEXT,
      rootpage INTEGER,
      sql TEXT
    );
    

    For tables, the type field will always be 'table' and the name field will be the name of the table. So to get a list of all tables in the database, use the following SELECT command:

    SELECT name FROM sqlite_master
    WHERE type='table'
    ORDER BY name;
    

    For indices, type is equal to 'index', name is the name of the index and tbl_name is the name of the table to which the index belongs. For both tables and indices, the sql field is the text of the original CREATE TABLE or CREATE INDEX statement that created the table or index. For automatically created indices (used to implement the PRIMARY KEY or UNIQUE constraints) the sql field is NULL.

    The SQLITE_MASTER table is read-only. You cannot change this table using UPDATE, INSERT, or DELETE. The table is automatically updated by CREATE TABLE, CREATE INDEX, DROP TABLE, and DROP INDEX commands.

    Temporary tables do not appear in the SQLITE_MASTER table. Temporary tables and their indices and triggers occur in another special table named SQLITE_TEMP_MASTER. SQLITE_TEMP_MASTER works just like SQLITE_MASTER except that it is only visible to the application that created the temporary tables. To get a list of all tables, both permanent and temporary, one can use a command similar to the following:

    SELECT name FROM 
       (SELECT * FROM sqlite_master UNION ALL
        SELECT * FROM sqlite_temp_master)
    WHERE type='table'
    ORDER BY name
    
  • 相关阅读:
    基于Dapper的开源Lambda扩展,且支持分库分表自动生成实体基础
    用SignalR和Layui搭建自己的web聊天网站
    MySQL查看、修改字符集及Collation
    ASP.NET MVC中有四种过滤器类型
    一个简单的大转盘抽奖程序(附.NetCore Demo源码)
    网站统计中的访问信息收集的前端实现
    微信小程序实现按首字母检索城市列表
    微信小程序异步处理
    微信小程序下拉框
    node.js上传文件
  • 原文地址:https://www.cnblogs.com/zhoug2020/p/4189877.html
Copyright © 2011-2022 走看看