python - 如何将mysql局部变量传递给python?
问题描述
我想使用 MySQLdb 模块将局部变量从 MySQL 传递给 python。
我知道有很多关于其他方式的答案(从 python 到 mysql),我不是在寻找那个。
我也知道如何使用从表中获取任何数据
SELECT data_name
FROM table_name
可以获取受影响的行以获取结果。
但是,SELECT @my_variable
从 python 运行 a 返回一个空结果。
LOCK TABLES `usedID` WRITE;
/...*Do some stuff here*/
SELECT `lastaugID` FROM `usedID` INTO @ID;
UNLOCK TABLES;
SELECT @ID;
从 mysql 执行上面的代码片段会返回预期的结果:
+-------+
| @ID |
+-------+
| 58404 |
+-------+
1 row in set (0.00 sec)
但是传递给 python 的值是一个空列表。
这是进行 SQL 查询的 python 片段:
def write_sql(self, sqlreq, sqlreqdict):
if self.sockname:
con = MySQLdb.Connect( unix_socket=self.sockname, user=self.sqluser,
passwd=self.sqlpasswd, db=self.sqldb,
cursorclass=MySQLdb.cursors.DictCursor );
else:
con = MySQLdb.Connect( host=self.sqlhost, user=self.sqluser,
passwd=self.sqlpasswd, db=self.sqldb,
cursorclass=MySQLdb.cursors.DictCursor );
cursor = con.cursor();
try:
cursor.execute( sqlreq, sqlreqdict );
except MySQLdb.IntegrityError, val:
html.print_errmsg_exit( "Database integrity error: " + str(val) );
res = cursor.fetchall();
del cursor;
con.commit();
con.close();
# Re-open the read-only cursors of the DB, to be able to process
# a request to read the just-created file:
self.init_db();
return res
PS:为什么不直接从表中读取?我需要锁定表并在解锁之前读取值。如果我在解锁后阅读,我可能会得到一个已被另一个会话更改的值。
解决方案
推荐阅读
- .net - 在 Visual Studio 中使用标题添加 Web 引用
- reactjs - 与样式 MUI 一起导出 HOC 组件
- excel - Excel 宏用于搜索关键字并将整行复制到另一张工作表
- python-3.x - AttributeError:“函数”对象没有属性“预测”。喀拉斯
- ios - FSCalendar 选择其他人时隐藏今天的图像
- r - 从终端 R 脚本运行中删除库导入打印
- dependencies - 如何将lib文件夹从WEB-INF提取到其他位置
- angular - 提供 ng 服务时出现 CLI 错误
- python - 如何在 django 的基于类的列表视图中添加点赞按钮
- c++ - 将 cv::Mat 打印到 std::cout 与 operator<< 多维数组的内存异常错误