Python sqlite3数据库是一款非常小巧的内置模块,它使用一个文件存储整个数据库,操作十分方便,相比其他大型数据库来说,确实有些差距。但是在性能表现上并不逊色,麻雀虽小,五脏俱全,sqlite3实现了多少sql-92标准,比如说transaction、trigger和复杂的查询等。

描述

  Python的数据库模块有统一的接口标准,所以数据库操作都有统一的模式(假设数据库模块名为db):

  1. 用db.connect创建数据库连接,假设连接对象为conn

  2. 如果该数据库操作不需要返回结果,就直接使用conn.execute查询,根据数据库事物隔离级别的不同,可能修改数据库需要conn.commit

  3. 如果需要返回查询结果则用conn.cursor创建游标对象cur,通过cur.execute查询数据库,cursor方法有fetchall、fetchone、fetchmany返回查询结果,根据数据库事物隔离级别不同,可能修改数据库需要coon.commit

  4. 关闭cur.close

sqlite3基本操作用例

#coding=utf-8

import sqlite3

conn = sqlite3.connect("sqlite.db")  #创建sqlite.db数据库
print ("open database success")
conn.execute("drop table IF EXISTS student")
query = """create table IF NOT EXISTS student(
customer VARCHAR(20),
produce VARCHAR(40),
amount FLOAT,
date DATE
);"""
conn.execute(query)
print ("Table created successfully") #在表中插入数据 ''' 方法1 '''
#data = '''INSERT INTO student(customer,produce,amount,date)\
# VALUES("zhangsan","notepad",999,"2017-01-02")'''
#conn.execute(data)
#data = '''INSERT INTO student(customer,produce,amount,date)\
# VALUES("lishi","binder",3.45,"2017-04-05")'''
#conn.execute(data)
#conn.commit() ''' 方法2 '''
statement = "INSERT INTO student VALUES(?,?,?,?)"
data = [("zhangsan","notepad",999,"2017-01-02"),("lishi","binder",3.45,"2017-04-05")]
conn.executemany(statement, data)
conn.commit() curson = conn.execute("select * from student")
conn.commit()
print (curson)
rows = curson.fetchall()
print (rows)
conn.close()

sqlite3 csv->db->csv

'''将csv数据导入数据库'''
import sys
import csv
import sqlite3 #解析csv文件
def parsecsvFile(filepath):
header = None
data = []
with open(filepath, 'r') as csvfile:
filereader = csv.reader(csvfile)
header = next(filereader)
#print (header)
for row in filereader:
data.append(row)
#print (data)
return header,data #使用sqlite3写数据库
def initdb(header, data):
conn = sqlite3.connect("sqlite.db")
print ("connect database success")
conn.execute("drop table IF EXISTS student")
conn.commit()
query = '''create table IF NOT EXISTS student(\
Supplier Name VARCHAR(32),
Invoice Number VARCHAR(16),
Part Number VARCHAR(16),
Cost VARCHAR(16),
Purchase Date DATE);'''
conn.execute(query)
conn.commit()
statement = "INSERT INTO student VALUES(?,?,?,?,?)"
conn.executemany(statement, data)
conn.commit()
curson = conn.execute("select * from student")
conn.commit()
print (curson)
rows = curson.fetchall()
print (rows)
conn.close()
return rows #根据数据库内容写csv文件
def wirtecsvfile(writefilepath, header, data):
with open(writefilepath, 'a+') as writefile:
writer = csv.writer(writefile, delimiter=",")
writer.writerow(header)
for row in data:
writer.writerow(row) if __name__ == "__main__":
readfilepath = sys.argv[1]
writefilepath = sys.argv[2]
header,data = parsecsvFile(readfilepath)
rows = initdb(header, data)
wirtecsvfile(writefilepath, header, rows)

Python之sqlite3的更多相关文章

  1. 《Python操作SQLite3数据库》快速上手教程

    为什么使用SQLite数据库? 对于非常简单的应用而言,使用文件作为持久化存储通常就足够了,但是大多数复杂的数据驱动的应用需要全功能的关系型数据库.SQLite的目标则是介于两者之间的中小系统.它有以 ...

  2. Python 中 sqlite3的使用

    Python 中 sqlite3的使用 一.sqlite安装 下载地址 http://www.sqlite.org 1.数据库生成 sqlite3.exe testdb 2.创建表格,插入数据 3.在 ...

  3. Python访问sqlite3数据库取得dictionary的正路!

    [引子] 很多人都知道,Python里是内置了很好用的sqlite3的.但这个库有个缺陷,在执行fetchall()/fetchone()等方法后,得到的是一个tuple.以前吧,做自己的小项目,tu ...

  4. python 对 sqlite3的简单使用

    SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成.Python就内 ...

  5. PYTHON 对SQLITE3的简单使用

    SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成.Python就内 ...

  6. Python安装sqlite3

    今天使用PYthon时,发现错误 ImportError: No module named sqlite 这是因为缺少 SQLITE3的缘故. 下面分享一下解决此问题的方法步骤: 1. 查看是Pyth ...

  7. python用sqlite3模块操作sqlite数据库-乾颐堂

    SQLite是一个包含在C库中的轻量级数据库.它并不需要独立的维护进程,并且允许使用非标准变体(nonstandard variant)的SQL查询语句来访问数据库. 一些应用可是使用SQLite保存 ...

  8. python之sqlite3使用详解

    Python SQLITE数据库是一款非常小巧的嵌入式开源数据库软件,也就是说没有独立的维护进程,所有的维护都来自于程序本身.它使用一个文件存储整个数据库,操 作十分方便.它的最大优点是使用方便,功能 ...

  9. Python处理Sqlite3数据库

    sqlite3比较小众 本章主要通过Python Code表述如何增.查.改.删 sqlite3 DB 一.直接上代码 #!/usr/bin/env python # -*- coding: utf- ...

随机推荐

  1. centos7安装zabbix3.2.4

    系统:CentOS Linux release 7.2.1511 (Core) zabbix:3.2.4 一.yum -y install httpd mysql mysql-server mysql ...

  2. word自动备份,word误删内容恢复

    有个问题时长困扰着我,就是一次不小心把word里面的一部分内容误删了之后,又手残点击ctrl+s给保存了,要是立即ctrl+z还能撤销,可要是关闭了word才想起来撤销就来不及啦,现在终于找到解决的办 ...

  3. 云摘录︱Word2Vec 作者Tomas Mikolov 的三篇代表作解析

    本文来源于公众号paperweekly 谈到了word2vec作者的三篇论文: 1.Efficient Estimation of Word Representation in Vector Spac ...

  4. SystemVerilog语言简介(一)

    1. 接口(Interface) Verilog模块之间的连接是通过模块端口进行的.为了给组成设计的各个模块定义端口,我们必须对期望的硬件设计有一个详细的认识.不幸的是,在设计的早期,我们很难把握设计 ...

  5. dojo表格分页插件报错

    dojo表格分页插件报错 (1)dojo/parser::parse() error ReferenceError {stack:(...),message:"layout is not d ...

  6. directshow filter中添加属性页

    directShow 属性页的制作,为CBall filter加了一个属性页 具体为分以下步骤: 1.在要显示属性的类中继承现ISpecifyPropertyPages类,并实现此类的GetPages ...

  7. SPOJ D-QUERY

    以前主席树学  kungbin 最近看了网上的版本 终于发现和我以前学的线段树差不多的了 希望最近能够加强 #include<bits/stdc++.h> using namespace ...

  8. Jupyter notebook Tensorflow GPU Memory 释放

    Jupyter notebook 每次运行完tensorflow的程序,占着显存不释放.而又因为tensorflow是默认申请可使用的全部显存,就会使得后续程序难以运行.暂时还没有找到在jupyter ...

  9. httpclient的主要业务代码逻辑(图解)

    一,主要代码逻辑(图解) 二,两个案例的对比(图解) 三,详细案例 3.1,博文一 httppost的用法(NameValuePair(简单名称值对节点类型)核心对象) 3.2,博文二 httpcli ...

  10. Windows Developer Day - Windows AI Platform

    本次 Windows Developer Day,最值得期待的莫过于 Windows AI Platform 了,可以说是千呼万唤始出来.观看直播的开发者们,留言最多的也是 Windows AI Pl ...