sqlite3

sqlite3

连接

import sqlite3


# sql语句中无论 大小写
conn = sqlite3.connect('test.db')
c = conn.cursor()
c.execute(…………)
……
conn.commit()
conn.close()

建表

c.execute('''drop table company''')
c.execute('''create table company 
(id int  primary key  not null,
name text not null,
age int not null,
address char(50),
salary real
);''')

c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
    VALUES (1, 'Paul', 32, 'California', 20000.00 )")

c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
    VALUES (2, 'Allen', 25, 'Texas', 15000.00 )")

c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
    VALUES (3, 'Teddy', 23, 'Norway', 20000.00 )")

c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
    VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 )")

c.execute('delete from company where id = 4')

c.execute('update company set salary = 99999 where  name = "allen"')

res = c.execute('select name,salary from company')
for i in res:
    print(i)

范例

import sqlite3


# sql语句中无论 大小写
conn = sqlite3.connect('test.db')
c = conn.cursor()
c.execute('''drop table company''')
c.execute('''create table company 
(id int  primary key  not null,
name text not null,
age int not null,
address char(50),
salary real
);''')


# 增加
c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
    VALUES (1, 'Paul', 32, 'California', 20000.00 )")

c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
    VALUES (2, 'Allen', 25, 'Texas', 15000.00 )")

c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
    VALUES (3, 'Teddy', 23, 'Norway', 20000.00 )")

c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
    VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 )")

# 删除
c.execute('delete from company where id = 4')

# 修改
c.execute('update company set salary = 99999 where  name = "allen"')


# 查询
res = c.execute('select name,salary from company')
for i in res:
    print(i)


conn.commit()
conn.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值