python中sqlite3模块查询数据一条或多条

#导入模块
import sqlite3
#创建链接
con = sqlite3.connect('C:\python_learn\DBA\SQLite3demo\sqlite3demo.db')
#创建游标对象
cur = con.cursor()
#编写sql语句
sql = "select * from  t_person "
#执行语句
try:
    cur.execute(sql)
    #获取结果集
    person_all = cur.fetchall() #获取所有数据
    # person_all = cur.fetchone() #获取一条数据
    for person in person_all:
        print(person)
    print("查询数据成功")
except Exception as e:
    print(e)
    print("查询数据失败")
finally:
    cur.close()
    con.close()
发布了14 篇原创文章 · 获赞 0 · 访问量 156

猜你喜欢

转载自blog.csdn.net/yimaoyingbi/article/details/104323424