Get the App
SLTechnology News&Howtos  ›  Database  › 

Steps for python3 version to operate mysql

Shulou Source: shulou.com Published: 2022-06-01 19:07:26 09月13日 Update

The following is about the python3 version of the steps to operate mysql, the secret of the text is close to the topic related. So, no gossip, let's go straight to the following, I believe that after reading the python3 version of the steps to operate mysql this article will certainly benefit you.

1. Connect mysql:import pymysql

Conn=pymysql.connect ("localhost", "root", "password", "database name") # Open database connection)

Cursor=db.cursor () # use the cursor () method to create a cursor object cursor)

2. Query data

Conn.query () conn.commit () conn.close () conn=pymysql.connect (=, =) cur=conn.cursor () cur.execute () r cur: (, (cur.rownumber)) (+ (r []) + (r []) cur.close () conn.close () 3, insert data import pymysql#, open database connection db = pymysql.connect ("localhost", "testuser", "test123") "TESTDB") # use the cursor () method to get the operation cursor cursor = db.cursor () # SQL insert statement sql = "" INSERT INTO EMPLOYEE (FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('Mac',' Mohan', 20,'M' 2000) "" try: # execute sql statement cursor.execute (sql) # submit to the database to execute db.commit () except: # roll back db.rollback () # close database connection db.close () 4, create data table import pymysql# Open database connection db = pymysql.connect ("localhost", "testuser", "test123" "TESTDB") # create a cursor object using the cursor () method cursorcursor = db.cursor () # execute SQL using the execute () method Delete cursor.execute ("DROP TABLE IF EXISTS EMPLOYEE") # create table sql = "" CREATE TABLE EMPLOYEE (FIRST_NAME CHAR (20) NOT NULL, LAST_NAME CHAR (20), AGE INT, SEX CHAR (1), INCOME FLOAT) "" cursor.execute (sql) # close database connection db.close () if the table exists

Is there anything you don't understand about the steps of operating mysql in the above python3 version? Or if you want to know more about it, you can continue to follow our industry information section.

Tags: Data databases methods steps versions cursors statements objects the following topics content places mysteries passwords datasheets text more plates articles industries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Apple MariaDB OPPO Reno Redmi