SoFunction
Updated on 2024-11-15

Inserting data into a mysql database using pymysql in python

Let's just look at the code!

from pymysql import *


def main():
  # Create a connection
  conn = connect(host='', port=3306, database='', user='',
          password='', charset='utf8')
  # Get the cursor object
  cs1 = ()
  # Execute sql statements
  query = 'insert into table name(listings1, listings2, listings3, listings4, listings5, listings6) values(%s, %s, %s, %s, %s, %s)'
  listings1 = (be) worth1
  listings2 = (be) worth2
  listings3 = (be) worth3
  listings4 = (be) worth4
  listings5 = (be) worth5
  listings6 = (be) worth6
  values = (listings1, listings2, listings3, listings4, listings5, listings6)
  (query, values)

  # Commit the previous operation, if multiple executes have already been performed, then commit them all.
  ()

  # Close the cursor object
  ()
  # Close the connection object
  ()


if __name__ == '__main__':
  main()

Additional expansion: remembering a pitfall in learning pymysql insert data

While learning python, do a simple mysql operation with the following correct code:

import 
 
# Getting a database connection
connection = (
  host='',
  port=3310,
  user='root',
  password='root',
  db='wiki'
)
 
try:
  # Get session pointer
  with () as cursor:
    # Create sql statements
    sql = "insert into `user`(`name`,`age`,`sex`) values (%s,%s,%s)"
 
    # Execute sql
    (sql,("lisi",18,"f"))
    # (sql)
    # Submitted
    ()
except:
  print("something wrong")
  ()
finally:
  ()

However, a small problem in the whole process is really a little bit difficult, in the runtime it always reported the following error:

look at the problem I see is the db to establish a connection, but check half a day also do not think that db assignment has any problems, and then look at the last line of the %d format problems, it is natural to think that the back of the insertion of the assignment of the problem, but still did not find the problem, and then the assignment of the value of the statement directly in the sql statement, such as: "insert into `user`(`name`,`age`,`sex`) values ('zhangsan',18,`f')" (think that could not go wrong again), the results are still wrong, right), but also to see the problem of the db. ) values ('zhangsan',18,'f')" (think that it is not possible to error again, right), the result is still reported this error, so sure not in the sql assignment problem, back to look at db = "wiki" this sentence, suddenly see port = '3310' this sentence, instantly think of the port is not necessary to add quotes, hold the attitude of trying, the results! okay.

Instantly feel so helpless, look at the console error, completely did not locate the line to go to the port, that is generally prompted by the error in a line and the following to find the cause, the results of this time to run on it!

Finally, the data type should be whatever it is, so be careful and keep it in mind!

Above this in python using pymysql to mysql database insert (insert) data example is all I share with you, I hope to be able to give you a reference, and I hope that you support me more.