Answer the question
In order to leave comments, you need to log in
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
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
#INSERT INTO имя_таблицы SET поле1=значение1, поле2=значение2
execute_query('INSERT INTO User SET name="Elon", years=49;')
#SELECT * FROM имя_таблицы
name = execute_query('SELECT name FROM User;')
print(name)
result:
[(Elon,)] # база данных возвращает кортеж массивов, по которым можно итерироваться
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question