SQLite Schema Information Metadata

后端 未结 7 1046
终归单人心
终归单人心 2020-11-27 03:46

I need to get column names and their tables in a SQLite database. What I need is a resultset with 2 columns: table_name | column_name.

In MySQL, I\'m a

7条回答
  •  臣服心动
    2020-11-27 04:09

    Another useful trick is to first get all the table names from sqlite_master.

    Then for each one, fire off a query "select * from t where 1 = 0". If you analyze the structure of the resulting query - depends on what language/api you're calling it from - you get a rich structure describing the columns.

    In python

    c = ...db.cursor()
    c.execute("select * from t where 1=0");
    c.fetchall();
    print c.description;
    

    Juraj

    PS. I'm in the habit of using 'where 1=0' because the record limiting syntax seems to vary from db to db. Furthermore, a good database will optimize out this always-false clause.

    The same effect, in SQLite, is achieved with 'limit 0'.

提交回复
热议问题