In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how the Python database SQL operation, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian with you to understand.
Python executes SQL
Realization method
1. Import dependent library
Mainly imports the pymysql library, the support package for database operation. Import commands:
Import pymysql
two。 Connect to the database
Establish a database connection and prepare the data connection cursor.
Sql_1 = "insert user values (123456 Zhang San);" sql_2 = "insert user values (234567)" Li Si ";" sql_3 = "insert user values (345678" Wang Wu ", 3pm" P0003Li ");" sql_4 = "insert user values (456789" Zhao Liu ", 4pm" P0004 "). "cursor.execute (sql_1) cursor.execute (sql_2) cursor.execute (sql_3) cursor.execute (sql_4) conn.commit ()
3. Perform SQL operation
(1) SQL insert data operation
Sql = "SELECT * from user;" cursor.execute (sql) datas = cursor.fetchall ()
(2) SQL query data operation
For data in datas: print (data)
(3) output data
For data in datas: print (data)
(4) close the connection
Cursor.close () conn.close ()
source code
# you need to prepare your database login account in advance: user,passwordimport pymysqlconn = pymysql.connect (host= "127.0.0.1", user= "* *", password= "* *", database= "database_20181103", charset= "utf8") cursor = conn.cursor () sql_1 = "insert user values (123456 Zhang San, 1) insert user values (234567 Li Si, 2) "sql_3 =" insert user values (345678, sql_3) cursor.execute (sql_4) conn.commit () cursor.execute (sql_1) cursor.execute (sql_2) cursor.execute (sql_3) cursor.execute (sql_4) conn.commit () sql = "SELECT * from user" "cursor.execute (sql) datas = cursor.fetchall () for data in datas: print (data) cursor.close () conn.close () Thank you for reading this article carefully. I hope the article" how Python operates the database SQL "shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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
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.