In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Most people do not understand the knowledge of this article "how to install and use pymysql", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to install and use pymysql" article.
1. Pymysql
There are many third-party libraries about interacting with databases in Python language. Take MySQL database as an example, there are mysqldb, mysqlclient, pymysql and so on.
Among the three, pymysql library is recommended by individuals. It is not only easy to install, but also easy to use.
Installation
The installation of pymysql is very simple, just like most libraries. Just enter the following command at the terminal:
Pip install pymysql
You only need to wait a moment to use the pymysql library.
If because of the network speed problem, you can also use the mirror source to install the library, such as Tsinghua Source, Taobao Source and so on.
Pip install pymysql-I ['Tsinghua Source URL'] uses # import module
Import pymysql
# create a connection
Conn = pymysql.connect (
Host=' database address, such as localhost, 127.0.0.1'
User=' user name'
Password=' password'
Database=' database name'
)
# create cursors. Cursors are somewhat similar to pointers, the mechanism of extracting one record at a time from a result set that includes multiple data records.
# # in a nutshell, cursors are like temporary database objects that store copies of rows of data in database tables, or pointers to rows of data stored in the data.
# # cursors are used to execute sql statements later.
Cursor = conn.cursor () # is set so that the subsequent result set appears as a tuple.
# cursor = conn.cursor (pymysql.cursors.DictCursor) # cursors, the result is in the form of a dictionary.
# sql statement
Sql = 'SELECT * FROM table'''
# execute sql statement
Cursor.execute (sql)
# submit the database. When you insert data, if you do not perform this step, the data will not be imported into the database.
Conn.commit ()
# close cursors and databases to free memory. This is a good habit!
Cursor.close ()
Conn.close () II. Sql query statement SELECT
The language of sql is not difficult to learn, but it may be a little complicated later. Programmers often use the sql language to add, delete, modify and query the database, and so on. The following is mainly about the query statement SELECT.
1. Get the data of a column in the table sql = 'SELECT name from tbl_role''''
Cursor.execute (sql)
# fetchall gets the contents of all queries
A = cursor.fetchall ()
For i in a:
Print (a)
At the same time, you can get multiple columns or all of the content. The sql statement is as follows:
SELECT * FROM tbl_roleSELECT column1, column2, column3 FROM tbl_role2, DISTINCT keywords
Combined with the SELECT statement, duplicate records will be removed, leaving unique information.
Sql = 'SELECT DISTINCT age FROM tbl_role'''3, ORDER BY sort
The ORDER BY keyword acts as an automatic sort, which is sorted in ascending order (ASC) by default. If you want to sort in descending order, you can add the keyword DESC.
Sql = 'SELECT * FROM tbl_role ORDER BY age DESC'''4, LIMIT keywords
All results that meet the criteria specified in the SQL statement are returned by default.
Sql =''SELECT * from tbl_role LIMIT 5 years'
5 means to get the first five lines of the list.
Sql =''SELECT * from tbl_role LIMIT 3,5 years'
If the number is indexed as a location in the program, the default is to start at 0. Therefore, 3 means to get the information of the first five lines from the fourth position.
The above is about the content of this article "how to install and use pymysql". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
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.