我尝试使用存储过程在mysql中将图像插入 blob列。
Python代码:
&
from mysql.connector import pooling
conn_pool = pooling.MySQLConnectionPool(pool_name="PipelinePool", pool_size=5, **DB_CONFIG)
conn = conn_pool.get_connection()
cursor = conn.cursor()
with open('test.jpg', 'rb') as image_file:
image = image_file.read()
cursor.callproc('INSERT_IMG', args=(image))
conn.commit()
但这一行会产生错误:
mysql.connector.errors.InterfaceError:调用存储例程失败; ' UTF8'编解码器不能解码位置0中的字节0xff:无效的起始字节
存储过程代码:
cursor.callproc('INSERT_IMG', args=(image))
答案 0 :(得分:0)
解决。根据塔德曼的评论。
我必须发送十六进制字符串作为参数。
UNHEX()
然后在存储过程中,我使用CREATE PROCEDURE `INSERT_IMG` (IN _image BLOB)
BEGIN
INSERT INTO some_table (IMAGE) VALUE (UNHEX(_image));
END
将十六进制字符串转换为二进制。
<?php
$blocked = [24,32,33,34,35];
$distance = 2;
function getFirstAllowed($i, $blocked, $distance) {
$target = $i-$distance;
while(in_array($target, $blocked)) {
$target--;
}
return $target;
}
// testing:
echo getFirstAllowed(26, $blocked, $distance)."<br>"; // 23
echo getFirstAllowed(27, $blocked, $distance)."<br>"; // 25
echo getFirstAllowed(36, $blocked, $distance)."<br>"; // 31