H
H
Hideo0022021-06-11 14:27:55
Python
Hideo002, 2021-06-11 14:27:55

How to add information to the database?

I'm checking the ping and I need to write the information to sqlite3, but since I'm new to working with databases, I don't understand how to transfer the ping output information to the database.
An example code for checking the ping itself:

from pythonping import ping

ping('google.com', timeout=60, count=60, interval=1, verbose=True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
CheXnik, 2021-06-11
@CheXnik

This code can both write something to the database and read from it

def execute_query(query):
    db_path = os.path.join(os.getcwd(), 'db.db')
    conn = sqlite3.connect(db_path)
    cur = conn.cursor()
    cur.execute(query)
    result = cur.fetchall()
    conn.commit()
    conn.close()
    return result

Example:
Write:
#INSERT INTO имя_таблицы SET поле1=значение1, поле2=значение2
execute_query('INSERT INTO User SET name="Elon", years=49;')

Reading:
#SELECT * FROM имя_таблицы
name = execute_query('SELECT name FROM User;')
print(name)

result:
[(Elon,)] # база данных возвращает кортеж массивов, по которым можно итерироваться

old.code.mu/sql/select.html - site with sqlite3 commands

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question