import sqlite3 #导入模块
conn = sqlite3.connect('example.db')
C=conn.cursor()
#创建表
C.execute('''CREATE TABLE stocks(data text,trans text, symple text,qty real,price real)''')
#插入一条数据
C.execute('''INSERT INTO stocks VALUES('2006-10-01,'BUY','RHA',100,35.14)''')
#提交当前事务,保存数据
#关闭数据连接
conn.close()
#查询数据库
for row in c.execute('SELECT * FROM stocks OPRED BY price'):
print(row)#s数据库是列表类型,可进行迭代
import sqlite3
conn = sqlite3.connect(":memory:")
cur=conn.cursor()
cur.execute("CREATE TABLE people(name_last,age)")
who = "li"
age="20"
cur.execute("INSERT INTO people VALUES(?, ?)",(who, age))
cur.execute("SELECT * FROM people WHERE name_last=:who AND age=:age",{"who":who,"age":age})
print(cur.fetchone())
Cursor
#1利用数据库完成小写字母
import sqlite3
class IterChars:
def __init__(self):
self.count=ord('a')
def __iter__(self):
return self
def __next__(self):
if self.count>ord('z'):
raise StopIteration
self.count+=1
return(chr(self.count-1),)
conn = sqlite3.connect(":memory:")
cur=conn.cursor()
cur.execute("CREATE TABLE characters(c)")
theIter=IterChars()
cur.executemany("INSERT INTO characters(c) VALUES(?)",theIter)
cur.execute("SELECT c FROM characters")
print(cur.fetchall())
#2利用yield完成对小写字母的创建
import sqlite3
import string
def char_gernerator():
for c in string.ascii_lowercase:
yield(c,)
conn = sqlite3.connect(":memory:")
cur=conn.cursor()
cur.execute("CREATE TABLE characters(c)")
#theIter=IterChars()
cur.executemany("INSERT INTO characters(c) VALUES(?)",char_gernerator())
cur.execute("SELECT c FROM characters")
print(cur.fetchall())
python的sqlite坑
#bug1表不存在
import sqlite3
conn=sqlite3.connect("D:/addressbook.db")
cur = conn.cursor()
cur.execute('''INSERT INTO addresslist(name,sex,phone,qq,address)VALUES('小王','女','18337257400','18674529','beijing')''')
cur.execute('''INSERT INTO addresslist(name,sex,phone,qq,address)VALUES('小李','女','18337257401','18674528','nanjing')''')
cur.execute('''INSERT INTO addresslist(name,sex,phone,qq,address)VALUES('小l','女','18337257403','18674525','tianjin')''')
conn.commit()
conn.close()
程序执行,我在心里想着这么low的代码应该没问题的,结果竟然报错了,fuck,表不存在什么鬼,于是百度,发现好多都是废话,好吧,不需要了,我在脑中想了会,既然不存在,那我就创建
#修改后
import sqlite3
conn=sqlite3.connect("D:/addressbook.db")
cur = conn.cursor()
cur.execute('''CREATE TABLE addresslist(name text,sex text,phone real,qq real,address text)''')
cur.execute('''INSERT INTO addresslist(name,sex,phone,qq,address)VALUES('小王','女','18337257400','18674529','beijing')''')
cur.execute('''INSERT INTO addresslist(name,sex,phone,qq,address)VALUES('小李','女','18337257401','18674528','nanjing')''')
cur.execute('''INSERT INTO addresslist(name,sex,phone,qq,address)VALUES('小l','女','18337257403','18674525','tianjin')''')
conn.commit()
conn.close()
程序bug完美解决
下面进行遍历查找
import sqlite3
conn=sqlite3.connect("D:/addressbook.db")
cur=conn.cursor()
cur.execute('SELECT * FROM addresslist')
li = cur.fetchall()
for line in li:
for item in line:
print(item,end=' ')
print()
conn.close()
好了,终于完美解决。
#sqllite之row
import sqlite3
conn=sqlite3.connect(":memory:")
c=conn.cursor()
#创建表
c.execute('''CREATE TABLE stocks(data text,trans text,symple text,qty real,price real)''')
c.execute("""INSERT INTO stocks VALUES ('2006-10-01','BUY','RHA',100,35.14)""")
conn.commit()
c.close()
conn.row_factory=sqlite3.Row
c=conn.cursor()
c.execute('SELECT * FROM stocks')
r=c.fetchone()
print(type(r))
print(tuple(r))
print(r[2])
print(r.keys())
print(r['qty'])
for field in r:
print(field)