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

相关文章

Python中time模块和datetime模块的用法示例

time模块方法: time.time():获取当前时间的时间戳 time.localtime():接受一个时间戳,并把它转化为一个当前时间的元组。不给参数的话就会默认将time.tim...

python2.7删除文件夹和删除文件代码实例

复制代码 代码如下:#!c:\python27\python.exe# -*- coding: utf-8 -*- import osimport re from os import p...

Python模拟三级菜单效果

Python模拟三级菜单效果

本文实例为大家分享了Python模拟三级菜单效果的具体代码,供大家参考,具体内容如下 1.功能简介 此程序模拟多级菜单操作,实现按菜单项对应数字索引进入下级菜单,按b键回退到上一级菜单,...

解决Python2.7读写文件中的中文乱码问题

Python2.7对于中文编码的问题处理的并不好,这几天在爬数据的时候经常会遇到中文的编码问题。但是本人对编码原理不了解,也没时间深究其中的原理。在此仅从应用的角度做一下总结, 1.设置...

Python django使用多进程连接mysql错误的解决方法

Python django使用多进程连接mysql错误的解决方法

问题 mysql 查询出现错误 error: (2014, "Commands out of sync; you can't run this command now")1 查询 m...