Online a lot of posts feel older, and stereotyped. I went to MySQL to see the official online python-driven operation, and found that most of the site is not the same.
The drivers that are installed first are:
pip install mysql-connector-python
The above is the command on Ubuntu.
After installation, the sample code developed is as follows:
From __future__ import print_functionfrom decimal Import decimalfrom datetime import datetime, date, Timedeltaimport MySQL . connector# Connect with the MySQL SERVERCNX = Mysql.connector.connect (user= ' Scott ', database= ' employees ') # Get both Buffe Red Cursorscura = Cnx.cursor (buffered=true) CurB = Cnx.cursor (buffered=true) # Query to get employees who joined in a period Defined by-datesquery = ("Select S.emp_no, Salary, From_date, to_date from employees as E" "left JOIN salaries as S USING (emp_no) "" WHERE to_date = Date (' 9999-01-01 ') "" and e.hire_date between date (%s) and date (%s) ") # UPDATE and in SERT statements for the old and new salaryupdate_old_salary = ("UPDATE salaries SET to_date =%s" "WHERE emp_no =%s A ND from_date =%s ") Insert_new_salary = (" INSERT into salaries (emp_no, from_date, to_date, salary) "" VALUES (%s,%s,% S,%s) ") # Select the employees getting a raisecura.execute (query, (date (1, 1), date (+, +))) # Iterate through The result of Curafor (EMp_no, salary, From_date, to_date) in CurA: # Update The old and insert the new salary new_salary = Int (Round (Salary * D Ecimal (' 1.15 ')) Curb.execute (Update_old_salary, (Tomorrow, Emp_no, from_date)) Curb.execute (Insert_new_salary, (Emp_no, Tomorrow, date (9999, 1, 1,), new_salary)) # Commit the changes cnx.commit () Cnx.close ()
Python's MySQL operation