python.sqlite3 简单操作
2012-11-25 22:15
597 查看
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', ); ''') 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])
相关文章推荐
- python.sqlite3 简单操作
- python3.3 简单操作sqlite
- python 简单网站 以及sqlite 操作 BaseHTTPServer
- Python 搞搞数据库 简单的SQLite操作
- Python 搞搞数据库 简单的SQLite操作前 之读取文档 readlines
- python操作 sqlite3的简单方法
- Python简单操作sqlite3的方法示例
- python 连接sqlite及简单操作
- Python 搞搞数据库 简单的SQLite操作前 之矩阵 matrix
- python sqlite3 连接到数据库,创建表,INSERT 、SELECT、UPDATE 、DELETE等简单操作
- python操作redis简单例子
- Python接口测试第一讲(代码管理工具git的简单操作)
- 基于Python实现一个简单的银行转账操作
- Python3.6简单的操作Mysql数据库的三个实例
- python学习文件简单读写操作
- Android自学之sqlite数据的基本操作和事物的简单应用
- Python3中简单的文件操作及两个简单小实例分享
- python openpyxl 操作 excel ==> 简单读取(持续完善...)
- Python3多线程操作简单示例
- python操作redis简单例子