python 数据库中
def opendb():
conn=sqlite3.connect("mydb.db")
cur=conn.execute("create table if not exists tongxunlu(usernum integer primary key,username varchar(128),address varchar(125),telnum varchar(128))")
return cur,conn
def show_all_db():
hel=opendb()
cur=hel[1].cursor()
cur.execute("select * from tongxunlu")
res=cur.fetchall()
for line in res:
print(line)
cur.close()
cur=hel[1].cursor()是什么意思?
版权声明:本文为CSDN博主「ur_ytii」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ur_ytii/article/details/112717816
我在下面给你做了注释,你可以学习一下,如果有不懂的留言或者根据我的解释你查找一下关键字
# 创建数据库对象 hel
hel=opendb()
# 通过数据connection对象hel来创建游标对象 cur
cur=hel[1].cursor()
# 执行sql
cur.execute("select * from tongxunlu")
因为你看的这个博文里面这个 opendb()
方法,返回两个对象,hel[1] 直接返回了创建数据库表tongxunlu的connection对象,并且后面使用了这个conn创建了游标对象
从Python3.x版本开始,在标准库中已经内置了SQLlite3模块,它可以支持SQLite3数据库的访问和相关的数据库操作。在需要操作SQLite3数据库数据时,只须在程序中导入SQLite3模块即可。