python连接sqlite的实例教程


本文摘自php中文网,作者零下一度,侵删。

本文通过实例代码给大家介绍了python 连接sqlite及简单操作,非常不错,具有参考借鉴价值,需要的朋友参考下吧

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


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

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中怎么运行shell脚本

小甲鱼零基础入门学习Python视频教程

Python之正则表达式中的贪心模式和非贪心模式的用法和区别

Python怎么安装plotly

Python调用xlsxwriter创建xlsx的方法

Python怎么调用idle

Python人工智能是什么意思

大学有Python课吗

3分钟搞懂Python中dict函数的含义是什么

Python requests快速入门介绍

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...