如何在Python使用 SQLite3 列出表?
在本文中,我们将讨论如何使用Python列出 SQLite 数据库中的所有表。
使用的数据库:
分步实施:
1.使用connect()方法创建连接对象,
sqliteConnection = sqlite3.connect('SQLite_Retrieving_data.db')
2. 创建了一个 SQL 查询,我们将用它搜索 sqlite3 数据库中存在的所有表的列表。
sql_query = """SELECT name FROM sqlite_master
WHERE type='table';"""
3. 使用连接对象,我们正在创建一个游标对象。
cursor = sqliteConnection.cursor()
4. 使用execute() 方法,我们将执行上面的SQL 查询。
cursor.execute(sql_query)
5. 最后,我们将打印 sqlite3 数据库中存在的所有表的列表。
print(cursor.fetchall())
下面是实现。
Python3
# Importing Sqlite3 Module
import sqlite3
try:
# Making a connection between sqlite3
# database and Python Program
sqliteConnection = sqlite3.connect('SQLite_Retrieving_data.db')
# If sqlite3 makes a connection with python
# program then it will print "Connected to SQLite"
# Otherwise it will show errors
print("Connected to SQLite")
# Getting all tables from sqlite_master
sql_query = """SELECT name FROM sqlite_master
WHERE type='table';"""
# Creating cursor object using connection object
cursor = sqliteConnection.cursor()
# executing our sql query
cursor.execute(sql_query)
print("List of tables\n")
# printing all tables list
print(cursor.fetchall())
except sqlite3.Error as error:
print("Failed to execute the above query", error)
finally:
# Inside Finally Block, If connection is
# open, we need to close it
if sqliteConnection:
# using close() method, we will close
# the connection
sqliteConnection.close()
# After closing connection object, we
# will print "the sqlite connection is
# closed"
print("the sqlite connection is closed")
输出: