Python使用sqlite3查看表结构与执行查询的完整示例
本文介绍了在Python中如何利用sqlite3模块查看数据库表结构并执行查询操作。通过PRAGMA table_info命令获取表字段信息,结合execute与fetchall方法实现数据检索,提供了完整的代码示例供参考。
Python sqlite3查看表结构与查询示例
具体实现方式如下所示:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sqlite3
conn = sqlite3.connect('test.db')
# 创建一个Cursor:
cursor = conn.cursor()
# 查询记录:
conn = sqlite3.connect('calendar.db')
cursor = conn.cursor()
# 执行查询语句:
cursor.execute('select * from perpetualCalendar')
# 获得查询结果集:
values = cursor.fetchall()
print(values)
# cursor.execute('PRAGMA table_info(perpetualCalendar)')
print (cursor.fetchall())
cursor.close()
conn.close()
要查看指定表的结构信息,可执行以下代码:cursor.execute('PRAGMA table_info(表名)')
以上关于Python中sqlite3查看表结构及查询语句的示例代码已分享完毕,希望能为读者提供有效参考,并欢迎大家继续支持本站获取更多技术内容。
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













