博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python类库26[sqlite]
阅读量:6945 次
发布时间:2019-06-27

本文共 2550 字,大约阅读时间需要 8 分钟。

一 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()

 

 

完! 

 

 

 

 

转载地址:http://caanl.baihongyu.com/

你可能感兴趣的文章
【emWin】例程十一:GIF图像显示
查看>>
WebAPI性能优化之压缩解压
查看>>
jquery获取下拉框中的循环值
查看>>
HTML5 一些有用的 APIs
查看>>
Unity获取指定资源目录下的所有文件
查看>>
C - The C Answer (2nd Edition) - Exercise 1-12
查看>>
linux定时任务cron 安装配置
查看>>
SequenceFile文件
查看>>
人脸和性别识别(基于OpenCV)
查看>>
Nginx Java 日志切割脚本
查看>>
浅谈代码审计入门实战:某博客系统最新版审计之旅
查看>>
nyoj 119士兵杀敌(三)(线段树区间最值查询,RMQ算法)
查看>>
truncate/drop表非常慢,怎么办?用硬链接,极速体验
查看>>
spring boot测试
查看>>
Timer使用
查看>>
H5+混合移动app应用开发——坑我太甚
查看>>
nc/netcat命令
查看>>
web3.js编译Solidity,发布,调用全部流程(手把手教程)
查看>>
Java国际化号码验证方法,国内手机号正则表达式
查看>>
HDU 1158 Employment Planning
查看>>