Answer the question
In order to leave comments, you need to log in
How to output real time data to sqlite database?
Hello! I want to make a notepad of events for the site parsing program, like "Parsing 1 out of 5 pages is completed ...", I got the idea to use Sqlite to create a database that is updated with events.
I do this in order to display it on a form created in Qt Designer Pyqt5, in textEdit
In a similar way, I assume filling the base with events:
r3 = 'Завершено скачивание данных ' + str(count_page) + ' из ' + str(max_count_pages) + ' страниц'
datasm = (sql_count,r3)
sql_insert(con,datasm)
sql_count =+ 1
Answer the question
In order to leave comments, you need to log in
Here is a simple code that displays data on the page and enters it into the database:
import sqlite3
import time
from datetime import datetime
conn = sqlite3.connect('events.db')
cursor = conn.cursor()
sql = """CREATE TABLE IF NOT EXISTS events(id INTEGER PRIMARY KEY AUTOINCREMENT,
page int,
status text,
date text)"""
cursor.execute(sql)
def add_event(page,status): # Метод добавления в базу: page - номер текущей страници, status - OK или ERROR
date_now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cursor.execute("INSERT INTO events VALUES(?,?,?,?)",(None, page, status, date_now))
conn.commit()
pass
max_page = 5
for page in range(1,max_page+1):
try:
# Тут сам парсинг, или метод парсинга
print(f'Завершено скачивание данных {str(page)} из {str(max_page)} страниц')
add_event(page,'OK')
time.sleep(0.5)
except:
print(f'Ошибка парсинга на странице {page}')
add_event(page,'ERROR')
conn.close()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question