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 connect to mysql database data

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to use python to connect to mysql database data. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Foreword:

Using python to connect to mysql database data

There are two recommended ways to read data:

One is to operate through cursors and fetch series methods, and the other is to read and operate through pandas's read_sql (). Each method has its own advantages and disadvantages, which can be selected and used according to the specific situation.

Examples are as follows:

1.fetchone/fetchmany/fetchall

Get one, more, all of them.

Import pymysql# database related information dbHost = 'xxxxxxx'dbUser =' xxx'dbPassword ='* 'dbName =' xxx'dbCharset = 'utf8'conn = pymysql.connect (host=dbHost, port=3306, user=dbUser, password=dbPassword, db=dbName, charset=dbCharset) # get cursor object cs = conn.cursor () # through cursor object Execute sql statement The return value is the number of rows of the affected record r = cs.execute ('select * from goods') # get a piece of data print (cs.fetchone ()) # first piece of data print ("=") # execute again to get the second piece of data print (cs.fetchone ()) # second piece of data # get multiple pieces of data print (cs.fetchmany (3)) # specify the number of entries # get all data print (cs.fetchall ()) # execute again What will be obtained will be an empty tuple, because the fetchall above has been fetched (cursors can be understood as a mark for the acquisition location) print (cs.fetchall ()) # when the acquisition is finished, the query data is returned as () # after the acquisition is finished, close the cursor and database connection # close the cursor cs.close () # close the connection conn.close () 2.pandas.read_sql ()

Use the read_sql () function of the pandas library to get the data, and you will get a DataFrame.

Import pymysqlimport pandas as pd# database related information dbHost = 'xxxxxxx'dbUser =' xxx'dbPassword ='* 'dbName =' xxx'dbCharset = 'utf8'conn = pymysql.connect (host=dbHost, port=3306, user=dbUser, password=dbPassword, db=dbName, charset=dbCharset) sql = "select xxxxxxxxxxxxxxxxxxxxxxxxxxxx" df = pd.read_sql (sql, conn) print (df) # close connection conn.close () about "how to connect mysql database data using python" this article ends here Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

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