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 ...
随机推荐
- Halcon11 Windows版 下载
Halcon11 下载地址:http://www.211xun.com/download_page_2.html HALCON 11 是一套机器视觉图像处理库,由一千多个算子以及底层的数据管理核心构成 ...
- 关于C++ STL标准库中map 的多元素应用
map的特性是,所有的元素会根据键值自动排序.map的所有元素都是pair,同时拥有实值(value)和键值(key).pair的第一个元素被视为键值,第二个被视为实质piar 的定义 templat ...
- c++ stl在acm的入门及使用
stl的全称为Standard Template Library,即为标准模板库,它主要依赖于模板,而不是对象,所以你需要对这个模板进行实例化,选择你要使用的类型.我们用的都是一些简单的容器吧 这里可 ...
- ms sqlserver数据库主文件特别大怎么办
因为项目中需要复制数据库,作为外网测试的数据库,但是数据库特别大,复制特别费劲,即使只复制主文件,主文件也特别大. 然后百度了下,发现数据库有个收缩功能,数据库右键——任务——收缩,可以对数据库进行收 ...
- Mysql,phpmyadmin密码忘了怎么办
1.关闭mysql服务 # service mysql stop 如果提示mysql: unrecognized service这样的错误提示. 先查看查找mysql.server,使用:find / ...
- BZOJ1396&2865 识别子串 【后缀自动机 + 线段树】
题目 输入格式 一行,一个由小写字母组成的字符串S,长度不超过10^5 输出格式 L行,每行一个整数,第i行的数据表示关于S的第i个元素的最短识别子串有多长. 输入样例 agoodcookcooksg ...
- 浅谈 easyui tabs 的href和content属性
众所周知,jQuery Easyui 的tabs插件有两种方式加载某个tab(标签页)上的内容:“href远程请求”和“content本地内容”,本文就两种方式的优缺点进行简单分析和思考. 两者特点: ...
- react 复习4- 生命周期
实例化 首次实例化 getDefaultProps getInitialState componentWillMount render componentDidMount 实例化完成后的更新 getI ...
- Mybatis Plugin插件安装破解及使用
2018年2月更新 2018年2月份,提供一个网上比较多的一个版本V3.21版本,下载资源里面有个已整合版直接解压放入C:\Users\你的用户名\.IntelliJIdea2017.3\config ...
- codechef AUG17 T5 Chef And Fibonacci Array
Chef has an array A = (A1, A2, ..., AN), which has N integers in it initially. Chef found that for i ...