L
L
Lim_Drake2020-12-15 11:14:54
Python
Lim_Drake, 2020-12-15 11:14:54

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

Is it possible to make queries that would display events in real time, that is, as soon as the filling has occurred, display the data on the screen? If possible, please share!
I apologize in advance, this is just my idea and I have only superficial knowledge in this area.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2020-12-15
@hottabxp

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()

PS: Initially, there was no question about Pyqt5.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question