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 build a drag and drop plugin using vue custom directives

We all know the drag-and-drop feature of HTML5, w...

Write a formal blog using XHTML CSS

The full name of Blog should be Web log, which me...

Web Design Tutorial (7): Improving Web Design Efficiency

<br />Previous article: Web Design Tutorial ...

MySQL index pushdown details

Table of contents 1. Leftmost prefix principle 2....

Robots.txt detailed introduction

Robots.txt is a plain text file in which website ...

JDBC Exploration SQLException Analysis

1. Overview of SQLException When an error occurs ...

Detailed steps for IDEA to integrate docker to achieve remote deployment

1. Enable remote access to the docker server Log ...

JavaScript implements random generation of verification code and verification

This article shares the specific code of JavaScri...

Detailed explanation of JavaScript Reduce

Table of contents map filter some every findIndex...

About the problems of congruence and inequality, equality and inequality in JS

Table of contents Congruent and Incongruent congr...

Implementation of static website layout in docker container

Server placement It is recommended to use cloud s...

How to use libudev in Linux to get USB device VID and PID

In this article, we will use the libudev library ...

XHTML Tutorial: XHTML Basics for Beginners

<br />This site’s original content, please i...