python sqlite3 创建、读取、修改、删除

操作示例,聊作笔记

# python sqlite3 的操作

#导入sqlite3
import sqlite3 as sq

#打开数据库连接

conn = sq.connect('test.db')
cur = conn.cursor()
print('open database successfully')

#查看存在的数据表
#conn.execute("select name from sqlite_master where type='table' order by name " )  #??
#conn.commit()

#清除已存在的数据表 students

conn.execute(''' drop table students''')
conn.commit()

#创建一个表
conn.execute('''
create table students
(ID int key not null,
NAME text not null,
AGE int not null);
''')

>>> import sqlite3 as s
>>> conn = s.connect('test.db')
>>> cur = conn.cursor()
>>> cur.execute("select name from sqlite_master where type='table' order by name " )
<sqlite3.Cursor object at 0x7fe2d21052d0>
>>> print(cur.fetchall)
<built-in method fetchall of sqlite3.Cursor object at 0x7fe2d21052d0>
>>> cur.fetchall
<built-in method fetchall of sqlite3.Cursor object at 0x7fe2d21052d0>
>>> cur.fetchall()
[]
>>> conn.execute('''
create table students
(ID int key not null,
NAME text not null,
AGE int not null);
''')
<sqlite3.Cursor object at 0x7fe2d2b3fdc0>
>>> conn.commit()
>>> conn.execute("insert into students(ID, NAME, AGE) values(1, 'Allen', 25)")
<sqlite3.Cursor object at 0x7fe2d20bc1f0>
>>> conn.execute("insert into students(ID, NAME, AGE) values(2, 'Maxsu', 20)")
<sqlite3.Cursor object at 0x7fe2d20bc260>
>>> conn.execute("insert into students(ID, NAME, AGE) values(2, 'Teddy', 24)")
<sqlite3.Cursor object at 0x7fe2d20bc1f0>
>>> conn.commit()
>>> cur = conn.execute("select * from students")
>>> cur.fetchall
<built-in method fetchall of sqlite3.Cursor object at 0x7fe2d20bc260>
>>> cur.fetchall()
[(1, 'Allen', 25), (2, 'Maxsu', 20), (2, 'Teddy', 24)]
>>> data = [(4, 'Allen2', 35), (5, 'Maxsu2', 30), (6, 'Teddy2', 34)]
>>> com = "insert into students values(?, ?, ?)"
>>> conn.executemany(com, data)
<sqlite3.Cursor object at 0x7fe2d21052d0>
>>> conn.commit()
>>> cur = conn.execute('select * from students')
>>> cur.fetchall
<built-in method fetchall of sqlite3.Cursor object at 0x7fe2d20bc1f0>
>>> cur.fetchall()
[(1, 'Allen', 25), (2, 'Maxsu', 20), (2, 'Teddy', 24), (4, 'Allen2', 35), (5, 'Maxsu2', 30), (6, 'Teddy2', 34)]
>>> conn.execute("update students set AGE=40 where ID=4")
<sqlite3.Cursor object at 0x7fe2d21052d0>
>>> conn.commit()
>>> cur = conn.execute('select * from students')
>>> cur.fetchall
<built-in method fetchall of sqlite3.Cursor object at 0x7fe2d20bc260>
>>> cur.fetchall()
[(1, 'Allen', 25), (2, 'Maxsu', 20), (2, 'Teddy', 24), (4, 'Allen2', 40), (5, 'Maxsu2', 30), (6, 'Teddy2', 34)]
>>> conn.execute("delete NAME from students where ID=3")
Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    conn.execute("delete NAME from students where ID=3")
sqlite3.OperationalError: near "NAME": syntax error
>>> conn.execute("delete from students where ID=3")
<sqlite3.Cursor object at 0x7fe2d21052d0>
>>> conn.commit()
>>> cur = conn.execute("select * from students")
>>> cur.fetchall()
[(1, 'Allen', 25), (2, 'Maxsu', 20), (2, 'Teddy', 24), (4, 'Allen2', 40), (5, 'Maxsu2', 30), (6, 'Teddy2', 34)]
>>> conn.execute("delete from students where ID=2")
<sqlite3.Cursor object at 0x7fe2d21052d0>
>>> conn.commit()
>>> cur= conn.execute('select * from students')
>>> cur.fetchall()
[(1, 'Allen', 25), (4, 'Allen2', 40), (5, 'Maxsu2', 30), (6, 'Teddy2', 34)]
>>> conn.execute("select * from test.db")
Traceback (most recent call last):
  File "<pyshell#44>", line 1, in <module>
    conn.execute("select * from test.db")
sqlite3.OperationalError: no such table: test.db
>>> conn.execute("select * from test")
Traceback (most recent call last):
  File "<pyshell#45>", line 1, in <module>
    conn.execute("select * from test")
sqlite3.OperationalError: no such table: test
>>> conn.execute("select * from sqlite_master")
<sqlite3.Cursor object at 0x7fe2d21052d0>
>>> cur = conn.execute("select * from sqlite_master")
>>> cur.fetchall()
[('table', 'students', 'students', 2, 'CREATE TABLE students\n(ID int key not null,\nNAME text not null,\nAGE int not null)')]
>>> cur.execute("select name from sqlite_master where type='table' order by name " )
<sqlite3.Cursor object at 0x7fe2d20ce5e0>
>>> cur.fetchall()
[('students',)]
>>> 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值