将sqlite PRAGMA信息的输出解析为pythonlis

2024-04-19 06:26:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用SQLite命令收集有关列名的信息:

columnsQuery = ("PRAGMA table_info(%s)" % (table))
columnNames = cursor.execute(columnsQuery)

根据我所做的研究,执行这个命令应该会给出给定表中的列名。由于我没有现成的sqlite数据库,所以我不知道PRAGMA的输出是什么样子的。有没有一种方法可以将变量“columnNames”解析为Python列名列表?在


Tags: 方法命令info信息数据库列表executesqlite
1条回答
网友
1楼 · 发布于 2024-04-19 06:26:37

pragma的输出有列和行,就像查询:

$ sqlite3
SQLite version 3.18.0 2017-02-11 14:59:58
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> create table t(id integer primary key, name text not null, date default current_date);
sqlite> .mode columns
sqlite> .header on 
sqlite> pragma table_info(t);            
cid         name        type        notnull     dflt_value  pk        
                                        
0           id          integer     0                       1         
1           name        text        1                       0         
2           date                    0           current_da  0         

相关问题 更多 >