How to insert special characters in mysql database using python

How do I insert a value in MySQL that consist of single quotes. i.e

"I am joy's brother and trying to get a 'job' "

The single quote is creating a problems.When I am trying to write some text using single quotes in text field. I am getting an error in the console i.e,.

SyntaxError: Unexpected token < in JSON at position 0

How do you insert single quotes through the user interface (front end) using angularJs ,MySQL.


To insert a special character such as “ ‘ “ (single quote) into MySQL, you need to use \’ escape character. The syntax is as follows −

insert into yourTableName(yourColumnName) values(' yourValue\’s ');

To understand the above syntax, let us create two tables. The query to create first table is as follows −

mysql> create table AvoidInsertErrorDemo
-> (
-> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> Sentence text
-> );
Query OK, 0 rows affected (0.53 sec)

Now you can insert special character such as ‘ in the table using insert command. The query is as follows −

mysql> insert into AvoidInsertErrorDemo(Sentence) values('a woman\'s hat');
Query OK, 1 row affected (0.16 sec)
mysql> insert into AvoidInsertErrorDemo(Sentence) values('Mrs. Chang\'s house');
Query OK, 1 row affected (0.31 sec)

Display all records from the table using select statement:

mysql> select *from AvoidInsertErrorDemo;

The following is the output −

+----+--------------------+
| Id | Sentence           |
+----+--------------------+
|  1 | a woman's hat      |
|  2 | Mrs. Chang's house |
+----+--------------------+
2 rows in set (0.00 sec)

How to insert special characters in mysql database using python

Updated on 30-Jul-2019 22:30:25

  • Related Questions & Answers
  • How to display a single quote text as a column value in MySQL?
  • How do I insert multiple values in a column with a single MySQL query?
  • How do I insert a NULL value in MySQL?
  • How to insert into two tables using a single MySQL query?
  • How do I insert a JPEG image into a Python Tkinter window?
  • How do I get the id after INSERT into MySQL database in Python?
  • How do I insert a record from one Mongo database into another?
  • How to insert only a single column into a MySQL table with Java?
  • How can I update MySQL table after quoting the values of a column with a single quote?
  • MySQL: How can I find a value with special character and replace with NULL?
  • How do we insert/store a file into MySQL database using JDBC?
  • How do I INSERT INTO from one MySQL table into another table and set the value of one column?
  • Insert JSON into a MySQL table?
  • Split a string and insert it as individual values into a MySQL table?
  • How do I insert all elements from one list into another in Java?


Using backticks around the column name will allow you to use special characters. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> `Student-Id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> `Student-Name` varchar(100),
   -> `Student-Age` int
   -> );
Query OK, 0 rows affected (0.55 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(`Student-Name`,`Student-Age`) values('Chris',21);
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable(`Student-Name`,`Student-Age`) values('Mike',19);
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable(`Student-Name`,`Student-Age`) values('Bob',18);
Query OK, 1 row affected (0.21 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+------------+--------------+-------------+
| Student-Id | Student-Name | Student-Age |
+------------+--------------+-------------+
|          1 | Chris        | 21          |
|          2 | Mike         | 19          |
|          3 | Bob          | 18          |
+------------+--------------+-------------+
3 rows in set (0.00 sec)

How to insert special characters in mysql database using python

Updated on 30-Jun-2020 09:38:05

  • Related Questions & Answers
  • Append special characters to column values in MySQL
  • MySQL query to retrieve only the column values with special characters?
  • MySQL query to remove special characters from column values?
  • MySQL query to replace special characters from column value
  • How to use special characters in Python Regular Expression?
  • How to use Unicode and Special Characters in Tkinter?
  • MySQL query to select a specific string with special characters
  • Lower case column names with MySQL SELECT?
  • Set Blank spaces in column names with MySQL?
  • MySQL regular expression to update a table with column values including string, numbers and special characters
  • How can we escape special characters in MySQL statement?
  • How to remove special characters from a database field in MySQL?
  • Special Characters in HTML
  • Fetch a specific record from a column with string values (string, numbers and special characters) in MySQL
  • JavaScript regex - How to replace special characters?

How do you insert a special character in Python?

To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.

How do I allow special characters in MySQL query?

MySQL - How to include special characters in a query.
\0 - An ASCII NUL (0x00) character..
\' - A single quote ( ' ) character..
\" - A double quote ( " ) character..
\b - A backspace character..
\n - A newline (linefeed) character..
\r - A carriage return character..
\t - A tab character..
\Z - ASCII 26 (Control-Z)..

How do you insert special characters in SQL?

Solution 3.
select * from table where myfield like '%15\% off%' ESCAPE '\'.
set @myString = replace( replace( replace( replace(@myString,'\','\\'), '%','\%'), '_','\_'), '[','\[').
select * from table where myfield like '%' + @myString + '%' ESCAPE '\'.

How do you add special characters to a database?

Use mysqli_real_escape_string() to Insert Special Characters Into a Database in PHP. To get user input with special characters from the form fields, we use the mysqli_real_escape_string() function. We need the following parameters: database connection and the strings we want to escape.