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 ...
随机推荐
- Leetcode 565.数组嵌套
数组嵌套 索引从0开始长度为N的数组A,包含0到N - 1的所有整数.找到并返回最大的集合S,S[i] = {A[i], A[A[i]], A[A[A[i]]], ... }且遵守以下的规则. 假设选 ...
- Spring整合hibernate -SessionFactory
本文目录 1 本文采用 hibernate4 整合 Spring3.1 2 把Spring获取datasource通过class="org.springframework.orm.hibe ...
- SpringCloud Eureka参数配置项详解
SpringCloud Eureka参数配置项详解(转) Eureka涉及到的参数配置项数量众多,它的很多功能都是通过参数配置来实现的,了解这些参数的含义有助于我们更好的应用Eureka的各种功能,下 ...
- php显示错误
error_reporting(E_ALL); ini_set('display_errors', '1'); //将出错信息输出到一个文本文件 ini_set('error_log', dir ...
- MySQL Innodb表导致死锁日志情况分析与归纳
发现当备份表格的sql语句与删除该表部分数据的sql语句同时运行时,mysql会检测出死锁,并打印出日志 案例描述在定时脚本运行过程中,发现当备份表格的sql语句与删除该表部分数据的sql语句同时 ...
- Android EditText默认不弹出输入法,以及获取光标,修改输入法Enter键的方法
一.Android EditText默认不弹出输入法的办法:1. 在AndroidManifest.xml中将需要默认隐藏键盘的Activity中添加属性即可(常用此方法) android:windo ...
- [SDOI2011][bzoj2245] 工作分配 [费用流]
题面 传送门 思路 数据范围n,m<=250 分配任务问题 这是典型的"看到数据范围就知道算法"类型 而且我们发现我们要保证一定产出的情况下最小化花费 这句话等价于保证一定流 ...
- 编程风格——UNIX 高手的 10 个习惯
引言 当您经常使用某个系统时,往往会陷入某种固定的使用模式.有时,您没有养成以尽可能最好的方式做事的习惯.有时,您的不良习惯甚至会导致出现混乱.纠正此类缺点的最佳方法之一,就是有意识地采用抵制这些坏习 ...
- Gym 100971B 水&愚
Description standard input/output Announcement Statements A permutation of n numbers is a sequence ...
- 纯js实现网页tab选项卡切换效果
纯js实现网页tab选项卡切换效果 百度搜索 js 点击菜单项就可以切换内容的效果