python类库26[sqlite]
一 sqlite 与 python 的类型对应
二 实例
import sqlite3 def sqlite_basic():
# Connect to db
conn = sqlite3.connect('test.db')
# create cursor
c = conn.cursor()
# Create table
c.execute('''
create table if not exists stocks
(date text, trans text, symbol text,
qty real, price real)
'''
)
# Insert a row of data
c.execute('''
insert into stocks
values ('2006-01-05','BUY','REHT',100,35.14)
'''
)
# query the table
rows = c.execute("select * from stocks")
# print the table
for row in rows:
print(row)
# delete the row
c.execute("delete from stocks where symbol=='REHT'")
# Save (commit) the changes
conn.commit()
# Close the connection
conn.close()
def sqlite_adv():
conn = sqlite3.connect('test2.db')
c = conn.cursor()
c.execute('''
create table if not exists employee
(id text, name text, age inteage)
''')
# insert many rows
for t in [('1', 'itech', 10),
('2', 'jason', 10),
('3', 'jack', 30),
]:
c.execute('insert into employee values (?,?,?)', t)
# create index
create_index = 'CREATE INDEX IF NOT EXISTS idx_id ON employee (id);'
c.execute(create_index)
# more secure
t = ('jason',)
c.execute('select * from employee where name=?', t)
# fetch query result
for row in c.fetchall():
print(row)
conn.commit()
conn.close()
def sqlite_adv2():
# memory db
con = sqlite3.connect(":memory:")
cur = con.cursor()
# execute sql
cur.executescript('''
create table book(
title,
author,
published
);
insert into book(title, author, published)
values (
'AAA book',
'Douglas Adams',
1987
);
''')
rows = cur.execute("select * from book")
for row in rows:
print("title:" + row[0])
print("author:" + row[1])
print("published:" + str(row[2]))
def sqlite_adv3():
import datetime # Converting SQLite values to custom Python types
# Default adapters and converters for datetime and timestamp
con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
cur = con.cursor()
cur.execute("create table test(d date, ts timestamp)") today = datetime.date.today()
now = datetime.datetime.now() cur.execute("insert into test(d, ts) values (?, ?)", (today, now))
cur.execute("select d, ts from test")
row = cur.fetchone()
print today, "=>", row[0], type(row[0])
print now, "=>", row[1], type(row[1]) cur.execute('select current_date as "d [date]", current_timestamp as "ts [timestamp]" from test')
row = cur.fetchone()
print "current_date", row[0], type(row[0])
print "current_timestamp", row[1], type(row[1])
#sqlite_basic()
#sqlite_adv()
#sqlite_adv2()
#sqlite_adv3()
完!
python类库26[sqlite]的更多相关文章
- python类库26[web2py之基本概念]
一 web2py的应用的执行环境Models,Controllers和views所在的执行环境中,以下对象已经被默认地导入: Global Objects: request,response,ses ...
- 二十六. Python基础(26)--类的内置特殊属性和方法
二十六. Python基础(26)--类的内置特殊属性和方法 ● 知识框架 ● 类的内置方法/魔法方法案例1: 单例设计模式 # 类的魔法方法 # 案例1: 单例设计模式 class Teacher: ...
- Python中使用SQLite
参考原文 廖雪峰Python教程 使用SQLite SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是用C写的,而且体积很小,所以经常被集成到各种应用程序中,甚至在IOS和 ...
- python类库32[多进程同步Lock+Semaphore+Event]
python类库32[多进程同步Lock+Semaphore+Event] 同步的方法基本与多线程相同. 1) Lock 当多个进程需要访问共享资源的时候,Lock可以用来避免访问的冲突. imp ...
- PyCharm无法找到已安装的Python类库的解决方法
一.问题描述 软件系统:Windows10.JetBrains PyCharm Edu 2018.1.1 x64 在命令行cmd中安装python类库包Numpy.Matplotlib.Pandas. ...
- python 数据库操作 SQLite、MySQL 摘录
转自: http://www.cnblogs.com/windlaughing/p/3157531.html 不管使用什么后台数据库,代码所遵循的过程都是一样的:连接 -> 创建游标 -> ...
- 在Ubuntu上升级SQLite,并让Python使用新版SQLite
(本文适用于Debian系的Linux,如Ubuntu.Raspbian等等.) 在Linux上,Python的sqlite3模块使用系统自带的SQLite引擎,然而系统自带的SQLite可能版本太老 ...
- SnowNLP:一个处理中文文本的 Python 类库[转]
SnowNLP是一个python写的类库,可以方便的处理中文文本内容,是受到了TextBlob的启发而写的,由于现在大部分的自然语言处理库基本都是针对英文的,于是写了一个方便处理中文的类库,并且和Te ...
- SQLite in Python: 如何在Python中使用SQLite数据库
SQLite3 可使用 sqlite3 模块与 Python 进行集成.sqlite3 模块是由 Gerhard Haring 编写的.它提供了一个与 PEP 249 描述的 DB-API 2.0 规 ...
随机推荐
- 【工具】rinetd 使用教程(linux 下的端口转发工具 )
日期:2019-07-30 20:00:36 更新: 作者:Bay0net 介绍:使用 rinetd 来转发某端口的流量. 0x01. 安装 官网 RINETD 安装方法很简单,一条语句就 OK 了. ...
- SAE Django如何禁止外部IP访问
在SAE上基于Django搭建的Web工程有时需要禁止来自某些特定IP地址的访问请求. 例如一个为搭建在SAE的其他项目提供服务的内部工程,可以设置为只允许SAE内部的IP地址访问,从而提高项目的安全 ...
- python关键字参数和位置参数
关键字参数必须跟随在位置参数后面! 因为python函数在解析参数时, 是按照顺序来的, 位置参数是必须先满足, 才能考虑其他可变参数.,否则报错如下: In [74]: print(s1.forma ...
- LeetCode.949-给定数字的最大时间(Largest Time for Given Digits)
这是悦乐书的第363次更新,第391篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第225题(顺位题号是949).给定4个整数组成的数组,返回最大的24小时时间. 最小的 ...
- Jmeter+TCP\Sockets(8583)报文压力测试
Jmeter一般被用来测试HTTP协议,我第一次拿来测试socket协议,pos机传输报文为8583,协议属于socket,也是TCP协议的一种,网上有LR怎么测试8583报文,我就研究了一下怎么用J ...
- SQL子连接案例
子查询 何时使用子查询 1. 子查询作为数据源 2. 数据加工 需求:根据不同顾客的所有的账户余额划分区间,进行分组 sql语句实现如下: select 'Small Fry' name , 0 lo ...
- css3实现倾斜转动的转盘
HTML代码: <div class="r-1">a</div> <div class="r-2">a</div> ...
- XCode8.3真机调试设置
本文使用XCode8.3.3 首先XCode->Preferncs,进入下面的界面 点击左下角“+”号,并输入账号,然后点击Manage Certificates,左下角添加IOS develo ...
- [codeforces940E]Cashback
题目链接 题意是说将$n$个数字分段使得每段贡献之和最小,每段的贡献为区间和减去前$\left \lfloor \frac{k}{c}\right \rfloor$小的和. 仔细分析一下可以知道,减去 ...
- 洛谷 P2331 最大子矩阵 题解
题面 对于m==1和m==2两种状态进行不同的dp: 设sum[i][1]表示第一列的前缀和,sum[i][2]表示第二列的前缀和: sum[i][1]=sum[i-1][1]+a[i][1]; su ...