Python - How to print a Sqlite table

Here I have simple python code to query sqlite3 database.

import sqlite3 as lite conn = lite.connect('db/posts.db') cur = conn.cursor() def get_posts(): cur.execute("SELECT * FROM Posts") print(cur.fetchall()) get_posts() 

I have already created a Posts table. When I run this, I get no errors and it just prints [] . I know that the Posts table is not empty, I created it in REPL. Why is this not working?

Any help is appreciated!

+4
source share
2 answers

Turns out I just forgot to use conn.commit() . Hope this helps someone.

+4
source

Use a context manager, so you donโ€™t have to make transactions.

 def get_posts(): with conn: cur.execute("SELECT * FROM Posts") print(cur.fetchall()) 
+1
source

Source: https://habr.com/ru/post/1485345/


All Articles