Hướng dẫn how do i know if python is connected to mysql? - làm cách nào để biết python có được kết nối với mysql không?

Tôi đang sử dụng mysqldb (http://mysql-python.sourceforge.net/). Có vẻ như Connection.open và Connection.sqlState () không hoạt động cho tôi. Dưới đây là mã:

def open(self):
    #TODO: check the connection's status
    # self.__conn.open OR self.__conn.sqlstate()
    try:
        print "sqlstate:"+str( self.__conn.sqlstate() )
        print "open?"+str( self.__conn.open )
        return "00000" == self.__conn.sqlstate()
    except Exception as e:
        print "Exception while checking MYSQL Connection:"+str(e) 
        return False

Nhưng khi tôi chạy "dịch vụ sudo mysql dừng lại; ngủ 60; dịch vụ sudo mysql bắt đầu;" để làm thử nghiệm. Đầu ra là như sau. Có vẻ như phần đầu ra sau đây được lặp lại mãi mãi (cuối cùng tôi đã giết quá trình). Khi máy chủ bị hỏng, Connection.open là 1 và Connection.sqlState () là 00000. Nhưng khi máy chủ tăng, Connection.executeMany () vẫn ném ngoại lệ. Bất kỳ ý tưởng? Cảm ơn.

...
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
    2015-10-20 14:09:06 Reconnected to MYSQL.
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
    2015-10-20 14:09:06 Reconnected to MYSQL.
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
    2015-10-20 14:09:06 Reconnected to MYSQL.
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
...

CẬP NHẬT

Tôi đã kiểm tra lại. Đầu ra là như sau. Mỗi giấc ngủ là 10 giây. Đầu ra là ok ngoại trừ kết nối.open là 1 ngay cả khi máy chủ bị hỏng. Nhưng Connection.sqlState () là đúng (HY000).

 2015-10-20 14:35:56 Exception while executing statement:(2006, 'MySQL server has gone away')
2015-10-20 14:35:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
2015-10-20 14:35:56 sqlstate:HY000
2015-10-20 14:35:56 open?1
2015-10-20 14:35:56 sleeping...
2015-10-20 14:36:06 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
2015-10-20 14:36:06 sqlstate:HY000
2015-10-20 14:36:06 open?1
2015-10-20 14:36:06 sleeping...
2015-10-20 14:36:16 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
2015-10-20 14:36:16 sqlstate:HY000
2015-10-20 14:36:16 open?1
2015-10-20 14:36:16 sleeping...
2015-10-20 14:36:26 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
2015-10-20 14:36:26 sqlstate:HY000
2015-10-20 14:36:26 open?1
2015-10-20 14:36:26 sleeping...
2015-10-20 14:36:36 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
2015-10-20 14:36:36 sqlstate:HY000
2015-10-20 14:36:36 open?1
2015-10-20 14:36:36 sleeping...
2015-10-20 14:36:46 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
2015-10-20 14:36:46 sqlstate:HY000
2015-10-20 14:36:46 open?1
2015-10-20 14:36:46 sleeping...
2015-10-20 14:36:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
2015-10-20 14:36:56 sqlstate:HY000
2015-10-20 14:36:56 open?1
2015-10-20 14:36:56 sleeping...
2015-10-20 14:37:06 sqlstate:00000
2015-10-20 14:37:06 open?1
2015-10-20 14:37:06 Reconnected to MYSQL.

Trang chủ »Python» Cơ sở dữ liệu »Kết nối cơ sở dữ liệu Python MySQL bằng Trình kết nối MySQLPython » Databases » Python MySQL Database Connection using MySQL Connector

Trong bài học này, bạn sẽ tìm hiểu cách kết nối cơ sở dữ liệu MySQL trong Python bằng cách sử dụng mô -đun python đầu nối MySQL. Hướng dẫn Python MySQL này trình bày cách phát triển và tích hợp các ứng dụng Python với máy chủ cơ sở dữ liệu MySQL.MySQL Connector Python‘ module. This Python MySQL tutorial demonstrates how to develop and integrate Python applications with a MySQL database server.

Trong Python, chúng ta có thể sử dụng các mô -đun sau để giao tiếp với MySQL.

  • ...
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
    ...
    
    1
  • ...
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
    ...
    
    2
  • ...
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
    ...
    
    3
  • ...
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
    ...
    
    4
  • ...
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
    ...
    
    5

Lưu ý: Trên tất cả các giao diện hoặc mô -đun đều tuân thủ đặc tả API cơ sở dữ liệu Python v2.0 (PEP 249) & NBSP; có nghĩa là cú pháp, phương thức và cách truy cập cơ sở dữ liệu là giống nhau.Above all interfaces or modules are adhere to Python Database API Specification v2.0 (PEP 249) that means the syntax, method, and way of access the database is the same in all.

PEP 249 được thiết kế để khuyến khích và duy trì sự tương đồng giữa các mô -đun Python được sử dụng để truy cập cơ sở dữ liệu. Bằng cách này, trên tất cả các mô -đun đều tuân theo các quy tắc được xác định trong Thông số kỹ thuật API cơ sở dữ liệu Python v2.0 (PEP 249).

Bạn có thể chọn bất kỳ mô -đun nào trên theo yêu cầu của bạn. Cách truy cập cơ sở dữ liệu MySQL vẫn giữ nguyên. Tôi khuyên bạn nên sử dụng bất kỳ hai mô-đun sau:-

  1. Đầu nối MySQL Python
  2. PYMYSQL

Lưu ý: Hướng dẫn này tập trung vào mô -đun Python đầu nối MySQL. Tất cả các ví dụ được tạo bằng cách sử dụng trình kết nối MySQL Python.: This tutorial focuses on the MySQL Connector Python module. All examples are created using MySQL Connector Python.

Ưu điểm và lợi ích của Trình kết nối MySQL Python: -

  • Trình kết nối MySQL Python được viết bằng python thuần túy và việc thực thi các truy vấn cơ sở dữ liệu là tự túc thông qua Python là có đủ khả năng.
  • Đây là một trình điều khiển hỗ trợ chính thức của Oracle để làm việc với MySQL và Python.
  • Nó tương thích với Python 3, được duy trì tích cực.

Cách kết nối cơ sở dữ liệu MySQL trong Python

Hãy cùng xem cách kết nối cơ sở dữ liệu MySQL trong Python bằng cách sử dụng mô -đun python kết nối MySQL.

Đối số cần thiết để kết nối

Bạn cần biết chi tiết sau đây của máy chủ MySQL để thực hiện kết nối từ Python.

Tranh luậnSự mô tả
tên tài khoảnTên người dùng mà bạn sử dụng để làm việc với máy chủ MySQL. Tên người dùng mặc định cho cơ sở dữ liệu MySQL là một gốc.root.
Mật khẩuMật khẩu được cung cấp bởi người dùng tại thời điểm cài đặt máy chủ MySQL. Nếu bạn đang sử dụng Root thì bạn đã thắng cần mật khẩu.
Tên máy chủTên máy chủ hoặc địa chỉ IP mà MySQL đang chạy. Nếu bạn đang chạy trên localhost, thì bạn có thể sử dụng localhost hoặc IP
...
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
    2015-10-20 14:09:06 Reconnected to MYSQL.
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
    2015-10-20 14:09:06 Reconnected to MYSQL.
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
    2015-10-20 14:09:06 Reconnected to MYSQL.
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
...
6 của nólocalhost or its IP
...
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
    2015-10-20 14:09:06 Reconnected to MYSQL.
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
    2015-10-20 14:09:06 Reconnected to MYSQL.
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
    2015-10-20 14:09:06 Reconnected to MYSQL.
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
...
6
Tên cơ sở dữ liệuTên của cơ sở dữ liệu mà bạn muốn kết nối và thực hiện các hoạt động.

Cách kết nối với cơ sở dữ liệu MySQL trong Python

  1. Cài đặt mô -đun kết nối MySQL

    Sử dụng lệnh PIP để cài đặt trình kết nối MySQL Python .________ 17

    ...
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
    ...
    
    7

  2. Nhập mô -đun kết nối MySQL

    Nhập bằng cách sử dụng câu lệnh

    ...
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
    ...
    
    8 để bạn có thể sử dụng các phương thức mô -đun này để liên lạc với cơ sở dữ liệu MySQL.

  3. Sử dụng phương thức Connect ()

    Sử dụng phương thức

    ...
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
    ...
    
    9 của lớp kết nối MySQL với các đối số cần thiết để kết nối MySQL. Nó sẽ trả về một đối tượng
     2015-10-20 14:35:56 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:35:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:35:56 sqlstate:HY000
    2015-10-20 14:35:56 open?1
    2015-10-20 14:35:56 sleeping...
    2015-10-20 14:36:06 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:06 sqlstate:HY000
    2015-10-20 14:36:06 open?1
    2015-10-20 14:36:06 sleeping...
    2015-10-20 14:36:16 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:16 sqlstate:HY000
    2015-10-20 14:36:16 open?1
    2015-10-20 14:36:16 sleeping...
    2015-10-20 14:36:26 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:26 sqlstate:HY000
    2015-10-20 14:36:26 open?1
    2015-10-20 14:36:26 sleeping...
    2015-10-20 14:36:36 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:36 sqlstate:HY000
    2015-10-20 14:36:36 open?1
    2015-10-20 14:36:36 sleeping...
    2015-10-20 14:36:46 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:46 sqlstate:HY000
    2015-10-20 14:36:46 open?1
    2015-10-20 14:36:46 sleeping...
    2015-10-20 14:36:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:56 sqlstate:HY000
    2015-10-20 14:36:56 open?1
    2015-10-20 14:36:56 sleeping...
    2015-10-20 14:37:06 sqlstate:00000
    2015-10-20 14:37:06 open?1
    2015-10-20 14:37:06 Reconnected to MYSQL.
    
    0 nếu kết nối được thiết lập thành công

  4. Sử dụng phương thức con trỏ ()

    Sử dụng phương thức con trỏ () của đối tượng

     2015-10-20 14:35:56 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:35:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:35:56 sqlstate:HY000
    2015-10-20 14:35:56 open?1
    2015-10-20 14:35:56 sleeping...
    2015-10-20 14:36:06 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:06 sqlstate:HY000
    2015-10-20 14:36:06 open?1
    2015-10-20 14:36:06 sleeping...
    2015-10-20 14:36:16 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:16 sqlstate:HY000
    2015-10-20 14:36:16 open?1
    2015-10-20 14:36:16 sleeping...
    2015-10-20 14:36:26 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:26 sqlstate:HY000
    2015-10-20 14:36:26 open?1
    2015-10-20 14:36:26 sleeping...
    2015-10-20 14:36:36 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:36 sqlstate:HY000
    2015-10-20 14:36:36 open?1
    2015-10-20 14:36:36 sleeping...
    2015-10-20 14:36:46 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:46 sqlstate:HY000
    2015-10-20 14:36:46 open?1
    2015-10-20 14:36:46 sleeping...
    2015-10-20 14:36:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:56 sqlstate:HY000
    2015-10-20 14:36:56 open?1
    2015-10-20 14:36:56 sleeping...
    2015-10-20 14:37:06 sqlstate:00000
    2015-10-20 14:37:06 open?1
    2015-10-20 14:37:06 Reconnected to MYSQL.
    
    0 để tạo đối tượng con trỏ để thực hiện các hoạt động SQL khác nhau.

  5. Sử dụng phương thức Execute ()

    Các phương thức EXECUTE () chạy truy vấn SQL và trả về kết quả.

  6. Trích xuất kết quả bằng Fetchall ()

    Sử dụng

     2015-10-20 14:35:56 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:35:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:35:56 sqlstate:HY000
    2015-10-20 14:35:56 open?1
    2015-10-20 14:35:56 sleeping...
    2015-10-20 14:36:06 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:06 sqlstate:HY000
    2015-10-20 14:36:06 open?1
    2015-10-20 14:36:06 sleeping...
    2015-10-20 14:36:16 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:16 sqlstate:HY000
    2015-10-20 14:36:16 open?1
    2015-10-20 14:36:16 sleeping...
    2015-10-20 14:36:26 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:26 sqlstate:HY000
    2015-10-20 14:36:26 open?1
    2015-10-20 14:36:26 sleeping...
    2015-10-20 14:36:36 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:36 sqlstate:HY000
    2015-10-20 14:36:36 open?1
    2015-10-20 14:36:36 sleeping...
    2015-10-20 14:36:46 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:46 sqlstate:HY000
    2015-10-20 14:36:46 open?1
    2015-10-20 14:36:46 sleeping...
    2015-10-20 14:36:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:56 sqlstate:HY000
    2015-10-20 14:36:56 open?1
    2015-10-20 14:36:56 sleeping...
    2015-10-20 14:37:06 sqlstate:00000
    2015-10-20 14:37:06 open?1
    2015-10-20 14:37:06 Reconnected to MYSQL.
    
    2 hoặc
     2015-10-20 14:35:56 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:35:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:35:56 sqlstate:HY000
    2015-10-20 14:35:56 open?1
    2015-10-20 14:35:56 sleeping...
    2015-10-20 14:36:06 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:06 sqlstate:HY000
    2015-10-20 14:36:06 open?1
    2015-10-20 14:36:06 sleeping...
    2015-10-20 14:36:16 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:16 sqlstate:HY000
    2015-10-20 14:36:16 open?1
    2015-10-20 14:36:16 sleeping...
    2015-10-20 14:36:26 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:26 sqlstate:HY000
    2015-10-20 14:36:26 open?1
    2015-10-20 14:36:26 sleeping...
    2015-10-20 14:36:36 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:36 sqlstate:HY000
    2015-10-20 14:36:36 open?1
    2015-10-20 14:36:36 sleeping...
    2015-10-20 14:36:46 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:46 sqlstate:HY000
    2015-10-20 14:36:46 open?1
    2015-10-20 14:36:46 sleeping...
    2015-10-20 14:36:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:56 sqlstate:HY000
    2015-10-20 14:36:56 open?1
    2015-10-20 14:36:56 sleeping...
    2015-10-20 14:37:06 sqlstate:00000
    2015-10-20 14:37:06 open?1
    2015-10-20 14:37:06 Reconnected to MYSQL.
    
    3 hoặc
     2015-10-20 14:35:56 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:35:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:35:56 sqlstate:HY000
    2015-10-20 14:35:56 open?1
    2015-10-20 14:35:56 sleeping...
    2015-10-20 14:36:06 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:06 sqlstate:HY000
    2015-10-20 14:36:06 open?1
    2015-10-20 14:36:06 sleeping...
    2015-10-20 14:36:16 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:16 sqlstate:HY000
    2015-10-20 14:36:16 open?1
    2015-10-20 14:36:16 sleeping...
    2015-10-20 14:36:26 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:26 sqlstate:HY000
    2015-10-20 14:36:26 open?1
    2015-10-20 14:36:26 sleeping...
    2015-10-20 14:36:36 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:36 sqlstate:HY000
    2015-10-20 14:36:36 open?1
    2015-10-20 14:36:36 sleeping...
    2015-10-20 14:36:46 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:46 sqlstate:HY000
    2015-10-20 14:36:46 open?1
    2015-10-20 14:36:46 sleeping...
    2015-10-20 14:36:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:56 sqlstate:HY000
    2015-10-20 14:36:56 open?1
    2015-10-20 14:36:56 sleeping...
    2015-10-20 14:37:06 sqlstate:00000
    2015-10-20 14:37:06 open?1
    2015-10-20 14:37:06 Reconnected to MYSQL.
    
    4 để đọc kết quả truy vấn.

  7. Đóng con trỏ và các đối tượng kết nối

    Sử dụng phương thức

     2015-10-20 14:35:56 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:35:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:35:56 sqlstate:HY000
    2015-10-20 14:35:56 open?1
    2015-10-20 14:35:56 sleeping...
    2015-10-20 14:36:06 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:06 sqlstate:HY000
    2015-10-20 14:36:06 open?1
    2015-10-20 14:36:06 sleeping...
    2015-10-20 14:36:16 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:16 sqlstate:HY000
    2015-10-20 14:36:16 open?1
    2015-10-20 14:36:16 sleeping...
    2015-10-20 14:36:26 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:26 sqlstate:HY000
    2015-10-20 14:36:26 open?1
    2015-10-20 14:36:26 sleeping...
    2015-10-20 14:36:36 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:36 sqlstate:HY000
    2015-10-20 14:36:36 open?1
    2015-10-20 14:36:36 sleeping...
    2015-10-20 14:36:46 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:46 sqlstate:HY000
    2015-10-20 14:36:46 open?1
    2015-10-20 14:36:46 sleeping...
    2015-10-20 14:36:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:56 sqlstate:HY000
    2015-10-20 14:36:56 open?1
    2015-10-20 14:36:56 sleeping...
    2015-10-20 14:37:06 sqlstate:00000
    2015-10-20 14:37:06 open?1
    2015-10-20 14:37:06 Reconnected to MYSQL.
    
    5 và
     2015-10-20 14:35:56 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:35:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:35:56 sqlstate:HY000
    2015-10-20 14:35:56 open?1
    2015-10-20 14:35:56 sleeping...
    2015-10-20 14:36:06 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:06 sqlstate:HY000
    2015-10-20 14:36:06 open?1
    2015-10-20 14:36:06 sleeping...
    2015-10-20 14:36:16 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:16 sqlstate:HY000
    2015-10-20 14:36:16 open?1
    2015-10-20 14:36:16 sleeping...
    2015-10-20 14:36:26 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:26 sqlstate:HY000
    2015-10-20 14:36:26 open?1
    2015-10-20 14:36:26 sleeping...
    2015-10-20 14:36:36 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:36 sqlstate:HY000
    2015-10-20 14:36:36 open?1
    2015-10-20 14:36:36 sleeping...
    2015-10-20 14:36:46 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:46 sqlstate:HY000
    2015-10-20 14:36:46 open?1
    2015-10-20 14:36:46 sleeping...
    2015-10-20 14:36:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:56 sqlstate:HY000
    2015-10-20 14:36:56 open?1
    2015-10-20 14:36:56 sleeping...
    2015-10-20 14:37:06 sqlstate:00000
    2015-10-20 14:37:06 open?1
    2015-10-20 14:37:06 Reconnected to MYSQL.
    
    6 để đóng các kết nối mở sau khi công việc của bạn hoàn thành

Hướng dẫn how do i know if python is connected to mysql? - làm cách nào để biết python có được kết nối với mysql không?
Kết nối cơ sở dữ liệu MySQL trong Python

Chạy truy vấn dưới đây trên bảng điều khiển MySQL nếu bạn chưa tạo bất kỳ cơ sở dữ liệu nào trong MySQL. Nếu không, bạn có thể bỏ qua truy vấn dưới đây.

Tạo cơ sở dữ liệu trong MySQL

Create database Electronics;

Ví dụ để kết nối với cơ sở dữ liệu MySQL trong Python

import mysql.connector
from mysql.connector import Error

try:
    connection = mysql.connector.connect(host='localhost',
                                         database='Electronics',
                                         user='pynative',
                                         password='pynative@#29')
    if connection.is_connected():
        db_Info = connection.get_server_info()
        print("Connected to MySQL Server version ", db_Info)
        cursor = connection.cursor()
        cursor.execute("select database();")
        record = cursor.fetchone()
        print("You're connected to database: ", record)

except Error as e:
    print("Error while connecting to MySQL", e)
finally:
    if connection.is_connected():
        cursor.close()
        connection.close()
        print("MySQL connection is closed")

Output..

Connected to MySQL Server version  5.7.19
You're connected to database:  ('electronics',)
MySQL connection is closed

Điểm để nhớ

  • Các trường hợp ngoại lệ có thể xảy ra trong quá trình này bằng cách nhập lớp lỗi từ mô -đun kết nối MySQL bằng cách sử dụng câu lệnh
     2015-10-20 14:35:56 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:35:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:35:56 sqlstate:HY000
    2015-10-20 14:35:56 open?1
    2015-10-20 14:35:56 sleeping...
    2015-10-20 14:36:06 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:06 sqlstate:HY000
    2015-10-20 14:36:06 open?1
    2015-10-20 14:36:06 sleeping...
    2015-10-20 14:36:16 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:16 sqlstate:HY000
    2015-10-20 14:36:16 open?1
    2015-10-20 14:36:16 sleeping...
    2015-10-20 14:36:26 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:26 sqlstate:HY000
    2015-10-20 14:36:26 open?1
    2015-10-20 14:36:26 sleeping...
    2015-10-20 14:36:36 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:36 sqlstate:HY000
    2015-10-20 14:36:36 open?1
    2015-10-20 14:36:36 sleeping...
    2015-10-20 14:36:46 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:46 sqlstate:HY000
    2015-10-20 14:36:46 open?1
    2015-10-20 14:36:46 sleeping...
    2015-10-20 14:36:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:56 sqlstate:HY000
    2015-10-20 14:36:56 open?1
    2015-10-20 14:36:56 sleeping...
    2015-10-20 14:37:06 sqlstate:00000
    2015-10-20 14:37:06 open?1
    2015-10-20 14:37:06 Reconnected to MYSQL.
    
    7. LớpRror rất hữu ích để gỡ lỗi khi chúng tôi không kết nối với MySQL. Ví dụ: truy cập lỗi bị từ chối khi tên người dùng hoặc mật khẩu sai.
    that may occur during this process by importing the Error class from the MySQL connector module using a
     2015-10-20 14:35:56 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:35:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:35:56 sqlstate:HY000
    2015-10-20 14:35:56 open?1
    2015-10-20 14:35:56 sleeping...
    2015-10-20 14:36:06 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:06 sqlstate:HY000
    2015-10-20 14:36:06 open?1
    2015-10-20 14:36:06 sleeping...
    2015-10-20 14:36:16 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:16 sqlstate:HY000
    2015-10-20 14:36:16 open?1
    2015-10-20 14:36:16 sleeping...
    2015-10-20 14:36:26 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:26 sqlstate:HY000
    2015-10-20 14:36:26 open?1
    2015-10-20 14:36:26 sleeping...
    2015-10-20 14:36:36 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:36 sqlstate:HY000
    2015-10-20 14:36:36 open?1
    2015-10-20 14:36:36 sleeping...
    2015-10-20 14:36:46 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:46 sqlstate:HY000
    2015-10-20 14:36:46 open?1
    2015-10-20 14:36:46 sleeping...
    2015-10-20 14:36:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:56 sqlstate:HY000
    2015-10-20 14:36:56 open?1
    2015-10-20 14:36:56 sleeping...
    2015-10-20 14:37:06 sqlstate:00000
    2015-10-20 14:37:06 open?1
    2015-10-20 14:37:06 Reconnected to MYSQL.
    
    7 statement.
    Error class is useful to debug when we failed to connect to MySQL. For example, ACCESS DENIED ERROR when the username or password is wrong.
  • Phương thức & nbsp; ____ 19 & nbsp; có thể ném ngoại lệ
     2015-10-20 14:35:56 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:35:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:35:56 sqlstate:HY000
    2015-10-20 14:35:56 open?1
    2015-10-20 14:35:56 sleeping...
    2015-10-20 14:36:06 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:06 sqlstate:HY000
    2015-10-20 14:36:06 open?1
    2015-10-20 14:36:06 sleeping...
    2015-10-20 14:36:16 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:16 sqlstate:HY000
    2015-10-20 14:36:16 open?1
    2015-10-20 14:36:16 sleeping...
    2015-10-20 14:36:26 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:26 sqlstate:HY000
    2015-10-20 14:36:26 open?1
    2015-10-20 14:36:26 sleeping...
    2015-10-20 14:36:36 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:36 sqlstate:HY000
    2015-10-20 14:36:36 open?1
    2015-10-20 14:36:36 sleeping...
    2015-10-20 14:36:46 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:46 sqlstate:HY000
    2015-10-20 14:36:46 open?1
    2015-10-20 14:36:46 sleeping...
    2015-10-20 14:36:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:56 sqlstate:HY000
    2015-10-20 14:36:56 open?1
    2015-10-20 14:36:56 sleeping...
    2015-10-20 14:37:06 sqlstate:00000
    2015-10-20 14:37:06 open?1
    2015-10-20 14:37:06 Reconnected to MYSQL.
    
    9 nếu một trong các tham số cần thiết là sai. Ví dụ: nếu bạn cung cấp một tên cơ sở dữ liệu không có trong MySQL. 
    ...
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
        2015-10-20 14:09:06 Reconnected to MYSQL.
        2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
        2015-10-20 14:09:06 sqlstate:00000
        2015-10-20 14:09:06 open?1
    ...
    
    9 method can throw a
     2015-10-20 14:35:56 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:35:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:35:56 sqlstate:HY000
    2015-10-20 14:35:56 open?1
    2015-10-20 14:35:56 sleeping...
    2015-10-20 14:36:06 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:06 sqlstate:HY000
    2015-10-20 14:36:06 open?1
    2015-10-20 14:36:06 sleeping...
    2015-10-20 14:36:16 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:16 sqlstate:HY000
    2015-10-20 14:36:16 open?1
    2015-10-20 14:36:16 sleeping...
    2015-10-20 14:36:26 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:26 sqlstate:HY000
    2015-10-20 14:36:26 open?1
    2015-10-20 14:36:26 sleeping...
    2015-10-20 14:36:36 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:36 sqlstate:HY000
    2015-10-20 14:36:36 open?1
    2015-10-20 14:36:36 sleeping...
    2015-10-20 14:36:46 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:46 sqlstate:HY000
    2015-10-20 14:36:46 open?1
    2015-10-20 14:36:46 sleeping...
    2015-10-20 14:36:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:56 sqlstate:HY000
    2015-10-20 14:36:56 open?1
    2015-10-20 14:36:56 sleeping...
    2015-10-20 14:37:06 sqlstate:00000
    2015-10-20 14:37:06 open?1
    2015-10-20 14:37:06 Reconnected to MYSQL.
    
    9 exception if one of the required parameters is wrong. For example, if you provide a database name that is not present in MySQL.
  • Create database Electronics;
    0 là phương pháp của lớp
     2015-10-20 14:35:56 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:35:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:35:56 sqlstate:HY000
    2015-10-20 14:35:56 open?1
    2015-10-20 14:35:56 sleeping...
    2015-10-20 14:36:06 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:06 sqlstate:HY000
    2015-10-20 14:36:06 open?1
    2015-10-20 14:36:06 sleeping...
    2015-10-20 14:36:16 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:16 sqlstate:HY000
    2015-10-20 14:36:16 open?1
    2015-10-20 14:36:16 sleeping...
    2015-10-20 14:36:26 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:26 sqlstate:HY000
    2015-10-20 14:36:26 open?1
    2015-10-20 14:36:26 sleeping...
    2015-10-20 14:36:36 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:36 sqlstate:HY000
    2015-10-20 14:36:36 open?1
    2015-10-20 14:36:36 sleeping...
    2015-10-20 14:36:46 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:46 sqlstate:HY000
    2015-10-20 14:36:46 open?1
    2015-10-20 14:36:46 sleeping...
    2015-10-20 14:36:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
    2015-10-20 14:36:56 sqlstate:HY000
    2015-10-20 14:36:56 open?1
    2015-10-20 14:36:56 sleeping...
    2015-10-20 14:37:06 sqlstate:00000
    2015-10-20 14:37:06 open?1
    2015-10-20 14:37:06 Reconnected to MYSQL.
    
    0 thông qua đó chúng tôi có thể xác minh là ứng dụng Python của chúng tôi được kết nối với MySQL.
  • Cuối cùng, chúng tôi đang đóng kết nối cơ sở dữ liệu MySQL bằng phương thức
    Create database Electronics;
    2 của lớp MySQLConnection.

Tạo bảng MySQL từ Python

Bây giờ bạn đã biết cách kết nối với máy chủ MySQL từ Python, trong phần này, chúng ta sẽ tìm hiểu cách tạo một bảng trong MySQL từ Python. Hãy cùng tạo ra bảng ‘máy tính xách tay trong cơ sở dữ liệu‘ Electronics.

import mysql.connector

try:
    connection = mysql.connector.connect(host='localhost',
                                         database='Electronics',
                                         user='pynative',
                                         password='pynative@#29')

    mySql_Create_Table_Query = """CREATE TABLE Laptop ( 
                             Id int(11) NOT NULL,
                             Name varchar(250) NOT NULL,
                             Price float NOT NULL,
                             Purchase_date Date NOT NULL,
                             PRIMARY KEY (Id)) """

    cursor = connection.cursor()
    result = cursor.execute(mySql_Create_Table_Query)
    print("Laptop Table created successfully ")

except mysql.connector.Error as error:
    print("Failed to create table in MySQL: {}".format(error))
finally:
    if connection.is_connected():
        cursor.close()
        connection.close()
        print("MySQL connection is closed")

Output::

Laptop Table created successfully 
MySQL connection is closed

Hoạt động của Python mysql crud

Ngoài ra, tìm hiểu cách thực hiện các hoạt động MYSQL khác nhau từ Python bằng cách đề cập đến các bài viết sau.

Nhấp vào từng hướng dẫn để nghiên cứu các hoạt động chi tiết..

  • Chèn các hàng vào bảng MySQL từ Python: Chèn một hàng và nhiều hàng vào bảng MySQL. Ngoài ra, tìm hiểu cách sử dụng các biến Python trong truy vấn được tham số hóa để chèn dữ liệu động vào bảng.
  • Chọn các hàng từ bảng MySQL bằng Python: Thực hiện truy vấn chọn SQL từ ứng dụng Python để tìm nạp các hàng từ bảng MySQL. Ngoài ra, tìm hiểu cách xử lý chọn kết quả truy vấn, tìm nạp tất cả các hàng hoặc hàng đơn từ bảng và đếm tổng số hàng của bảng.
  • Cập nhật các hàng của bảng MySQL từ Python: Cập nhật một hàng duy nhất, nhiều hàng, một cột duy nhất và các cột khác nhau. Ngoài ra, tìm hiểu cách sử dụng các biến Python trong truy vấn được tham số hóa để cập nhật dữ liệu bảng.
  • Xóa các hàng bảng khỏi Python: Xóa một hàng duy nhất, nhiều hàng, một cột duy nhất và các cột khác nhau. Ngoài ra, học cách xóa tất cả các hàng, xóa bảng và toàn bộ cơ sở dữ liệu khỏi MySQL bằng Python.
  • Thực hiện các quy trình lưu trữ MySQL từ Python và tìm hiểu cách chuyển các tham số vào và ra cho thủ tục lưu trữ MySQL.
  • Truy vấn được tham số hóa Python MySQL và câu lệnh đã chuẩn bị: Học cách sử dụng các truy vấn được tham số hóa hoặc câu lệnh đã chuẩn bị để sử dụng các biến Python trong truy vấn SQL để chuyển dữ liệu động đến bảng MySQL.
  • Python MySQL cam kết và rollback để quản lý các giao dịch: Quản lý các giao dịch cơ sở dữ liệu MySQL từ Python để duy trì tính chất axit của các giao dịch MYSQL bằng các phương thức cam kết () và rollback ().
  • Kết nối cơ sở dữ liệu Python với MySQL: Tìm hiểu để tạo và sử dụng nhóm kết nối để tăng hiệu suất của các ứng dụng Python MySQL của bạn.
  • Python MySQL Blob chèn và truy xuất dữ liệu kỹ thuật số: Tìm hiểu để chèn hoặc lấy bất kỳ thông tin kỹ thuật số nào như tệp, hình ảnh, video hoặc bài hát dưới dạng dữ liệu blob vào bảng MySQL từ Python.

Danh sách đối số kết nối Python MySQL

Chúng tôi đã thảo luận về bốn đối số bắt buộc cần thiết để kết nối máy chủ MySQL.

Hãy xem những đối số kết nối khác mà chúng ta có thể sử dụng để liên lạc với máy chủ MySQL từ Python. Dưới đây là danh sách tất cả các đối số kết nối khác và ý nghĩa của chúng.

  • Create database Electronics;
    3: Cổng TCP/IP của máy chủ MySQL. Giá trị này phải là một số nguyên. Chúng tôi có thể chỉ định số cổng khác nhau nếu máy chủ MySQL đang nghe một cổng khác. Giá trị mặc định cho đối số cổng này là 3306.
  • Create database Electronics;
    4: Chỉ định có sử dụng Unicode hay không. Giá trị mặc định là true.
  • Create database Electronics;
    5: Nhân vật MySQL được đặt thành sử dụng, các biến đặt ký tự liên quan đến tương tác máy khách với máy chủ. Có gần 30 đến 40 hỗ trợ máy chủ MySQL Charset. Giá trị mặc định của đối số Charset là UTF8.
  • Create database Electronics;
    6: Đặt nó thành True nếu bạn muốn tự động giao dịch cam kết. Nếu bạn muốn quản lý các giao dịch trong MySQL từ Python, bạn cần đặt giá trị này đúng hoặc sai. Giá trị mặc định là sai, tức là, các thay đổi không được cam kết với cơ sở dữ liệu. Bạn cần gọi rõ ràng một phương thức cam kết để duy trì các thay đổi của bạn trong cơ sở dữ liệu.
  • Create database Electronics;
    7: Để tìm kiếm cảnh báo, điều này rất hữu ích khi biết kết nối được thiết lập nhưng với các cảnh báo. Giá trị mặc định là sai.
  • Create database Electronics;
    8: Đặt nó khi bạn muốn nêu ra một ngoại lệ về cảnh báo. Giá trị mặc định là sai.
  • Create database Electronics;
    9: Thời gian chờ cho các kết nối ổ cắm TCP và UNIX. Kết nối chấm dứt sau khi hết thời gian chờ này.
  • import mysql.connector
    from mysql.connector import Error
    
    try:
        connection = mysql.connector.connect(host='localhost',
                                             database='Electronics',
                                             user='pynative',
                                             password='pynative@#29')
        if connection.is_connected():
            db_Info = connection.get_server_info()
            print("Connected to MySQL Server version ", db_Info)
            cursor = connection.cursor()
            cursor.execute("select database();")
            record = cursor.fetchone()
            print("You're connected to database: ", record)
    
    except Error as e:
        print("Error while connecting to MySQL", e)
    finally:
        if connection.is_connected():
            cursor.close()
            connection.close()
            print("MySQL connection is closed")
    
    0: Nếu đúng, các đối tượng con trỏ lấy kết quả ngay sau khi thực hiện các truy vấn. Giá trị mặc định là sai.
  • import mysql.connector
    from mysql.connector import Error
    
    try:
        connection = mysql.connector.connect(host='localhost',
                                             database='Electronics',
                                             user='pynative',
                                             password='pynative@#29')
        if connection.is_connected():
            db_Info = connection.get_server_info()
            print("Connected to MySQL Server version ", db_Info)
            cursor = connection.cursor()
            cursor.execute("select database();")
            record = cursor.fetchone()
            print("You're connected to database: ", record)
    
    except Error as e:
        print("Error while connecting to MySQL", e)
    finally:
        if connection.is_connected():
            cursor.close()
            connection.close()
            print("MySQL connection is closed")
    
    1: Nếu đúng, kết quả MySQL được trả về AS-IS thay vì chuyển đổi thành các loại Python. Giá trị mặc định là sai. Bạn có thể đặt nó thành True nếu bạn muốn có kết quả truy vấn trong loại MySQL.
  • import mysql.connector
    from mysql.connector import Error
    
    try:
        connection = mysql.connector.connect(host='localhost',
                                             database='Electronics',
                                             user='pynative',
                                             password='pynative@#29')
        if connection.is_connected():
            db_Info = connection.get_server_info()
            print("Connected to MySQL Server version ", db_Info)
            cursor = connection.cursor()
            cursor.execute("select database();")
            record = cursor.fetchone()
            print("You're connected to database: ", record)
    
    except Error as e:
        print("Error while connecting to MySQL", e)
    finally:
        if connection.is_connected():
            cursor.close()
            connection.close()
            print("MySQL connection is closed")
    
    2: Khi cài đặt thành TRUE, sử dụng IPv6 khi địa chỉ giải quyết được cả IPv4 và IPv6. Theo mặc định, IPv4 được sử dụng trong các trường hợp như vậy. Giá trị mặc định cho đối số này là sai.
  • import mysql.connector
    from mysql.connector import Error
    
    try:
        connection = mysql.connector.connect(host='localhost',
                                             database='Electronics',
                                             user='pynative',
                                             password='pynative@#29')
        if connection.is_connected():
            db_Info = connection.get_server_info()
            print("Connected to MySQL Server version ", db_Info)
            cursor = connection.cursor()
            cursor.execute("select database();")
            record = cursor.fetchone()
            print("You're connected to database: ", record)
    
    except Error as e:
        print("Error while connecting to MySQL", e)
    finally:
        if connection.is_connected():
            cursor.close()
            connection.close()
            print("MySQL connection is closed")
    
    3: Đó là tên nhóm kết nối mà bạn đang tạo hoặc sử dụng.
  • import mysql.connector
    from mysql.connector import Error
    
    try:
        connection = mysql.connector.connect(host='localhost',
                                             database='Electronics',
                                             user='pynative',
                                             password='pynative@#29')
        if connection.is_connected():
            db_Info = connection.get_server_info()
            print("Connected to MySQL Server version ", db_Info)
            cursor = connection.cursor()
            cursor.execute("select database();")
            record = cursor.fetchone()
            print("You're connected to database: ", record)
    
    except Error as e:
        print("Error while connecting to MySQL", e)
    finally:
        if connection.is_connected():
            cursor.close()
            connection.close()
            print("MySQL connection is closed")
    
    4: Kích thước nhóm kết nối mà bạn muốn tạo. Giá trị mặc định là 5.
  • import mysql.connector
    from mysql.connector import Error
    
    try:
        connection = mysql.connector.connect(host='localhost',
                                             database='Electronics',
                                             user='pynative',
                                             password='pynative@#29')
        if connection.is_connected():
            db_Info = connection.get_server_info()
            print("Connected to MySQL Server version ", db_Info)
            cursor = connection.cursor()
            cursor.execute("select database();")
            record = cursor.fetchone()
            print("You're connected to database: ", record)
    
    except Error as e:
        print("Error while connecting to MySQL", e)
    finally:
        if connection.is_connected():
            cursor.close()
            connection.close()
            print("MySQL connection is closed")
    
    5: Đặt lại các biến phiên khi kết nối được trả về nhóm. Mặc định là đúng.
  • import mysql.connector
    from mysql.connector import Error
    
    try:
        connection = mysql.connector.connect(host='localhost',
                                             database='Electronics',
                                             user='pynative',
                                             password='pynative@#29')
        if connection.is_connected():
            db_Info = connection.get_server_info()
            print("Connected to MySQL Server version ", db_Info)
            cursor = connection.cursor()
            cursor.execute("select database();")
            record = cursor.fetchone()
            print("You're connected to database: ", record)
    
    except Error as e:
        print("Error while connecting to MySQL", e)
    finally:
        if connection.is_connected():
            cursor.close()
            connection.close()
            print("MySQL connection is closed")
    
    6: Chỉ định có nên sử dụng phần mở rộng Python hay C thuần túy hay không. Nếu use_pure = false, thì một mô -đun Python thuần túy được sử dụng; Nếu không, nó kết nối MySQL bằng tiện ích mở rộng C. Hơn nữa, nếu phần mở rộng C không có sẵn, trình kết nối MySQL sẽ tự động trở lại với việc triển khai Python thuần túy.
  • import mysql.connector
    from mysql.connector import Error
    
    try:
        connection = mysql.connector.connect(host='localhost',
                                             database='Electronics',
                                             user='pynative',
                                             password='pynative@#29')
        if connection.is_connected():
            db_Info = connection.get_server_info()
            print("Connected to MySQL Server version ", db_Info)
            cursor = connection.cursor()
            cursor.execute("select database();")
            record = cursor.fetchone()
            print("You're connected to database: ", record)
    
    except Error as e:
        print("Error while connecting to MySQL", e)
    finally:
        if connection.is_connected():
            cursor.close()
            connection.close()
            print("MySQL connection is closed")
    
    7: Vị trí của tệp ổ cắm UNIX. Chúng cho phép giao tiếp giữa hai quá trình.
  • import mysql.connector
    from mysql.connector import Error
    
    try:
        connection = mysql.connector.connect(host='localhost',
                                             database='Electronics',
                                             user='pynative',
                                             password='pynative@#29')
        if connection.is_connected():
            db_Info = connection.get_server_info()
            print("Connected to MySQL Server version ", db_Info)
            cursor = connection.cursor()
            cursor.execute("select database();")
            record = cursor.fetchone()
            print("You're connected to database: ", record)
    
    except Error as e:
        print("Error while connecting to MySQL", e)
    finally:
        if connection.is_connected():
            cursor.close()
            connection.close()
            print("MySQL connection is closed")
    
    8: plugin xác thực để sử dụng, được thêm vào 1.2.1.
  • import mysql.connector
    from mysql.connector import Error
    
    try:
        connection = mysql.connector.connect(host='localhost',
                                             database='Electronics',
                                             user='pynative',
                                             password='pynative@#29')
        if connection.is_connected():
            db_Info = connection.get_server_info()
            print("Connected to MySQL Server version ", db_Info)
            cursor = connection.cursor()
            cursor.execute("select database();")
            record = cursor.fetchone()
            print("You're connected to database: ", record)
    
    except Error as e:
        print("Error while connecting to MySQL", e)
    finally:
        if connection.is_connected():
            cursor.close()
            connection.close()
            print("MySQL connection is closed")
    
    9: đối chiếu MySQL để sử dụng. Bạn có thể sử dụng đối chiếu mà bạn đặt trong khi cài đặt Máy chủ MySQL. Giá trị mặc định là utf8_generalw_chiich.
  • Connected to MySQL Server version  5.7.19
    You're connected to database:  ('electronics',)
    MySQL connection is closed
    0: Đặt biến phiên SQL_MODE tại thời điểm kết nối.

Sử dụng từ điển để giữ các đối số kết nối MySQL

Hơn nữa, hãy xem cách sử dụng từ điển để lưu trữ tất cả các đối số kết nối này.

Nếu bạn có nhiều đối số kết nối, thì tốt nhất là giữ chúng trong từ điển và sử dụng toán tử

Connected to MySQL Server version  5.7.19
You're connected to database:  ('electronics',)
MySQL connection is closed
1. & NBSP; Ví dụ: bạn biết rằng bạn yêu cầu tối thiểu bốn đối số & nbsp; (tức là tên người dùng, mật khẩu, tên máy chủ, tên cơ sở dữ liệu) để kết nối MySQL.

Nếu bạn có nhiều đối số kết nối, thì tốt nhất là giữ chúng trong một từ điển và sử dụng toán tử **. Trong các trường hợp đặc biệt, chúng ta cần nhiều hơn bốn đối số trong phương thức kết nối để kết nối cơ sở dữ liệu MySQL. Hãy để hiểu điều này. Ví dụ: dưới đây là ba đối số kết nối khác mà chúng ta có thể sử dụng trong phương thức

...
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
    2015-10-20 14:09:06 Reconnected to MYSQL.
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
    2015-10-20 14:09:06 Reconnected to MYSQL.
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
    2015-10-20 14:09:06 Reconnected to MYSQL.
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
...
9.three more connection arguments we can use in the
...
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
    2015-10-20 14:09:06 Reconnected to MYSQL.
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
    2015-10-20 14:09:06 Reconnected to MYSQL.
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
    2015-10-20 14:09:06 Reconnected to MYSQL.
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
...
9 method.

  1. Connected to MySQL Server version  5.7.19
    You're connected to database:  ('electronics',)
    MySQL connection is closed
    3 - Thời gian chờ cho các kết nối ổ cắm TCP và UNIXTimeout for the TCP and Unix socket connections
  2. Connected to MySQL Server version  5.7.19
    You're connected to database:  ('electronics',)
    MySQL connection is closed
    4-Có phải giao dịch cam kết tự động hay không. Mặc định này sai
  3. ________ 44 & nbsp; - Kích thước nhóm kết nối nếu bạn muốn sử dụng gộp kết nối.

Bạn có thể sử dụng nhiều đối số kết nối khác theo nhu cầu của bạn, thêm tất cả chúng vào từ điển và truyền từ điển để kết nối () phương thức. Hãy để chứng minh nó trong ví dụ dưới đây.

import mysql.connector
from mysql.connector import Error

try:
    connection_config_dict = {
        'user': 'pynative',
        'password': 'pynative@123',
        'host': '127.0.0.1',
        'database': 'Electronics',
        'raise_on_warnings': True,
        'use_pure': False,
        'autocommit': True,
        'pool_size': 5
    }
    connection = mysql.connector.connect(**connection_config_dict)

    if connection.is_connected():
        db_Info = connection.get_server_info()
        print("Connected to MySQL Server version ", db_Info)
        cursor = connection.cursor()
        cursor.execute("select database();")
        record = cursor.fetchone()
        print("Your connected to database: ", record)

except Error as e:
    print("Error while connecting to MySQL", e)
finally:
    if connection.is_connected():
        cursor.close()
        connection.close()
        print("MySQL connection is closed")

Thay đổi thời gian chờ kết nối MySQL từ Python

Đôi khi chúng ta cần thay đổi giá trị thời gian chờ kết nối nếu chúng ta đọc hoặc chèn dữ liệu mở rộng vào máy chủ MySQL. Kết nối chấm dứt nếu yêu cầu mất nhiều thời gian hơn giá trị này.

Sử dụng đối số

Connected to MySQL Server version  5.7.19
You're connected to database:  ('electronics',)
MySQL connection is closed
3 của Trình kết nối MySQL Python để quản lý các vấn đề về thời gian chờ bằng cách tăng giá trị thời gian chờ.

Connected to MySQL Server version  5.7.19
You're connected to database:  ('electronics',)
MySQL connection is closed
3 là giá trị thời gian chờ thứ hai cho các kết nối ổ cắm TCP và UNIX. Lần này biểu thị số giây mà máy chủ MySQL chờ đợi để đáp ứng yêu cầu hiện tại.

Bạn cũng có thể đặt các tham số sau của máy chủ MySQL bằng cách thực hiện truy vấn SQL từ Python để xử lý sự cố thời gian chờ kết nối. Chỉ thay đổi các tham số sau chỉ giá trị khi đối số

Connected to MySQL Server version  5.7.19
You're connected to database:  ('electronics',)
MySQL connection is closed
3 có thể kiểm soát vấn đề thời gian chờ.

  • Connected to MySQL Server version  5.7.19
    You're connected to database:  ('electronics',)
    MySQL connection is closed
    9: Số giây mà máy chủ nên chờ hoạt động trên kết nối tương tác trước khi đóng nó.
  • import mysql.connector
    
    try:
        connection = mysql.connector.connect(host='localhost',
                                             database='Electronics',
                                             user='pynative',
                                             password='pynative@#29')
    
        mySql_Create_Table_Query = """CREATE TABLE Laptop ( 
                                 Id int(11) NOT NULL,
                                 Name varchar(250) NOT NULL,
                                 Price float NOT NULL,
                                 Purchase_date Date NOT NULL,
                                 PRIMARY KEY (Id)) """
    
        cursor = connection.cursor()
        result = cursor.execute(mySql_Create_Table_Query)
        print("Laptop Table created successfully ")
    
    except mysql.connector.Error as error:
        print("Failed to create table in MySQL: {}".format(error))
    finally:
        if connection.is_connected():
            cursor.close()
            connection.close()
            print("MySQL connection is closed")
    
    0 - Số giây máy chủ nên chờ & nbsp; cho hoạt động trên kết nối trước khi đóng nó.

Thí dụ

import mysql.connector
from mysql.connector import Error

try:
    connection = mysql.connector.connect(host='localhost',
                                         database='Electronics',
                                         user='pynative',
                                         password='pynative@#29', connection_timeout=180)

    if connection.is_connected():
        db_Info = connection.get_server_info()
        print("Connected to MySQL database... MySQL Server version on ", db_Info)

        cursor = connection.cursor()
        # global connection timeout arguments
        global_connect_timeout = 'SET GLOBAL connect_timeout=180'
        global_wait_timeout = 'SET GLOBAL connect_timeout=180'
        global_interactive_timeout = 'SET GLOBAL connect_timeout=180'

        cursor.execute(global_connect_timeout)
        cursor.execute(global_wait_timeout)
        cursor.execute(global_interactive_timeout)

        connection.commit()

except Error as e:
    print("Error while connecting to MySQL", e)
finally:
    # closing database connection.
    if connection.is_connected():
        cursor.close()
        connection.close()
        print("MySQL connection is closed")

Như bạn có thể thấy, tôi đã đặt tất cả các giá trị thời gian chờ kết nối thành 180 giây, tức là, 3 phút trong chương trình trên.

Kết nối với MySQL bằng cách sử dụng tiện ích mở rộng Python C đầu nối

Mô -đun kết nối Python có giao diện mở rộng C để kết nối cơ sở dữ liệu MySQL. Đối số kết nối

import mysql.connector
from mysql.connector import Error

try:
    connection = mysql.connector.connect(host='localhost',
                                         database='Electronics',
                                         user='pynative',
                                         password='pynative@#29')
    if connection.is_connected():
        db_Info = connection.get_server_info()
        print("Connected to MySQL Server version ", db_Info)
        cursor = connection.cursor()
        cursor.execute("select database();")
        record = cursor.fetchone()
        print("You're connected to database: ", record)

except Error as e:
    print("Error while connecting to MySQL", e)
finally:
    if connection.is_connected():
        cursor.close()
        connection.close()
        print("MySQL connection is closed")
6 xác định xem có nên kết nối với MySQL bằng giao diện Python thuần túy hoặc tiện ích mở rộng C hay không.

Giá trị mặc định của

import mysql.connector
from mysql.connector import Error

try:
    connection = mysql.connector.connect(host='localhost',
                                         database='Electronics',
                                         user='pynative',
                                         password='pynative@#29')
    if connection.is_connected():
        db_Info = connection.get_server_info()
        print("Connected to MySQL Server version ", db_Info)
        cursor = connection.cursor()
        cursor.execute("select database();")
        record = cursor.fetchone()
        print("You're connected to database: ", record)

except Error as e:
    print("Error while connecting to MySQL", e)
finally:
    if connection.is_connected():
        cursor.close()
        connection.close()
        print("MySQL connection is closed")
6 là
import mysql.connector

try:
    connection = mysql.connector.connect(host='localhost',
                                         database='Electronics',
                                         user='pynative',
                                         password='pynative@#29')

    mySql_Create_Table_Query = """CREATE TABLE Laptop ( 
                             Id int(11) NOT NULL,
                             Name varchar(250) NOT NULL,
                             Price float NOT NULL,
                             Purchase_date Date NOT NULL,
                             PRIMARY KEY (Id)) """

    cursor = connection.cursor()
    result = cursor.execute(mySql_Create_Table_Query)
    print("Laptop Table created successfully ")

except mysql.connector.Error as error:
    print("Failed to create table in MySQL: {}".format(error))
finally:
    if connection.is_connected():
        cursor.close()
        connection.close()
        print("MySQL connection is closed")
3 có nghĩa là nó sử dụng việc triển khai Python thuần túy để kết nối mà chúng ta đã thảo luận. Ví dụ dưới đây trình bày cách kết nối bằng tiện ích mở rộng C.

...
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
    2015-10-20 14:09:06 Reconnected to MYSQL.
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
    2015-10-20 14:09:06 Reconnected to MYSQL.
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
    2015-10-20 14:09:06 Reconnected to MYSQL.
    2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
    2015-10-20 14:09:06 sqlstate:00000
    2015-10-20 14:09:06 open?1
...
0

Bước tiếp theo:

Để thực hành những gì bạn đã học trong bài viết này, vui lòng giải quyết một dự án tập thể dục cơ sở dữ liệu Python để thực hành và thành thạo các hoạt động của cơ sở dữ liệu Python.

Tôi đã tạo ra một bài tập hệ thống thông tin bệnh viện bằng Python và MySQL. Bài tập này có sáu câu hỏi. Tôi đã cung cấp các bảng cần thiết để bạn có thể tiến hành trực tiếp để giải quyết các vấn đề của bài tập này.

Đó là nó, folks. Hãy cho tôi biết ý kiến ​​của bạn trong phần dưới đây.