In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use Python to connect to MySQL database to achieve data storage. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Introduction
MySQL is a relational database. MySQL has become the most popular open source database because of its high performance, low cost and good reliability. It was first developed by Sweden's MySQL AB and later acquired by Oracle.
How to use Python to connect to MySQL database to achieve data storage, we will focus on the following.
Python connects MySQL to realize data storage
First of all, we need to prepare Python's pymysql module, MySQL database (find teaching installation on this network), Navicat Premium 15 database tools (yes or no, just easy to operate)
The pymysql module can be installed in the terminal (cmd) by typing pip install pymysql, or it can be installed in pycharm (there will be no teaching on Baidu, so I won't talk much about it here)
MySQL is also installed by itself, and there is teaching in station b (you must watch the tutorial slowly when installing this. It took me four minutes to debug it).
When the tool environment is ready, let's get started.
Enter the terminal and open it as an administrator (cmd), and enter mysql-h 127.0.0.1-u root-p
Enter your own password to log in (the successful login page is as follows)
After logging in successfully, we don't have to worry about it. Open python (pycharm) and call pymysql first.
Import pymysql # calls pymysql
Open the connection in tool Navicat Premium 15-MySQL
Connection name: localhost port 3306 user name: your account: test the connection after your own confirmation, and click OK if there is no problem.
After confirming, create a new database named python (name all rows) on the left, open the table in python (database, not the programming python), and name the new table test.
Design table, name can be changed, other unchanged
After designing the table of the database, open python (pycharm) and enter the code to connect to the database: (comments are instructive)
# access the local database host to the local name can be the IP address by default: localhost user, account password, password, port, port db, library charset, encoder db= pymysql.connect (host= "localhost", user= "root", password= "12345", port= 3306) Charset= "utf8") # get operation cursor = db.cursor () # query the first data sql = "select * from test" cursor.execute (sql) data = cursor.fetchone () print ("(note: when none is displayed, the database has no data) the data queried are:") print (data)
This function is to query the first piece of data, and the output is the first piece of data in your database.
We now start to write input data and insert data into the database for storage (annotations are illustrative)
Zhanghao = input ("Please enter the account to be saved:") quangbu = ('% s'% zhanghao) # try and except are personally understood as if and else, but if does not use try: # output data into the database cursor.execute ("insert into test (id) values ('% s')"% quangbu) # data is submitted to db.commit () print ("saved successfully") # if it is not successful Failed except Exception as err: print ("failed to modify ", err) db.rollback ()
After you have the foundation of the first item, you can try to write the second password and the third name. You really can't write it and read on.
-
-
-
-
-
-
-
-
-
Let's continue to write the other two.
Mima = input ("Please enter the password to be saved:") mi = ('% s'% mima) mingzi = input ("Please enter the comment to be saved:") zi = ('% s'% mingzi)
After writing these two inputs, we only need to make changes in the line where the output data enters the database
Cursor.execute ("insert into test (id,password,name) values ('% quangbu,mi,zi)
Write the end, close the connection, perfect
Db.close ()
Let's run it.
Initial data
Run the python code
Refresh the Navicat Premium 15 software, and the data we inserted is in our database.
On how to use Python to connect to MySQL database to achieve data storage is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can 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.
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.