Как вывести базу данных(Python)?
from datetime import datetime
import sqlite3
__connection = None
def get_connection():
global __connection
if __connection is None:
__connection = sqlite3.connect('anketa.db')
return __connection
def init_db(force: bool = False):
conn = get_connection()
c = conn.cursor()
if force:
c.execute('DROP TABLE IF EXISTS user_message')
c.execute('''
CREATE TABLE IF NOT EXISTS user_message (
id INTEGER PRIMARY KEY,
user_id INTEGER NOT NULL,
text TEXT NOT NULL,
date DATETIME NOT NULL
)
''')
conn.commit()
def read_table(records):
conn = get_connection()
c = conn.cursor()
c.execute('SELECT * FROM user_message')
records = c.fetchall()
for row in records:
print("id:", row[0])
print("user_id:", row[1])
print("text:", row[2])
print("date:", row[3])
print(end="\n")
if __name__ == '__main__':
init_db()
read_table()
Что я делаю не так? Ошибка: TypeError: read_table() missing 1 required positional argument: 'records'
Делаю через функции, так как потом я вызываю ее в главном файле для вывода в телеграм.
Ответы (1 шт):
Автор решения: nomnoms12
→ Ссылка
Удалите параметр records у функции read_table:
def read_table():
...
Подробнее
Ваша ошибка переводится как
ОшибкаТипа: read_table() не принимает 1 обязательный позиционный аргумент: 'records'
Формальным исправлением было бы передать его:
if __name__ == '__main__':
init_db()
records = ...
read_table(records)
Но в самой функции Вы не используете его, а переопределяете результатом вызова c.fetchall():
def read_table(records):
...
records = c.fetchall()
Поэтому удаление параметра не изменит поведение функции и устранит ошибку.