import sqlite3
1.打开或创建数据库文件
conn = sqlite3.connect('test.db')
2.创建数据表
conn = sqlite3.connect('test.db')
c = conn.cursor()
sql = '''
create table company
(id integer not null primary key autoincrement,
name text not null,
age int not null,
address char(50),
salary real);
'''
c.execute(sql)
conn.commit()
c.close()
conn.close()
3.插入数据
conn = sqlite3.connect('test.db')
c = conn.cursor()
sql = 'insert into company(name, age, address, salary) values ("张三",32,"成都",8000)'
c.execute(sql)
conn.commit()
c.close()
conn.close()
'''
采集数据一般写法
data = [2,"张三",32,"成都",8000]
sql = 'insert into company (id, name, age, address, salary) value (%s)'%','.join(data)
'''
4.查询
conn = sqlite3.connect('test.db')
c = conn.cursor()
sql = 'select * from company'
data = c.execute(sql)
for d in data:
print(d)
print('id',d[0])
print('name',d[1])
print('address',d[2])
print('salary',d[3])
print('---------------------------------')
c.close()
conn.close()
5.删表
conn = sqlite3.connect('test.db')
c = conn.cursor()
sql = 'drop table douyu'
c.execute(sql)
conn.commit()
c.close()
conn.close()
转载请注明原文地址:https://ipadbbs.8miu.com/read-2321.html