Table of Contents
Insert binary image into mysql 1064 error
How to save pictures in binary to mysql
Home Backend Development Python Tutorial How to solve the 1064 error when inserting binary images into mysql using python3

How to solve the 1064 error when inserting binary images into mysql using python3

May 18, 2023 pm 08:22 PM
mysql python

Insert binary image into mysql 1064 error

 conn = pymysql.connect(*)
 cur = conn.cursor()
 os.chdir('/home/jibo/zxcsSpider/images/full/')
 list = os.listdir()
 count1 = 0
 for imagename in list:
     count1 += 1
     f = open(imagename, 'rb')
     data = f.read()
     f.close()
     imagepath = '/home/images/full/' + imagename
     imagebin = pymysql.Binary(data)
     sql = "insert into images(imagename,imagepath,imagebin) values('%s', '%s', '%s')"
     cur.execute(sql, (imagename, imagepath, imagebin))
     conn.commit()
 print('image:' + str(count1))
 cur.close()
 conn.close()
Copy after login

When the above code is run, mysql will report a 1064 error:

pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'cecedd67b6d4716bdbab9fac36e0b2ad1c81cbac.jpg'', ''/home/images/f' at line 1")

Modification: Change ('%s', '%s', '%s') to (%s, %s, %s)

 
 conn = pymysql.connect(*)
 cur = conn.cursor()
 os.chdir('/home/images/full/')
 list = os.listdir()
 count1 = 0
 for imagename in list:
     count1 += 1
     f = open(imagename, 'rb')
     data = f.read()
     f.close()
     imagepath = '/home/images/full/' + imagename
     imagebin = pymysql.Binary(data)
     sql = "insert into images(imagename,imagepath,imagebin) values(%s, %s, %s)"
     cur.execute(sql, (imagename, imagepath, imagebin))
     conn.commit()
 print('image:' + str(count1))
 cur.close()
 conn.close()
Copy after login

Successfully run again;

Due to the interference of some documents, I spent a long time on the Internet to find the cause of the problem. The parameter %s in pymysql does not need to be added with a '' sign. When we insert a string, an error may not be reported for this problem, because the character The string itself also has quotation marks. If you add quotation marks again, an error will not be reported, but if it is in binary format, an error will be reported.

How to save pictures in binary to mysql

MYSQL supports saving pictures to the database, and there are corresponding A special field BLOB (Binary Large Object)

First create a table in your mysql database to store pictures

CREATE TABLE Images(Id INT PRIMARY KEY AUTO_INCREMENT, Data MEDIUMBLOB);
Copy after login

Then use python code to save local pictures to the database

# coding=utf-8
 
import MySQLdb
import sys
 
try:
    fin = open("/home/dsq/tb/8.jpg") #打开本地图片,路径要写自己的
    img = fin.read()
    fin.close()   #读取结束,关闭文件
except IOError as e:
    print "Error %d: %s" % (e.args[0], e.args[1])
    sys.exit(1)   #出现错误打印错误并退出
 
 
try:
    conn = MySQLdb.connect(host="localhost", port=3306, user="root", passwd="#你的数据库密码#", db="数据库名")    #连接到数据库
 
    cursor = conn.cursor()    #获取cursor游标
    cursor.execute("INSERT INTO Images SET Data='%s'" % MySQLdb.escape_string(img))   #执行SQL语句
 
    conn.commit()   #提交数据
    cursor.close()
    conn.close()    #断开连接
except MySQLdb.Error,e:
    conn.rollback()
    print "Error %d: %s" % (e.args[0], e.args[1])
    sys.exit(1)    #出现错误,自动回滚,打印错误并退出
Copy after login

How to solve the 1064 error when inserting binary images into mysql using python3

found that the picture was successfully stored in the database

The above is the detailed content of How to solve the 1064 error when inserting binary images into mysql using python3. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

Python: Automation, Scripting, and Task Management Python: Automation, Scripting, and Task Management Apr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

See all articles