python创建student表,包含stid,stname,stage,stcount字段,完成数据的增删改查操作
时间: 2023-08-18 16:07:00 浏览: 82
可以使用 Python 自带的 sqlite3 模块来创建 student 表,并完成数据的增删改查操作。下面是一个示例代码:
```python
import sqlite3
# 连接数据库
conn = sqlite3.connect('student.db')
c = conn.cursor()
# 创建 student 表
c.execute('''CREATE TABLE student
(stid INT PRIMARY KEY,
stname TEXT NOT NULL,
stage INT NOT NULL,
stcount INT NOT NULL);''')
# 插入数据
c.execute("INSERT INTO student (stid, stname, stage, stcount) VALUES (1, 'Alice', 18, 100)")
c.execute("INSERT INTO student (stid, stname, stage, stcount) VALUES (2, 'Bob', 19, 95)")
c.execute("INSERT INTO student (stid, stname, stage, stcount) VALUES (3, 'Charlie', 20, 90)")
conn.commit()
# 查询数据
c.execute("SELECT * FROM student")
rows = c.fetchall()
for row in rows:
print(row)
# 更新数据
c.execute("UPDATE student SET stcount = 80 WHERE stid = 2")
conn.commit()
# 删除数据
c.execute("DELETE FROM student WHERE stid = 3")
conn.commit()
# 关闭连接
conn.close()
```
以上代码中,首先通过 `sqlite3.connect()` 方法连接数据库,然后使用 `c.execute()` 方法执行 SQL 语句来创建表、插入数据、查询数据、更新数据和删除数据。最后使用 `conn.close()` 方法关闭连接。
阅读全文
相关推荐
















