Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use python to manipulate mysql database insertion

Shulou Source: shulou.com Published: 2022-06-01 23:44:08 10月03日 Update

This article mainly explains "how to use python to manipulate mysql database insertion", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use python to manipulate mysql database insertion" bar!

Python manipulates the mysql database to insert a new record into a table.

Pycahrm provides a good function, on the right above, you can connect to the database, and manually operate the database inside, and skip the connection steps.

1. First look at the structure of the following table, an car table

1.python process realization

To install a library pymysql first

Import pymysql as mysql# connects to the database, .connect () returns a connection object db= mysql.connect (host= "localhost", port=3306, user= "root", passwd= "123456", db= "testcar") # SQL statement The colon str is the type prompt sql: str = "insert into testcar.car (carid, brand, in_time, out_time)"\ "VALUES ('987' cur', '2012'')" # create a cursor with db (connection object) cur = db.cursor () # execute a database query command with cursor cur, and use result to receive the return value result = cur.execute (sql) print (result) # to submit the current transaction Will be submitted to the database, you can try to execute only the above code, see the result db.commit () # close the cursor object cur.close () # close the connection db.close ()

About the objects and methods related to the pymysql.connect () method, you can take a look at this boss's article, which has relevant parameters and return values.

two。 After completing the process implementation, try to modularize the design

"" in this file Complete the modular implementation of python manipulation mysql "" import pymysql as mysql# connects to the database def connect (db_name): con = mysql.connect (host= "localhost", port=3306, user= "root", passwd= "123456", db=db_name) return con# inserts a record def insert (sql) into the table Db_name): con = connect (db_name) cur = con.cursor () result = cur.execute (sql) con.commit () cur.close () con.close () if result = 1: print ("execution succeeded!") Return

And then call in main.py

# main.pyimport pmysqlsql: str = "insert into testcar.car (carid, brand, in_time, out_time)"\ "VALUES ('asasa',' Ferrari', '2010 Ferrari')" if _ _ name__ = = "_ _ main__": pmysql.insert (sql, "testcar")

At this point, the insert operation of the table can be realized, and the other operations of addition, deletion, query and modification will be more or less the same.

Thank you for reading, the above is the content of "how to use python to manipulate mysql database insertion". After the study of this article, I believe you have a deeper understanding of how to use python to manipulate mysql database insertion, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Data databases objects cursors learning content articles methods modules procedures attempts success more or less the same transactions code colons functions parameters right commands Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Technology MySQL MariaDB Linux