Python操作sqlite数据库

【例1】create a table:

原数据库表格:

执行代码:

import sqlite3

conn = sqlite3.connect('phone.sqlite')
cur = conn.cursor()

cur.execute('create table clients (id INT PRIMARY KEY, name CHAR(60))')

cur.close()

conn.close()

执行后:

【例2】insert into a new value

代码:

import sqlite3
conn = sqlite3.connect('phone.sqlite')
cur = conn.cursor()
phone = ("Lumia 800", "windows",7)
cur.execute("insert into stock (name, os, number) values (?,?,?)", phone);
conn.commit()
cur.close()
conn.close()

执行后:

【例3】select one by one

数据库stock表:

代码:

import sqlite3

conn = sqlite3.connect('phone.sqlite')
cur = conn.cursor()

cur.execute("select * from stock where os = 'ios'");
row = cur.fetchone()
while row:
print row
row = cur.fetchone()
cur.close()
conn.close()

执行结果:

【例4】sum some numbers

对仓库中ios手机数量求和

import sqlite3

conn = sqlite3.connect('phone.sqlite')
cur = conn.cursor()

cur.execute("select sum(number) from stock where os = 'ios'");
row = cur.fetchone()
print row

cur.close()

conn.close()

最后结果:

转载需保留链接来源:软件玩家 » Python操作sqlite数据库

赞 (0) 打赏

评论 0

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

感恩您的支助!

微信扫一扫打赏