current position:Home>Problems encountered in writing the HTML content of articles into the database during the development of Django blog
Problems encountered in writing the HTML content of articles into the database during the development of Django blog
2022-01-29 15:40:45 【Random code 3000】
Little knowledge , Great challenge ! This article is participating in “ A programmer must have a little knowledge ” Creative activities
pit 1: Python operation mysql The database appears pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax;
The following is python In doing mysql Database insert data code operation :
import pymysql.cursors
# Circular warehousing operation
for ll in range(0, len(wallPaperBeanList)):
# Connect MySQL database
connection = pymysql.connect(host='127.0.0.1', port=3306, user='ad', password='ad', db='AllThingArePower',
charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
# adopt cursor Create cursors
cursor = connection.cursor()
insert_sql = "INSERT INTO 'wallpaper' ('category','view_img','img','created_time','img_tag') VALUES ("+ wallPaperBeanList[ll].category +','+wallPaperBeanList[ll].view_img +','+wallPaperBeanList[ll].img +','+wallPaperBeanList[ll].created_time +','+'null' +')'
# print('category==' + wallPaperBeanList[ll].category + ';view_img==' + str(
# wallPaperBeanList[ll].view_img) + ';img==' + str(wallPaperBeanList[ll].img) + ';created_time==' + str(wallPaperBeanList[ll].created_time) + ';img_tag==' + str(wallPaperBeanList[ll].img_tag))
cursor.execute(insert_sql)
# Submit SQL
connection.commit()
# Close data connection
connection.close()
Copy code
After running, the following exception appears , The complete exception log information is pasted below :
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 ''wallpaper' ('category','view_img','img','created_time','img_tag') VALUES (Origi' at line 1")1
Copy code
No problem with normal text only html Text error reporting , At first I always thought it was mysql There are mistakes in grammar , But I have never found anything that can be changed , After Baidu, there is a blog saying mysql When splicing statements, there may be parameters with double quotation marks , Use pymysql.escape_string() Method to handle the parameters with double quotation marks , This solution didn't solve my problem , Later, I found a better solution , Just don't use % perhaps + Operator to splice SQL sentence , Placeholders should be used . namely execute Second parameter of .
The modified code :
import pymysql.cursors
# Circular warehousing operation
for ll in range(0, len(wallPaperBeanList)):
# Connect MySQL database
connection = pymysql.connect(host='127.0.0.1', port=3306, user='ad', password='ad', db='AllThingArePower',
charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
# adopt cursor Create cursors
cursor = connection.cursor()
cursor.execute('insert into wallpaper (category,view_img,img,created_time,img_tag) values (%s,%s,%s,%s,%s)', (str(wallPaperBeanList[ll].category), str(
wallPaperBeanList[ll].view_img),str(wallPaperBeanList[ll].img),str(wallPaperBeanList[ll].created_time),str(wallPaperBeanList[ll].img_tag)))
# Submit SQL
connection.commit()
# Close data connection
Copy code
pit 2: An error occurred while writing to the database : Mysql Failure , abnormal pymysql.err.InternalError: (1366, "Incorrect string value: '\xF0\x9D\x90\xBF;......
Problem description :
Insert Mysql It failed ,python The code reports the following exception :
pymysql.err.InternalError: (1366, "Incorrect string value: '\xF0\x9D\x90\xBF;......
Cause analysis :
UTF-8 The encoding could be two 、 Three 、 Four bytes .Emoji The expression is 4 Bytes , and Mysql Of utf8 Code the most 3 Bytes , So the data doesn't plug in .
Solution :
modify Mysql Table character set and Pymysql Character set when connecting to Library .
1、 modify Mysql Character set of table
explain : Convert the established table character set to utf8mb4, The sorting rule is changed to utf8mb4_bin
command :alter table TABLE_NAME convert to character set utf8mb4 collate utf8mb4_bin; ( take TABLE_NAME Replace with your table name )
Copy code
Be careful : The collation is not utf8mb4_general_ci, It is utf8mb4_bin, Don't take it for granted
2、 Modify the character set of database connection
conn = pymysql.connect(host='localhost', user='root', password='root', port=3306, db='cncb', charset='utf8mb4')
Copy code
copyright notice
author[Random code 3000],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201291540434723.html
The sidebar is recommended
- [Python introduction project] use Python to generate QR code
- Compile D + +, and use d to call C from python
- Quickly build Django blog based on function calculation
- Python collects and monitors system data -- psutil
- Finally, this Python import guide has been sorted out. Look!
- Quickly build Django blog based on function calculation
- Python interface test unittest usage details
- Implementation of top-level design pattern in Python
- You can easily get started with Excel. Python data analysis package pandas (VII): breakdown
- Python simulation random coin toss (non optimized version)
guess what you like
-
Python tiktok 5000+ V, and found that everyone love this video.
-
Using linear systems in python with scipy.linalg
-
Using linear systems in python with scipy.linalg
-
Together with Python to do a license plate automatic recognition system, fun and practical!
-
You can easily get started with Excel. Python data analysis package pandas (XI): segment matching
-
Advanced practical case: Javascript confusion of Python anti crawling
-
Using linear systems in python with scipy.linalg
-
Fast power modulus Python implementation of large numbers
-
Quickly build Django blog based on function calculation
-
This paper clarifies the chaotic switching operation and elegant derivation of Python
Random recommended
- You can easily get started with Excel pandas (I): filtering function
- You can easily get started with Excel. Python data analysis package pandas (II): advanced filtering (I)
- You can easily get started with Excel. Python data analysis package pandas (2): advanced filtering (2)
- You can easily get started with Excel. Python data analysis package pandas (3): making score bar
- Test Development: self study Dubbo + Python experience summary and sharing
- You can easily get started with Excel. Python data analysis package pandas (V): duplicate value processing
- How does Python correctly call jar package encryption to get the encrypted value?
- Python 3 interview question: give an array. If there is 0 in the array, add a 0 after 0, and the overall array length remains the same
- Python simple Snake game (single player mode)
- Using linear systems in python with scipy.linalg
- Python executes functions and even code through strings! Come and understand the operation of such a top!
- Decoding the verification code of Taobao slider with Python + selenium, the road of information security
- [Python introduction project] use Python to generate QR code
- Vanessa basks in her photos and gets caught up in the golden python. There are highlights in the accompanying text. She can't forget Kobe after all
- [windows] Python installation pyteseract
- [introduction to Python project] create bar chart animation in Python
- Fundamentals of Python I
- Python series tutorials 116
- Python code reading (chapter 35): fully (deeply) expand nested lists
- Practical series 1 ️⃣ Wechat applet automatic testing practice (with Python source code)
- Python Basics: do you know how to use lists?
- Solution of no Python 3.9 installation was detected when uninstalling Python
- [Python homework] coupling network information dissemination
- [common links of Python & Python]
- Python application software development tool - tkinterdesigner v1.0 5.1 release!
- [Python development tool tkinterdiesigner]: example: develop stock monitoring alarm using Tkinter desinger
- [Python development tool Tkinter designer]: Lecture 2: introduction to Tkinter designer's example project
- [Python development tool Tkinter designer]: Lecture 1: introduction to the basic functions of Tkinter Designer
- [introduction to Python tutorial] use Python 3 to teach you how to extract any HTML main content
- Python socket implements UDP server and client
- Python socket implements TCP server and client
- leetcode 1261. Find Elements in a Contaminated Binary Tree(python)
- [algorithm learning] 1486 Array XOR operation (Java / C / C + + / Python / go / trust)
- leetcode 1974. Minimum Time to Type Word Using Special Typewriter(python)
- The mobile phone uses Python to operate picture files
- [learning notes] Python exception handling try except...
- Two methods of using pandas to read poorly structured excel. You're welcome to take them away
- Python sum (): the summation method of Python
- Practical experience sharing: use pyo3 to build your Python module
- Using Python to realize multitasking process