In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
Replace some dates of the month with words
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.