有没有办法从python中获取数据库的架构? [英] Is there a way to get a schema of a database from within python?

查看:68
本文介绍了有没有办法从python中获取数据库的架构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试寻找一种在数据库中查找表名的方法(如果有的话)。我发现从sqlite cli我可以使用:

I'm trying to find out a way to find the names of tables in a database(if any exist). I find that from a sqlite cli I can use:

>.tables

然后输入字段:

>PRAGMA TABLE_INFO(table_name)

这显然在python中不起作用。

This obviously doesn't work within python. Is there even a way to do this with python or should I just be using the sqlite command-line?

推荐答案

应该使用python来做到这一点吗?还是应该使用sqlite命令行?能够从 sqlite_master 表访问表名。

You should be able access the table names from the sqlite_master table.

SELECT name FROM sqlite_master WHERE type='table';

不能直接访问列的名称。获取它们的最简单方法是查询表并从查询结果中获取列名。

The names of the columns are not directly accessible. The easiest way to get them is to query the table and get the column names from the query result.

SELECT * FROM table_name LIMIT 1;

这篇关于有没有办法从python中获取数据库的架构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆