python 连接sqlite及简单操作

yipeiwu_com6年前Python基础

废话不多说了,直接给大家贴代码了,具体代码如下所示:

import sqlite3
#查询
def load(table):
  #连接数据库
  con = sqlite3.connect("E:/Datebase/SQLiteStudio/Park.db")
   #获得游标
  cur = con.cursor()
  #查询整个表
  cur.execute('select *from '+table)
  lists = ['name','password']
  if table == 'login':
    #将数据库列名存入字典
    colnames = {desc[0] for desc in cur.description}
    将字典和数据库的数据一起存入列表,获得了记录字典
    rowdicts = [dict(zip(lists, row)) for row in cur.fetchall()]
  else:
    rowdicts = []
    for row in cur:
      rowdicts.append(row)
  con.commit()
  cur.close()
  return rowdicts
#插入数据
def insert_data(ID,name,money):
  con = sqlite3.connect("E:/Datebase/SQLiteStudio/Park.db")
  cur = con.cursor()
  #使用SQL语句插入
  cur.execute('insert into Charge values (?,?,?)', (ID,name, money))
  #插入后进行整表查询,看是否成功插入
  cur.execute('select *from Charge')
  print(cur.fetchall())
  con.commit()
  cur.close()

以上所述是小编给大家介绍的python 连接sqlite及简单操作,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

Python的动态重新封装的教程

让我们描绘一下本文的情节:假设您要在本地机器上运行一个进程,而部分程序逻辑却在另一处。让我们特别假设这个程序逻辑会不时更新, 而您运行进程时,希望使用最新的程序逻辑。有许多方法可以满足刚...

python 读取视频,处理后,实时计算帧数fps的方法

实时计算每秒的帧数 cap = cv2.VideoCapture("DJI_0008.MOV") #cap = cv2.VideoCapture(0) # Define the...

python getpass实现密文实例详解

python getpass实现密文实例详解

getpass模块的使用: 在python中实现密码密文需要导入getpass模块,在python中要使用内置模块的话,需要使用import进行导入,比如import getpass...

跟老齐学Python之用while来循环

跟老齐学Python之用while来循环

在python中,它也有这个含义,不过有点区别的是,“当...时候”这个条件成立在一段范围或者时间间隔内,从而在这段时间间隔内让python做好多事情。就好比这样一段情景: while...

python破解bilibili滑动验证码登录功能

python破解bilibili滑动验证码登录功能

地址:https://passport.bilibili.com/login 左图事完整验证码图,右图是有缺口的验证码图      &n...