Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python to manipulate mysql database insertion

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report