Python操作sqlite数据库小节
学习了Python操作sqlite数据库,做一个小结,以备后用。
import sqlite3
import os
# 进行数据库操作时,主要是参数如何传输
try:
# 链接数据库
conn=sqlite3.connect('DYDHX.db')
c=conn.cursor()
# 插入
c.execute("insert into tb_goods values(?,?,?,?,?)",("4","小龙虾",'xia','2000','斤'))
c.execute("insert into tb_goods values(%d,'%s','%s','%s','%s')"%(5,'小龙虾','xia','2000','斤'))
c.execute("insert into tb_goods('goodsid','goodsname','kucun','unit') values (?,?,?,?)",("14","小龙虾",'2000','斤'))
conn.commit()
# 查询
goods=c.execute("select * from tb_goods ")
goods=c.execute("select * from tb_goods where goodsid= ? ",("2"))
goods=c.execute("select * from tb_goods where goodsid= ? ","2")
goods=c.execute("select * from tb_goods where goodsid= %d "%(2))
goods=c.execute("select * from tb_goods where goodsid= %d and goodsname='%s'"%(1,"小龙虾"))
goods=c.execute("select * from tb_goods where goodsid=? and goodsname=?",(1,"小龙虾"))
# 将查询出的数据放入列表中,result列表中的元素是字典,goodlist列表中的元素是列表
result=[]
goodlist=[]
for g in goods:
# print("name=",g[1])
v={}
v['id']=g[0]
v['name']=g[1]
v['kucun']=g[3]
result.append(v) tmp=[]
tmp.append(g[0])
tmp.append(g[1])
tmp.append(g[3])
goodlist.append(tmp)
print(result)
print("goodlist=",goodlist) # 生成一个列表数据,列表的元素是字典
for i in range(0,len(result)):
result[i]['id']=result[i]['id']+10
result[i]['name']="螃蟹"
result[i]['kucun']=1200
print(result)
# 将一个列表插入到表里,列表中元素是字典
for g in result:
conn.execute("insert into tb_goods('goodsid','goodsname','kucun') values (?,?,?)", (g['id'],g['name'],g['kucun']))
conn.commit()
# 生成一个列表数据,列表的元素是列表
for i in range(0,len(goodlist)):
goodlist[i][0]=goodlist[i][0]+10
goodlist[i][1]="螃蟹"
goodlist[i][2]=1200
print(goodlist)
# 将一个列表插入到表里,列表中元素是列表
for g in goodlist:
conn.execute("insert into tb_goods('goodsid','goodsname','kucun') values (?,?,?)", g)
conn.commit() # 删除
c.execute("delete from tb_goods where goodsid= ? ","2")
c.execute("delete from tb_goods where goodsid= %d "%(14))
conn.commit()
except Exception as e:
print(e)
Python操作sqlite数据库小节的更多相关文章
- Python操作SQLite数据库的方法详解
Python操作SQLite数据库的方法详解 本文实例讲述了Python操作SQLite数据库的方法.分享给大家供大家参考,具体如下: SQLite简单介绍 SQLite数据库是一款非常小巧的嵌入式开 ...
- python操作sqlite数据库
root@cacti:~/box# cat convert.py #!/usr/bin/env python import sqlite3,time,rrdtool,os def boxstatus( ...
- Python 操作 SQLite 数据库
写在之前 SQLite 是一个小型的关系型数据库,它最大的特点在于不需要单独的服务.零配置.我们在之前讲过的两个数据库,不管是 MySQL 还是 MongoDB,都需要我们安装.安装之后,然后运行起来 ...
- python 操作sqlite数据库
'''SQLite数据库是一款非常小巧的嵌入式开源数据库软件,也就是说 没有独立的维护进程,所有的维护都来自于程序本身. 在python中,使用sqlite3创建数据库的连接,当我们指定的数据库文件不 ...
- 8.1 使用Python操作SQLite数据库
SQLite是内嵌在Python中的轻量级.基于磁盘文件袋额数据库管理系统,不需要安装和配置服务,支持使用SQL语句来访问数据库.该数据库使用C语言开发,支持大多数SQL91标准,支持原子的.一致的. ...
- Python 操作sqlite数据库及保存查询numpy类型数据(二)
# -*- coding: utf-8 -*- ''' Created on 2019年3月6日 @author: Administrator ''' import sqlite3 import nu ...
- Python 操作sqlite数据库及保存查询numpy类型数据(一)
# -*- coding: utf-8 -*- ''' Created on 2019年3月6日 @author: Administrator ''' import sqlite3 import nu ...
- [python]用Python进行SQLite数据库操作
用Python进行SQLite数据库操作 1.导入Python SQLITE数据库模块 Python2.5之后,内置了SQLite3,成为了内置模块,这给我们省了安装的功夫,只需导入即可~ ]: u ...
- Windows下安装MySQLdb, Python操作MySQL数据库的增删改查
这里的前提是windows上已经安装了MySQL数据库,且配置完成,能正常建表能操作. 在此基础上仅仅需安装MySQL-python-1.2.4b4.win32-py2.7.exe就ok了.仅仅有1M ...
随机推荐
- MFC深入浅出读书笔记第二部分1
第六章 MFC程序的生死因果 MFC学习过程,这个方法不错,条例清晰. 1.CWinApp -- 取代WinMain地位 WinMain函数的功能由CWinApp的三个函数实现 virtual BO ...
- 9.Iptables与Firewalld防火墙
第9章 Iptables与Firewalld防火墙 章节简述: 保障数据的安全性是继保障数据的可用性之后最为重要的一项工作.防火墙作为公网与内网之间的保护屏障,在保障数据的安全性方面起着至关重要的作用 ...
- URL 传参中需要处理的特殊字符
例如实际请求URL如下: http://www.douwansha.com/mdeditor?data=[{"address":null,"name":&quo ...
- 2018CCPC网络赛
A - Buy and Resell HDU - 6438 The Power Cube is used as a stash of Exotic Power. There are nn cities ...
- [译]如何检查python中的值是否为nan?
float('nan')是Nan不是一个数字,我该如何判断一个值为nan,有什么简单的方法么? 使用math.isnan()来进行判断 >>> import math >> ...
- android TranslateAnimation动画执行时的坐标获取。
android 的Tween动画并不会改变控件的属性值,比如以下测试片段: 定义一个从屏幕右边进入,滚动到屏幕左边消失的一个TranslateAnimation动画: <?xml version ...
- Unity 脚本<1>
RaycastHit2D hit = Physics2D.Linecast(targetPosition, targetPosition + new Vector2(x, y)); 猜测是lineca ...
- HDU 5687 Problem C(Trie+坑)
Problem C Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tota ...
- windows系统——U 盘损坏修复
u盘损坏怎么修复 1.打开控制面板——管理工具——计算机管理——存储——磁盘管理——右击“磁盘1”——点击“初始化磁盘”——“确定”——初始化完毕后,U盘为“联机”状态. 2.在右边空白处“新建磁盘分 ...
- pdf生成
要用本文的方法生成PDF文件,需要两个控件:itextsharp.dll和ICSharpCode.SharpZipLib.dll,由于示例代码实在太多,我将代码全部整理出来,放在另外一个文件“示例代码 ...