python - Python pymysql : cannot run multiple statements
问题描述
I am working on a project which needs to read a input.sql file and source all the commands in it. It was working fine for all other statements, but for procedures it is not working.
You can try to replicate it yourself using these inputs.
CREATE TABLE IF NOT EXISTS products( Prod_id INT AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(255) NOT NULL, Price INT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP); Insert into products(Name,Price) Values('soap',1); Insert into products(Name,Price) Values('toilet paper',2); Insert into products(Name,Price) Values('Choco taco',3); CREATE PROCEDURE GetAllProducts() BEGIN SELECT * FROM products; END;
But while I am executing a create procedure and call statements, I am unable to do so. Can anyone please help me out?
a few statements as below but, it is not working
with connection.cursor() as cursor:
for statement in script.split(';'): if len(statement) > 0: cursor.execute(statement + ';')
I understand that the issue is because of the ; delimiter, But how can I make sure the whole procedure gets executed?
Cursor of pymysql seems to execute one statement at a time and doesn't allow multiple execution.?
解决方案
因为您要拆分;
,所以该过程将作为两个单独的语句执行:
BEGIN
SELECT * FROM products;
和
END;
这两个都是无效的 SQL。
推荐阅读
- angular - 如何防止从角度 6 中的日期字符串中删除零
- java - 在完全创建实例之前(在同一个构造函数内)JAVA 使用“this”关键字来引用同一个实例时,java 如何解析引用
- c - 如何在 epoll 中使用环回设备(或类似设备)?
- c - 如何使用 switch case 更新值
- c# - C#粘贴文本通过验证
- javascript - 如果复选框也未选中,则始终获得相同的值
- node.js - Nodejs-如何在登录功能中获取用户详细信息
- css - 在图像上制作正方形?
- php - codeIgniter 中的错误滑块轮播 Boostrap
- c# - 按属性分组并在每个属性下列出从 sql 到 C#