python体验(05).python处理sqlite数据库
python处理sqlite数据库
1、检查用户环境是否安装sqlite
aptitude show sqlite3
2、创建数据库,添加表并插入数据
sqlite3 myfamily
3、编写代码
1、检查用户环境是否安装sqlite
aptitude show sqlite3
2、创建数据库,添加表并插入数据
sqlite3 myfamily
create table people(id int, name varchar(30), age int, sex char(1));
insert into people values(0,'Zihao Xu',5,'M');
select * from people;.exit;
insert into people values(0,'Zihao Xu',5,'M');
select * from people;.exit;
phoenix@debian:~/py/database$ cat sqlite.py
#!/usr/bin/python
#filename:sqlite.py
#
import sqlite3 #import sqlite3 module
con = sqlite3.connect('myfamily') #connect to the database
cur = con.cursor() #get the database cursor
cur.execute('insert into people(id,name,age,sex)' +
' values(99,\'temp\',99,\'F\')')
r = cur.execute('delete from people where age=99')
con.commit()
cur.execute('select * from people')
s = cur.fetchall()
print s
cur.close()
con.close()
#!/usr/bin/python
#filename:sqlite.py
#
import sqlite3 #import sqlite3 module
con = sqlite3.connect('myfamily') #connect to the database
cur = con.cursor() #get the database cursor
cur.execute('insert into people(id,name,age,sex)' +
' values(99,\'temp\',99,\'F\')')
r = cur.execute('delete from people where age=99')
con.commit()
cur.execute('select * from people')
s = cur.fetchall()
print s
cur.close()
con.close()