Store binary data to MySQL using escape_string in MySQLdb
2008-12-02 15:00
423 查看
mediumblob can store data less than 16M:
mysql> desc images;
+-------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| image | mediumblob | YES | | NULL | |
+-------+------------+------+-----+---------+----------------+
#!/usr/bin/env python
# --*-- coding=utf-8 --*--
import MySQLdb
conn = MySQLdb.connect('192.168.100.168', 'root', 'pass', 't')
cursor = conn.cursor()
f = open('poppy.jpg', 'rb')
bin_data = f.read()
f.close()
stmt = 'insert into images (image) values("%s")' % (conn.escape_string(bin_data))
cursor.execute(stmt)
or using special format below, caution, there is no quoted characters enclosed for %s:
stmt = 'insert into images (image) values(%s)'
cursor.execute(stmt, (bin_data))
mysql> desc images;
+-------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| image | mediumblob | YES | | NULL | |
+-------+------------+------+-----+---------+----------------+
#!/usr/bin/env python
# --*-- coding=utf-8 --*--
import MySQLdb
conn = MySQLdb.connect('192.168.100.168', 'root', 'pass', 't')
cursor = conn.cursor()
f = open('poppy.jpg', 'rb')
bin_data = f.read()
f.close()
stmt = 'insert into images (image) values("%s")' % (conn.escape_string(bin_data))
cursor.execute(stmt)
or using special format below, caution, there is no quoted characters enclosed for %s:
stmt = 'insert into images (image) values(%s)'
cursor.execute(stmt, (bin_data))
相关文章推荐
- Using Presto to combine data from Hive and MySQL in one SQL-like query
- How To Generate An XML File As A Target Datastore Using ODI In An Integration Interface ? [ID 454268
- Configure HUE to store data in MySQL
- Upload Data from Excel File in ABAP using TEXT_CONVERT_XLS_TO_SAP
- Convert string to binary and binary to string in C#
- Create DataStore using FDO API in Map 3D
- Read and Write compressed data to a binary file using ASP.NET
- mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication
- Using DataLinks to get or edit a connection string
- csharp:Learn how to post JSON string to generic Handler using jQuery in ASP.Net C#.
- BI Java 补丁错误处理 :Cannot login to the SAP J2EE Engine using user and password as provided in the Filesystem Secure Store. Enter va
- mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication错误解决办法
- #455 – Using ItemContainerStyle to Bind Data Elements in a Collection to a Grid
- mysql_connect(): mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication.
- MySQL:Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. INSERT...
- MySQL:Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEM
- mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication解决办法
- mysqlnd cannot connect to MySQL 4.1+ using the old insecure【解决方法】
- Could not store transport type data for Receive Location 'Recv.Loc' to config store. Primary SSO Server 'Sql-server' failed. The external credentials in the SSO database are more recent.
- Different ways how to escape an XML string in C# (zz)