Python-如何打印Sqlite表 [英] Python - How to print Sqlite table

查看:68
本文介绍了Python-如何打印Sqlite表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我有一些简单的python代码来查询sqlite3数据库.

Here I have some simple python code to query a 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()

我已经创建了表 Posts .运行此程序时,我没有收到任何错误,它只是打印出 [] .我知道Posts表不是空的,我是在REPL中创建的.为什么这不起作用?

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

感谢您的帮助!

推荐答案

使用上下文管理器,因此您不必提交!

Use a context manager, so you don´t have to commit!

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

这篇关于Python-如何打印Sqlite表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆