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 ...
随机推荐
- 【Median of Two Sorted Arrays】cpp
题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...
- Leetcode 561.数组拆分I
数组拆分 I 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总 ...
- C++ bitset类的使用与简介
有些程序要处理二进制位的有序集,每个位可能包含的是0(关)或1(开)的值.位是用来保存一组项或条件的yes/no信息(有时也称标志)的简洁方法.标准库提供了bitset类使得处理位集合更容易一些.要使 ...
- Codeforces Round #418 (Div. 2) A+B+C!
终判才知道自己失了智.本场据说是chinese专场,可是请允许我吐槽一下题意! A. An abandoned sentiment from past shabi贪心手残for循环边界写错了竟然还过了 ...
- 生成比较短的Token字符串
有的时候,我们需要生成一些Token作为标识:如认证后的标识符,资源的提取码等.一个比较常见的算法是生成一个GUID来作为Token,由于GUID的随机性和唯一性特点,作为Token是一个非常可靠的选 ...
- FZU 2041 二分枚举
思路:先O(n)预处理出ri[i][j],le[i][j],分别表示第i个位置向右边移动出j个空格需要的步数,表示第i个位置向左边移动出j个空格需要的步数. 然后枚举间隙处,二分判段最大间隔. #in ...
- BZOJ2396 神奇的矩阵 【随机化 + 矩乘】
题目链接 BZOJ2396 题解 一种快速判断两个矩阵是否相等的方法: 对于两个\(n * n\)矩阵,两边同时乘一个\(n * 1\)的随机矩阵,如果结果相等,那么有很大概率两个矩阵相等 如果左边是 ...
- leetcode 15 3sum & leetcode 18 4sum
3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...
- webpack 样式模块打包深入学习
1. style-loader css-loader sass-loader 分别的作用 style-loader: 将所有的样式嵌入到dom的style属性当中. css-loader: 将css当 ...
- shell中规则表达式与特殊符号
在 bash 的操作环境中还有一个非常有用的功能,那就是通配符 (wildcard) ! 我们利用 bash 处理数据就更方便了!底下我们列出一些常用的通配符喔: 符号 意义 * 代表『 0 个到无穷 ...