把一下mysql的命令行,封装进一个python代码,实现存储过程。代码如下: /* CREATE DATABASE test; USE `test`; CREATE TABLE `course` ( `Cno` VARCHAR(20) NOT NULL, `Cname` VARCHAR(20) NOT NULL, `Tno` VARCHAR(20) NOT NULL ) ENGINE=INNODB DEFAULT CHARSET=utf8; INSERT INTO `course`(`Cno`,`Cname`,`Tno`) VALUES ('3-105','计算机导论','825'),('3-245','操作系统','804'),('6-166','数字电路','856'),('9-888','高等数学','831'); CREATE TABLE `score` ( `Sno` VARCHAR(20) NOT NULL, `Cno` VARCHAR(20) NOT NULL, `Degree` DECIMAL(4,1) DEFAULT NULL ) ENGINE=INNODB DEFAULT CHARSET=utf8; INSERT INTO `score`(`Sno`,`Cno`,`Degree`) VALUES ('103','3-245','86.0'),('105','3-245','75.0'),('109','3-245','68.0'),('103','3-105','92.0'),('105','3-105','88.0'),('109','3-105','76.0'),('101','3-105','64.0'),('107','3-105','91.0'),('108','3-105','78.0'),('101','6-166','85.0'),('107','6-166','79.0'),('108','6-166','81.0'); CREATE TABLE `student` ( `Sno` VARCHAR(20) NOT NULL, `Sname` VARCHAR(20) NOT NULL, `Ssex` VARCHAR(20) NOT NULL, `Sbirthday` DATETIME DEFAULT NULL, `class` VARCHAR(20) NOT NULL ) ENGINE=INNODB DEFAULT CHARSET=utf8; INSERT INTO `student`(`Sno`,`Sname`,`Ssex`,`Sbirthday`,`class`) VALUES ('108','丘东','男','1977-09-01 00:00:00','95033'),('105','匡明','男','1975-10-02 00:00:00','95031'),('107','王丽','女','1976-01-23 00:00:00','95033'),('101','李军','男','1976-02-20 00:00:00','95033'),('109','王芳','女','1975-02-10 00:00:00','95031'),('103','陆君','男','1974-06-03 00:00:00','95031'); CREATE TABLE `teacher` ( `Tno` VARCHAR(20) NOT NULL, `Tname` VARCHAR(20) NOT NULL, `Tsex` VARCHAR(20) NOT NULL, `Tbirthday` DATETIME DEFAULT NULL, `Prof` VARCHAR(20) DEFAULT NULL, `Depart` VARCHAR(20) NOT NULL ) ENGINE=INNODB DEFAULT CHARSET=utf8; INSERT INTO `teacher`(`Tno`,`Tname`,`Tsex`,`Tbirthday`,`Prof`,`Depart`) VALUES ('804','李诚','男','1958-12-02 00:00:00','副教授','计算机系'),('856','张旭','男','1969-03-12 00:00:00','讲师','电子工程系'),('825','王萍','女','1972-05-05 00:00:00','助教','计算机系'),('831','刘冰','女','1977-08-14 00:00:00','助教','电子工程系');
时间: 2023-09-05 08:15:18 浏览: 81
使用Python代码调用命令行来执行MySQL数据库备份和恢复操作
下面是一个 Python 代码示例,用于在 MySQL 中创建存储过程和插入数据:
```python
import mysql.connector
# 连接 MySQL 数据库
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="yourdatabase"
)
# 获取游标
mycursor = mydb.cursor()
# 创建存储过程
mycursor.execute("""
CREATE PROCEDURE myprocedure()
BEGIN
SELECT Sname, Cname, Degree
FROM student, course, score
WHERE student.Sno = score.Sno AND course.Cno = score.Cno;
END
""")
# 提交更改
mydb.commit()
# 插入数据
mycursor.execute("""
CREATE DATABASE IF NOT EXISTS test;
USE `test`;
CREATE TABLE IF NOT EXISTS `course` (
`Cno` VARCHAR(20) NOT NULL,
`Cname` VARCHAR(20) NOT NULL,
`Tno` VARCHAR(20) NOT NULL
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `course`(`Cno`,`Cname`,`Tno`) VALUES ('3-105','计算机导论','825'),('3-245','操作系统','804'),('6-166','数字电路','856'),('9-888','高等数学','831');
CREATE TABLE IF NOT EXISTS `score` (
`Sno` VARCHAR(20) NOT NULL,
`Cno` VARCHAR(20) NOT NULL,
`Degree` DECIMAL(4,1) DEFAULT NULL
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `score`(`Sno`,`Cno`,`Degree`) VALUES ('103','3-245','86.0'),('105','3-245','75.0'),('109','3-245','68.0'),('103','3-105','92.0'),('105','3-105','88.0'),('109','3-105','76.0'),('101','3-105','64.0'),('107','3-105','91.0'),('108','3-105','78.0'),('101','6-166','85.0'),('107','6-166','79.0'),('108','6-166','81.0');
CREATE TABLE IF NOT EXISTS `student` (
`Sno` VARCHAR(20) NOT NULL,
`Sname` VARCHAR(20) NOT NULL,
`Ssex` VARCHAR(20) NOT NULL,
`Sbirthday` DATETIME DEFAULT NULL,
`class` VARCHAR(20) NOT NULL
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `student`(`Sno`,`Sname`,`Ssex`,`Sbirthday`,`class`) VALUES ('108','丘东','男','1977-09-01 00:00:00','95033'),('105','匡明','男','1975-10-02 00:00:00','95031'),('107','王丽','女','1976-01-23 00:00:00','95033'),('101','李军','男','1976-02-20 00:00:00','95033'),('109','王芳','女','1975-02-10 00:00:00','95031'),('103','陆君','男','1974-06-03 00:00:00','95031');
CREATE TABLE IF NOT EXISTS `teacher` (
`Tno` VARCHAR(20) NOT NULL,
`Tname` VARCHAR(20) NOT NULL,
`Tsex` VARCHAR(20) NOT NULL,
`Tbirthday` DATETIME DEFAULT NULL,
`Prof` VARCHAR(20) DEFAULT NULL,
`Depart` VARCHAR(20) NOT NULL
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `teacher`(`Tno`,`Tname`,`Tsex`,`Tbirthday`,`Prof`,`Depart`) VALUES ('804','李诚','男','1958-12-02 00:00:00','副教授','计算机系'),('856','张旭','男','1969-03-12 00:00:00','讲师','电子工程系'),('825','王萍','女','1972-05-05 00:00:00','助教','计算机系'),('831','刘冰','女','1977-08-14 00:00:00','助教','电子工程系');
""")
# 提交更改
mydb.commit()
# 调用存储过程
mycursor.callproc("myprocedure")
# 获取存储过程返回的值
for result in mycursor.stored_results():
print(result.fetchall())
```
在上述代码中,我们首先连接到了 MySQL 数据库,然后使用游标执行了一个 CREATE PROCEDURE 语句,该语句用于创建一个名为 myprocedure 的存储过程。
接着,我们插入了一些数据,并提交了更改。
最后,我们使用游标调用了存储过程,并使用了一个 for 循环遍历了存储过程返回的值。
请注意,这只是一个简单的示例,实际情况下,你可能需要根据你的需求进行更改。
阅读全文