In-depth explanation of the failure of writing SQL containing English single quotation marks "''"

In-depth explanation of the failure of writing SQL containing English single quotation marks "''"

I have encountered a problem. When testing the editing of information, if the edited content contains a single quote ('), it will fail to be written to the MySQL database. Use the following code to print the error:

if (!this->m_sql_query.prepare(update_string))
{
printf("exec fail—>%s\n", this->m_sql_query.lastError().text().toStdString().c_str());
}
if (!this->m_sql_query.exec())
{
printf("exec fail—>%s\n", this->m_sql_query.lastError().text().toStdString().c_str());
}

The printed result is: exec fail—>syntax error unable to execute statement

Please refer to the following relevant information:


insert image description hereinsert image description here

After analysis, it was found that the string in the inserted content was truncated at the single quotes when editing the SQL statement, resulting in a syntax error.

Solution : Replace the characters containing single quotes (') with ('').

SQL uses two single quotes instead of one single quote.

str = str.replace(” '“, ”''“);

This is the end of this article about the in-depth explanation of the problem of failure to write English single quotes "'" in SQL. For more related content about failure to write English single quotes "'" in SQL, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to handle single quotes and double quotes in SQL statements
  • Explanation of single and double quotes in PostgreSQL
  • Mybatis automatically adds single quotes to the generated SQL statement
  • A detailed analysis of the murder caused by a misplaced double quote in MySQL
  • How to use single quotes and double quotes in SQL statements

<<:  Docker Machine in-depth explanation

>>:  Some points on using standard HTML codes in web page creation

Recommend

How to configure ssh to log in to Linux using git bash

1. First, generate the public key and private key...

Example of ellipsis when CSS multi-line text overflows

Ellipses appear when multi-line text overflows Th...

Design reference WordPress website building success case

Each of these 16 sites is worth reading carefully,...

Detailed explanation of common commands in Docker repository

Log in docker login Complete the registration and...

17 excellent web designs carefully crafted by startups

Startups often bring us surprises with their unco...

Solution - BASH: /HOME/JAVA/JDK1.8.0_221/BIN/JAVA: Insufficient permissions

1) Enter the folder path where the jdk file is st...

Implementation of MySQL asc and desc data sorting

Data sorting asc, desc 1. Single field sorting or...

Docker Compose network settings explained

Basic Concepts By default, Compose creates a netw...

JS object copying (deep copy and shallow copy)

Table of contents 1. Shallow copy 1. Object.assig...

This article will show you the principle of MySQL master-slave synchronization

Table of contents Brief Analysis of MySQL Master-...

Solution to MySQL replication failure caused by disk fullness

Table of contents Case scenario Solving the probl...

Detailed explanation of the frame and rules attributes of the table in HTML

The frame and rules attributes of the table tag c...

Vue.js implements tab switching and color change operation explanation

When implementing this function, the method I bor...