New string formatting methods in the python3.x version _python

Source: Internet
Author: User
Tags string format

We know that python3.x introduced a new string format syntax. Different from the python2.x

Copy Code code as follows:

'%s%s '% (a,b)

Python3.x is
Copy Code code as follows:

' {0} {1} '. Format (A,B)

Today, when I use MYSQLDB, I need to use a parameter
Copy Code code as follows:

Cursor.execute (Sql,param)

Statement to complete the SQL operation. Be misled by the old sayings of other articles, using the

Copy Code code as follows:

Cursor.execute (' INSERT into test values (%s,%s,%s) ', param)

Where Param is a tuple that represents the data to be inserted, and each element in the tuple is the value of each column in the database.
But execution always throws a database exception, an error message:
Copy Code code as follows:

"1064" You have a error in your SQL syntax; Check the manual that corresponds to your MySQL server version for the right syntax to use near ' (%s,%s,%s) ' in line ... (omitted below).

In fact, as long as the change to this is good:
Copy Code code as follows:

Cursor.execute (' INSERT into test values ({0},{1},{2}) ', param)

To tell the truth, Python3 in order to fill in the Python2 of the pits led to backward, making learning Python3 cost improved a lot. But in order not to hinder the development of science and technology, social progress, I still resolutely devote to the new version ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.