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装饰器定义及运用实例讲解

介绍Python的函数装饰器

Python有double类型吗

Python的tornado之websocket的概念以及应用介绍

pandas修改dataframe列名的方法介绍(代码示例)

如何删除Python字典中的元素?如何清空字典?

Python什么时候出的

Python使用struct处理二进制的方法详解

Python ide修改背景颜色的教程

Python哪年火的

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




打赏

取消

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

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

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

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

评论

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