In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Python operation MySQL pymsql module is how to use, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Pymsql is a module for operating MySQL in Python. It uses almost the same method as MySQLdb, but mysqldb does not support Python3.
Download and install pip3 install pymysql using operation 1. Execute sqlcards python# python#-*-coding:utf-8-*-import pymysql# create a connection conn = pymysql.connect (host='127.0.0.1', port=3306, user='root', passwd='123456', db='test',charset='utf8') # create a cursor cursor = conn.cursor () # execute SQL And reclaim the number of affected rows effect_row = cursor.execute ("update hosts set host = '1.1.1.2'") # execute conditional SQL and return the number of affected rows # effect_row = cursor.execute ("update hosts set host = '1.1.1.2' where nid >% s", (1,)) # execute SQL And return the number of affected rows # effect_row = cursor.executemany ("insert into hosts (host,color_id) values (% s)", [("1.1.1.11", 1), ("1.1.1.11", 2)]) # submit Otherwise, you cannot save the newly created or modified data conn.commit () # get the latest self-added ID new _ id = cursor.lastrowid# close the cursor cursor.close () # close the connection conn.close () 2. Get query data #! / usr/bin/env python#-*-coding:utf-8-*-import pymysqlconn = pymysql.connect (host='127.0.0.1', port=3306, user='root', passwd='123' Db='t1') cursor = conn.cursor () cursor.execute ("select * from hosts") # get the first row of data row_1 = cursor.fetchone () print (row_1) # get the first n rows of data # row_2 = cursor.fetchmany (3) # get all data row_3 = cursor.fetchall () cursor.close () conn.close () 3.fetch data type
The data obtained by default is the meta-ancestor type. If you want the data of either the dictionary type or the dictionary type, use the setting cursor as the dictionary type:
#! / usr/bin/env python#-*-coding:utf-8-*-import pymysqlconn = pymysql.connect (host='127.0.0.1', port=3306, user='root', passwd='123', db='t1') # cursor is set to dictionary type cursor= conn.cursor (cursor=pymysql.cursors.DictCursor) r = cursor.execute ("select * from hosts") result = cursor.fetchone () for k, v in result.items (): print (k V) cursor.close () conn.close () finished reading the above content Have you mastered how Python operates the pymsql module of MySQL? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.