python 连接sqlite及简单操作

yipeiwu_com5年前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设计】网站的支持!

相关文章

在VS Code上搭建Python开发环境的方法

在VS Code上搭建Python开发环境的方法

1、下载安装 python https://www.python.org/downloads/windows/ web-based installer 在线安装 executable...

python实现k均值算法示例(k均值聚类算法)

简单实现平面的点K均值分析,使用欧几里得距离,并用pylab展示。 复制代码 代码如下:import pylab as pl #calc Euclid squiredef calc_e_...

Python写的服务监控程序实例

前言: Redhat下安装Python2.7 rhel6.4自带的是2.6, 发现有的机器是python2.4。 到python网站下载源代码,解压到Redhat上,然后运行下面的命令:...

pytorch中tensor.expand()和tensor.expand_as()函数详解

tensor.expend()函数 >>> import torch >>> a=torch.tensor([[2],[3],[4]]) >...

Python单例模式实例分析

本文实例讲述了Python单例模式的使用方法。分享给大家供大家参考。具体如下: 方法一 复制代码 代码如下:import threading    class S...