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

Steps to operate Mysql in Python Development

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "the steps of operating Mysql in Python development". In the daily operation, I believe that many people have doubts about the steps of operating Mysql in Python development. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "the steps of operating Mysql in Python development". Next, please follow the editor to study!

Operating Mysql steps in Python Development

Code implementation:

1. # install the package-pymysql sudo pip install pymysql first

2. # call from pymysql import in the program after #

3. The connection object is used to establish a connection to the database to create the object: call the connect () method''

4. Conn = connect (parameter list)

5. '' parameter list:

6. Host: connect to the MySQL host, or "localhost" if it is local.

7. Port: connect to MySQL host port. Default is 3306.

8. Database: database name

9. User: the user name of the connection

10. Password: password of the connection

11. Charset: the coding method used for communication. Utf8 is recommended.

12.''

13.'' Connection object method

14. Close () closes the connection

15. Commit () submit

16. Rollback () rollback

17. Cursor () returns

18. Cursor object for executing sql statements

19. For example: select,insert,update,delete'

20. Cs1 = conn.cursor ()

21. '' Cursor object method

twenty-two。 Close () closes

23. Execute (operation [, parameters]) executes statements and returns the number of affected rows, which is mainly used to execute insert, update, delete statements

24. You can also execute statements such as create, alter, drop, etc.

25. When fetchone () executes the query statement, it gets the first row of data in the query result set and returns a tuple

twenty-six。 When fetchall () executes the query, it takes all the rows of the result set, one row forms a tuple, and then loads these tuples into a tuple to return''.

twenty-seven。 ''

twenty-eight。 Cursor object Properties

twenty-nine。 Rowcount read-only property, which represents the most recent

thirty。 Number of rows affected after execute () execution

thirty-one。 Connection gets the current connection object''

thirty-two。 # example

thirty-three。 # create a Connection connection

thirty-four。 Conn = connect (host='localhost', port=3306, user='root', password='mysql', database='python1'

thirty-five。 Charset='utf8')

thirty-six。 # get the Cursor object cs = conn.cursor ()

thirty-seven。 # Update #

thirty-eight。 Sql = 'update students set name= "Liu Bang" where id=6'

thirty-nine。 # Delete

forty。 # sql = 'delete from students where id=6'

forty-one。 # execute select statement and return the number of affected rows: query a piece of student data

forty-two。 Sql = 'select id,name from students where id = 7'

forty-three。 # sql = 'SELECT id,name FROM students WHERE id = 7' count=cs.execute (sql)

forty-four。 # print the number of lines affected

45.print count

At this point, the study of "the steps to operate Mysql in Python development" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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