昊天SEO

python操作sqlite

#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
from sqlite3 import dbapi2 as sqlite
 
# Create a database:
con = sqlite.connect('mydatabase.db3')
cur = con.cursor()
 
# Create a table:
cur.execute('create table clients (id INT PRIMARY KEY, name CHAR(60))')
 
# Insert a single line:
client = (5,"John Smith")
cur.execute("insert into clients (id, name) values (?, ?)", client )
con.commit()
 
# Insert several lines at once:
clients = [ (7,"Ella Fitzgerald"),
 (8,"Louis Armstrong"),
 (9,"Miles Davis")
 ]
cur.executemany("insert into clients (id, name) values (?, ?)", clients )
con.commit()
 
cur.close()
con.close()
 
#下面的代码对数据库进行连接查询
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
from sqlite3 import dbapi2 as sqlite
 
# Connect to an existing database
con = sqlite.connect('mydatabase.db3')
cur = con.cursor()
 
# Get row by row
print "Row by row:"
cur.execute('select id, name from clients order by name;')
row = cur.fetchone()
while row:
 print row
 row = cur.fetchone()
 
# Get all rows at once:
print "All rows at once:"
cur.execute('select id, name from clients order by name;')
print cur.fetchall()
 
cur.close()
con.close()
网站的维护离不开大家的支持鼓励,捐赠让我更有动力走的更远&& 也可以关注我的微信公众号,发布更多的干货
本文网址:https://www.168seo.cn/python/1630.html

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址