码迷,mamicode.com
首页 > 数据库 > 详细

mysql.connector操作mysql的blob值

时间:2016-12-26 21:20:39      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:pen   effective   bin   object   over   line   this   code   asp   

This tutorial shows you how to work with MySQL BLOB data in Python, with examples of updating and reading BLOB data.

The  authors table has a column named  photo whose data type is BLOB. We will read data from a picture file and update to the photo column.

Updating BLOB data in Python

First, we develop a function named  read_file() that reads a file and returns the file’s content:

Second, we create a new function named  update_blob() that updates photo for an author specified by author_id .

Let’s examine the code in detail:

  1. First, we call the  read_file() function to read data from a file and return it.
  2. Second, we compose an UPDATE statement that updates photo column for an author specified by author_id . The  args variable is a tuple that contains file data andauthor_id . We will pass this variable to the  execute() method together with the query .
  3. Third, inside the  try except block, we connect to the database, instantiate a cursor, and execute the query with args . To make the change effective, we call commit() method of the MySQLConnection object.
  4. Fourth, we close the cursor and database connection in the  finally block.

Notice that we imported MySQLConnection and Error objects from the MySQL Connector/Python package and  read_db_config() function from the  python_mysql_dbconfig module that we developed in the previous tutorial.

Let’s test the  update_blob() function.

Notice that you can use the following photo and put it into the pictures folder for testing.

技术分享

Inside the main function, we call the  update_blob() function to update the photo column for the author with id 144. To verify the result, we select data from the  authors table.

技术分享

It works as expected.

Reading BLOB data in Python

In this example, we select BLOB data from the  authors table and write it into a file.

First, we develop a  write_file() function that write a binary data into a file as follows:

Second, we create a new function named  read_blob() as below:

The  read_blob() function reads BLOB data from the  authors table and write it into a file specified by the  filename parameter.

The code is straightforward:

  1. First, we compose a SELECT statement that retrieves photo of a specific author.
  2. Second, we get the database configuration by calling the  read_db_config() function.
  3. Third, inside the  try except block, we connect to the database, instantiate cursor, and execute the query. Once we got the BLOB data, we use the  write_file() function to write it into a file specified by the filename .
  4. Fourth, in the finally block, we close the cursor and database connection.

Now, let’s test the  read_blob() function.

If you open the output folder in the project and see a picture there, it means that you have successfully read the blob from the database.

mysql.connector操作mysql的blob值

标签:pen   effective   bin   object   over   line   this   code   asp   

原文地址:http://www.cnblogs.com/hushaojun/p/6223505.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!