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 cookielib 登录人人网的实现代码

先上脚本吧,等下来讲下知识点: 复制代码 代码如下: #!/usr/bin/env python #encoding=utf-8 import sys import re import...

Python装饰器使用实例:验证参数合法性

python是不带静态检查的动态语言,有时候需要在调用函数时保证参数合法。检查参数合法性是一个显著的切面场景,各个函数都可能有这个需求。但另一方面,参数合法性是不是应该由调用方来保证比较...

简述Python2与Python3的不同点

在Python2和Python3中都提供print()方法来打印信息,但两个版本间的print稍微有差异 主要体现在以下几个方面: 1.python3中print是一个内置函数,有多个参...

python中bs4.BeautifulSoup的基本用法

导入模块 from bs4 import BeautifulSoup soup = BeautifulSoup(html_doc,"html.parser") 下面看下常见的用...

pytorch获取模型某一层参数名及参数值方式

1、Motivation: I wanna modify the value of some param; I wanna check the value of some param....