New string formatting methods in the python3.x version

Source: Internet
Author: User
We know that python3.x introduced a new string formatting syntax. Different from the python2.x.

The code is as follows:


'%s%s '% (A, B)


Python3.x is

The code is as follows:


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


Today, when I was using mysqldb, I needed to use a parameter

The code is as follows:


Cursor.execute (Sql,param)


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

The code is as follows:


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


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

The code is as follows:


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


In fact, as long as the change to this is good:

The code is as follows:


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


To tell the truth, Python3 to fill the Python2 of the pits led to the non-compatible, so that the cost of learning Python3 increased a lot. But in order not to hinder the development of science and technology, social progress, I still resolutely devote myself 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.