Phương pháp nào được sử dụng để kết nối với cơ sở dữ liệu trong python?

Từ một công ty xây dựng đến một sàn giao dịch chứng khoán, mọi tổ chức đều phụ thuộc vào cơ sở dữ liệu lớn. Về cơ bản, đây là tập hợp các bảng và được kết nối với nhau thông qua các cột. Các hệ thống cơ sở dữ liệu này hỗ trợ SQL, Ngôn ngữ truy vấn có cấu trúc, được sử dụng để tạo, truy cập và thao tác dữ liệu. SQL được sử dụng để truy cập dữ liệu, đồng thời tạo và khai thác các mối quan hệ giữa dữ liệu được lưu trữ. Ngoài ra, các cơ sở dữ liệu này hỗ trợ các quy tắc chuẩn hóa cơ sở dữ liệu để tránh dư thừa dữ liệu. Ngôn ngữ lập trình Python có các tính năng mạnh mẽ để lập trình cơ sở dữ liệu. Python hỗ trợ nhiều cơ sở dữ liệu khác nhau như MySQL, Oracle, Sybase, PostgreSQL, v.v. Python cũng hỗ trợ Ngôn ngữ Định nghĩa Dữ liệu [DDL], Ngôn ngữ Thao tác Dữ liệu [DML] và Câu lệnh Truy vấn Dữ liệu. Đối với lập trình cơ sở dữ liệu, API Python DB là một mô-đun được sử dụng rộng rãi cung cấp giao diện lập trình ứng dụng cơ sở dữ liệu

Lợi ích của Python đối với lập trình cơ sở dữ liệu

Có nhiều lý do chính đáng để sử dụng Python để lập trình các ứng dụng cơ sở dữ liệu

  • Lập trình bằng Python được cho là hiệu quả hơn và nhanh hơn so với các ngôn ngữ khác
  • Python nổi tiếng vì tính di động của nó
  • Đó là nền tảng độc lập
  • Python hỗ trợ con trỏ SQL
  • Trong nhiều ngôn ngữ lập trình, nhà phát triển ứng dụng cần quan tâm đến các kết nối mở và đóng của cơ sở dữ liệu, để tránh các ngoại lệ và lỗi khác. Trong Python, các kết nối này được xử lý
  • Python hỗ trợ các hệ thống cơ sở dữ liệu quan hệ
  • API cơ sở dữ liệu Python tương thích với nhiều cơ sở dữ liệu khác nhau, vì vậy rất dễ dàng di chuyển và chuyển các giao diện ứng dụng cơ sở dữ liệu

DB-API [SQL-API] cho Python

Python DB-API độc lập với bất kỳ công cụ cơ sở dữ liệu nào, cho phép bạn viết các tập lệnh Python để truy cập bất kỳ công cụ cơ sở dữ liệu nào. Triển khai API Python DB cho MySQL là MySQLdb. Đối với PostgreSQL, nó hỗ trợ các mô-đun psycopg, PyGresQL và pyPgSQL. Các triển khai DB-API cho Oracle là dc_oracle2 và cx_oracle. Pydb2 là triển khai DB-API cho DB2. DB-API của Python bao gồm các đối tượng kết nối, đối tượng con trỏ, ngoại lệ tiêu chuẩn và một số nội dung mô-đun khác, tất cả chúng ta sẽ thảo luận

đối tượng kết nối

Các đối tượng kết nối tạo kết nối với cơ sở dữ liệu và chúng được sử dụng thêm cho các giao dịch khác nhau. Các đối tượng kết nối này cũng được sử dụng làm đại diện cho phiên cơ sở dữ liệu

Một kết nối được tạo như sau

>>>conn = MySQLdb.connect['library', user='suhas', password='python']

Bạn có thể sử dụng một đối tượng kết nối để gọi các phương thức như commit[], rollback[] và close[] như hình bên dưới

>>>cur = conn.cursor[]  //creates new cursor object for executing SQL statements
>>>conn.commit[]  //Commits the transactions
>>>conn.rollback[]  //Roll back the transactions
>>>conn.close[]  //closes the connection
>>>conn.callproc[proc,param]  //call stored procedure for execution
>>>conn.getsource[proc]  //fetches stored procedure code

đối tượng con trỏ

Con trỏ là một trong những tính năng mạnh mẽ của SQL. Đây là những đối tượng chịu trách nhiệm gửi các câu lệnh SQL khác nhau tới máy chủ cơ sở dữ liệu. Có một số lớp con trỏ trong

>>>cur = conn.cursor[]  //creates new cursor object for executing SQL statements
>>>conn.commit[]  //Commits the transactions
>>>conn.rollback[]  //Roll back the transactions
>>>conn.close[]  //closes the connection
>>>conn.callproc[proc,param]  //call stored procedure for execution
>>>conn.getsource[proc]  //fetches stored procedure code
7

  1. >>>cur = conn.cursor[]  //creates new cursor object for executing SQL statements
    >>>conn.commit[]  //Commits the transactions
    >>>conn.rollback[]  //Roll back the transactions
    >>>conn.close[]  //closes the connection
    >>>conn.callproc[proc,param]  //call stored procedure for execution
    >>>conn.getsource[proc]  //fetches stored procedure code
    8 là lớp cơ sở cho đối tượng Con trỏ
  2. >>>cur = conn.cursor[]  //creates new cursor object for executing SQL statements
    >>>conn.commit[]  //Commits the transactions
    >>>conn.rollback[]  //Roll back the transactions
    >>>conn.close[]  //closes the connection
    >>>conn.callproc[proc,param]  //call stored procedure for execution
    >>>conn.getsource[proc]  //fetches stored procedure code
    9 là lớp con trỏ mặc định.
    >>>cursor.execute['SELECT * FROM books']
    >>>cursor.execute['''SELECT * FROM books WHERE book_name = 'python' AND  book_author = 'Mark Lutz' ]
    >>>cursor.close[]
    0,
    >>>cursor.execute['SELECT * FROM books']
    >>>cursor.execute['''SELECT * FROM books WHERE book_name = 'python' AND  book_author = 'Mark Lutz' ]
    >>>cursor.close[]
    1,
    >>>cursor.execute['SELECT * FROM books']
    >>>cursor.execute['''SELECT * FROM books WHERE book_name = 'python' AND  book_author = 'Mark Lutz' ]
    >>>cursor.close[]
    2, và
    >>>cur = conn.cursor[]  //creates new cursor object for executing SQL statements
    >>>conn.commit[]  //Commits the transactions
    >>>conn.rollback[]  //Roll back the transactions
    >>>conn.close[]  //closes the connection
    >>>conn.callproc[proc,param]  //call stored procedure for execution
    >>>conn.getsource[proc]  //fetches stored procedure code
    8 là một số thành phần của lớp
    >>>cursor.execute['SELECT * FROM books']
    >>>cursor.execute['''SELECT * FROM books WHERE book_name = 'python' AND  book_author = 'Mark Lutz' ]
    >>>cursor.close[]
    4
  3. >>>cursor.execute['SELECT * FROM books']
    >>>cursor.execute['''SELECT * FROM books WHERE book_name = 'python' AND  book_author = 'Mark Lutz' ]
    >>>cursor.close[]
    1 sử dụng hàm
    >>>cursor.execute['SELECT * FROM books']
    >>>cursor.execute['''SELECT * FROM books WHERE book_name = 'python' AND  book_author = 'Mark Lutz' ]
    >>>cursor.close[]
    6 để truy xuất các tập kết quả từ truy vấn đã thực hiện. Các bộ kết quả này được lưu trữ ở phía máy khách
  4. >>>cursor.execute['SELECT * FROM books']
    >>>cursor.execute['''SELECT * FROM books WHERE book_name = 'python' AND  book_author = 'Mark Lutz' ]
    >>>cursor.close[]
    7 sử dụng hàm
    >>>cursor.execute['SELECT * FROM books']
    >>>cursor.execute['''SELECT * FROM books WHERE book_name = 'python' AND  book_author = 'Mark Lutz' ]
    >>>cursor.close[]
    8 để truy xuất các tập kết quả từ truy vấn đã thực hiện. Các bộ kết quả này được lưu trữ ở phía máy chủ

Ví dụ sau minh họa việc thực thi các lệnh SQL sử dụng các đối tượng con trỏ. Bạn có thể sử dụng thực thi để thực thi các lệnh SQL như

>>>cursor.execute['SELECT * FROM books']
>>>cursor.execute['''SELECT * FROM books WHERE book_name = 'python' AND  book_author = 'Mark Lutz' ]
>>>cursor.close[]
9. Để thực hiện tất cả các thao tác SQL, bạn cần đóng con trỏ là
>>> cursor.execute['insert books values [%s,%s,%s,%s]',['Py9098','Programming With Perl',120,100]]
Traceback [most recent call last]:
File "", line 1, in ?
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 95, in execute
return self._execute[query, args]
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 114, in _execute
self.errorhandler[self, exc, value]
raise errorclass, errorvalue
_mysql_exceptions.IntegrityError: [1062, "Duplicate entry 'Py9098' for key 1"]
0

>>>cursor.execute['SELECT * FROM books']
>>>cursor.execute['''SELECT * FROM books WHERE book_name = 'python' AND  book_author = 'Mark Lutz' ]
>>>cursor.close[]

Xử lý lỗi và ngoại lệ trong DB-API

Xử lý ngoại lệ rất dễ dàng trong mô-đun DB-API Python. Chúng ta có thể đặt các cảnh báo và thông báo xử lý lỗi trong các chương trình. Python DB-API có nhiều tùy chọn khác nhau để xử lý việc này, như

>>> cursor.execute['insert books values [%s,%s,%s,%s]',['Py9098','Programming With Perl',120,100]]
Traceback [most recent call last]:
File "", line 1, in ?
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 95, in execute
return self._execute[query, args]
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 114, in _execute
self.errorhandler[self, exc, value]
raise errorclass, errorvalue
_mysql_exceptions.IntegrityError: [1062, "Duplicate entry 'Py9098' for key 1"]
1,
>>> cursor.execute['insert books values [%s,%s,%s,%s]',['Py9098','Programming With Perl',120,100]]
Traceback [most recent call last]:
File "", line 1, in ?
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 95, in execute
return self._execute[query, args]
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 114, in _execute
self.errorhandler[self, exc, value]
raise errorclass, errorvalue
_mysql_exceptions.IntegrityError: [1062, "Duplicate entry 'Py9098' for key 1"]
2,
>>> cursor.execute['insert books values [%s,%s,%s,%s]',['Py9098','Programming With Perl',120,100]]
Traceback [most recent call last]:
File "", line 1, in ?
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 95, in execute
return self._execute[query, args]
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 114, in _execute
self.errorhandler[self, exc, value]
raise errorclass, errorvalue
_mysql_exceptions.IntegrityError: [1062, "Duplicate entry 'Py9098' for key 1"]
3,
>>> cursor.execute['insert books values [%s,%s,%s,%s]',['Py9098','Programming With Perl',120,100]]
Traceback [most recent call last]:
File "", line 1, in ?
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 95, in execute
return self._execute[query, args]
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 114, in _execute
self.errorhandler[self, exc, value]
raise errorclass, errorvalue
_mysql_exceptions.IntegrityError: [1062, "Duplicate entry 'Py9098' for key 1"]
4,
>>> cursor.execute['insert books values [%s,%s,%s,%s]',['Py9098','Programming With Perl',120,100]]
Traceback [most recent call last]:
File "", line 1, in ?
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 95, in execute
return self._execute[query, args]
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 114, in _execute
self.errorhandler[self, exc, value]
raise errorclass, errorvalue
_mysql_exceptions.IntegrityError: [1062, "Duplicate entry 'Py9098' for key 1"]
5,
>>> cursor.execute['insert books values [%s,%s,%s,%s]',['Py9098','Programming With Perl',120,100]]
Traceback [most recent call last]:
File "", line 1, in ?
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 95, in execute
return self._execute[query, args]
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 114, in _execute
self.errorhandler[self, exc, value]
raise errorclass, errorvalue
_mysql_exceptions.IntegrityError: [1062, "Duplicate entry 'Py9098' for key 1"]
6,
>>> cursor.execute['insert books values [%s,%s,%s,%s]',['Py9098','Programming With Perl',120,100]]
Traceback [most recent call last]:
File "", line 1, in ?
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 95, in execute
return self._execute[query, args]
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 114, in _execute
self.errorhandler[self, exc, value]
raise errorclass, errorvalue
_mysql_exceptions.IntegrityError: [1062, "Duplicate entry 'Py9098' for key 1"]
7 và
>>> cursor.execute['insert books values [%s,%s,%s,%s]',['Py9098','Programming With Perl',120,100]]
Traceback [most recent call last]:
File "", line 1, in ?
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 95, in execute
return self._execute[query, args]
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 114, in _execute
self.errorhandler[self, exc, value]
raise errorclass, errorvalue
_mysql_exceptions.IntegrityError: [1062, "Duplicate entry 'Py9098' for key 1"]
8
Hãy xem xét từng cái một

  1. Tính toàn vẹnLỗi. Hãy xem chi tiết lỗi toàn vẹn. Trong ví dụ sau, chúng tôi sẽ cố gắng nhập các bản ghi trùng lặp vào cơ sở dữ liệu. Nó sẽ hiển thị lỗi toàn vẹn,
    >>> cursor.execute['insert books values [%s,%s,%s,%s]',['Py9098','Programming With Perl',120,100]]
    Traceback [most recent call last]:
    File "", line 1, in ?
    File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 95, in execute
    return self._execute[query, args]
    File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 114, in _execute
    self.errorhandler[self, exc, value]
    raise errorclass, errorvalue
    _mysql_exceptions.IntegrityError: [1062, "Duplicate entry 'Py9098' for key 1"]
    9, như hình bên dưới.
    >>> cursor.execute['insert books values [%s,%s,%s,%s]',['Py9098','Programming With Perl',120,100]]
    Traceback [most recent call last]:
    File "", line 1, in ?
    File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 95, in execute
    return self._execute[query, args]
    File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 114, in _execute
    self.errorhandler[self, exc, value]
    raise errorclass, errorvalue
    _mysql_exceptions.IntegrityError: [1062, "Duplicate entry 'Py9098' for key 1"]
  2. lỗi hoạt động. Nếu có bất kỳ lỗi hoạt động nào như không có cơ sở dữ liệu nào được chọn, Python DB-API sẽ xử lý lỗi này như
    >>> cursor.execute['insert books values [%s,%s,%s,%s]',['Py9098','Programming With Perl',120,100]]
    Traceback [most recent call last]:
    File "", line 1, in ?
    File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 95, in execute
    return self._execute[query, args]
    File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 114, in _execute
    self.errorhandler[self, exc, value]
    raise errorclass, errorvalue
    _mysql_exceptions.IntegrityError: [1062, "Duplicate entry 'Py9098' for key 1"]
    7, hiển thị bên dưới.
    >>> cursor.execute['Create database Library']
    >>> q='select name from books where cost>=%s order by name'
    >>>cursor.execute[q,[50]]
    Traceback [most recent call last]:
    File "", line 1, in ?
    File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 95, in execute
    return self._execute[query, args]
    File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 114, in _execute
    self.errorhandler[self, exc, value]
    File "/usr/lib/python2.3/site-packages/MySQLdb/connections.py", line 33, in defaulterrorhandler
    raise errorclass, errorvalue
    _mysql_exceptions.OperationalError: [1046, 'No Database Selected']
  3. Lỗi lập trình. Nếu có bất kỳ lỗi lập trình nào như tạo cơ sở dữ liệu trùng lặp, Python DB-API sẽ xử lý lỗi này như
    >>> cursor.execute['insert books values [%s,%s,%s,%s]',['Py9098','Programming With Perl',120,100]]
    Traceback [most recent call last]:
    File "", line 1, in ?
    File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 95, in execute
    return self._execute[query, args]
    File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 114, in _execute
    self.errorhandler[self, exc, value]
    raise errorclass, errorvalue
    _mysql_exceptions.IntegrityError: [1062, "Duplicate entry 'Py9098' for key 1"]
    8, hiển thị bên dưới.
    >>> cursor.execute['Create database Library']
    Traceback [most recent call last]:>>> cursor.execute['Create database Library']
    Traceback [most recent call last]:
    File "", line 1, in ?
    File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 95, in execute
    return self._execute[query, args]
    File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 114, in _execute
    self.errorhandler[self, exc, value]
    File "/usr/lib/python2.3/site-packages/MySQLdb/connections.py", line 33, in defaulterrorhandler
    raise errorclass, errorvalue
    _mysql_exceptions.ProgrammingError: [1007, "Can't create database 'Library'. Database exists"]

Python và MySQL

Python và MySQL là sự kết hợp tốt để phát triển các ứng dụng cơ sở dữ liệu. Sau khi bắt đầu dịch vụ MySQL trên Linux, bạn cần có MySQLdb, API DB-API của Python cho MySQL để thực hiện các thao tác cơ sở dữ liệu. Bạn có thể kiểm tra xem mô-đun MySQLdb đã được cài đặt trong hệ thống của mình chưa bằng lệnh sau

>>>import MySQLdb

Nếu lệnh này chạy thành công, bây giờ bạn có thể bắt đầu viết tập lệnh cho cơ sở dữ liệu của mình

Để viết các ứng dụng cơ sở dữ liệu bằng Python, có năm bước cần làm theo

  1. Nhập giao diện SQL bằng lệnh sau.
    >>> import MySQLdb
  2. Thiết lập kết nối với cơ sở dữ liệu bằng lệnh sau.
    >>> conn=MySQLdb.connect[host='localhost',user='root',passwd='']

    …trong đó

    >>> cursor.execute['Create database Library']
    >>> q='select name from books where cost>=%s order by name'
    >>>cursor.execute[q,[50]]
    Traceback [most recent call last]:
    File "", line 1, in ?
    File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 95, in execute
    return self._execute[query, args]
    File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 114, in _execute
    self.errorhandler[self, exc, value]
    File "/usr/lib/python2.3/site-packages/MySQLdb/connections.py", line 33, in defaulterrorhandler
    raise errorclass, errorvalue
    _mysql_exceptions.OperationalError: [1046, 'No Database Selected']
    2 là tên máy chủ của bạn, theo sau là tên người dùng và mật khẩu. Trong trường hợp root, không cần cung cấp mật khẩu

  3. Tạo một con trỏ cho kết nối bằng lệnh sau.
    >>>cursor = conn.cursor[]
  4. Thực hiện bất kỳ truy vấn SQL nào bằng cách sử dụng con trỏ này như được hiển thị bên dưới—ở đây, kết quả đầu ra dưới dạng 1L hoặc 2L hiển thị một số hàng bị ảnh hưởng bởi truy vấn này.
    >>>cur = conn.cursor[]  //creates new cursor object for executing SQL statements
    >>>conn.commit[]  //Commits the transactions
    >>>conn.rollback[]  //Roll back the transactions
    >>>conn.close[]  //closes the connection
    >>>conn.callproc[proc,param]  //call stored procedure for execution
    >>>conn.getsource[proc]  //fetches stored procedure code
    0
  5. Cuối cùng, tìm nạp tập kết quả và lặp lại tập kết quả này. Ở bước này, người dùng có thể tìm nạp các tập kết quả như hình bên dưới.
    >>>cur = conn.cursor[]  //creates new cursor object for executing SQL statements
    >>>conn.commit[]  //Commits the transactions
    >>>conn.rollback[]  //Roll back the transactions
    >>>conn.close[]  //closes the connection
    >>>conn.callproc[proc,param]  //call stored procedure for execution
    >>>conn.getsource[proc]  //fetches stored procedure code
    1

    Trong ví dụ này, hàm fetchall[] được sử dụng để tìm nạp các tập kết quả

Các thao tác SQL khác

Chúng tôi có thể thực hiện tất cả các hoạt động SQL với Python DB-API. Các truy vấn chèn, xóa, tổng hợp và cập nhật có thể được minh họa như sau

  1. Chèn truy vấn SQL_______1_______2

    Nếu người dùng muốn chèn các mục nhập trùng lặp cho số truy cập của sách, thì DB-API Python sẽ hiển thị lỗi vì đây là khóa chính. Ví dụ sau đây minh họa điều này

    >>>cur = conn.cursor[]  //creates new cursor object for executing SQL statements
    >>>conn.commit[]  //Commits the transactions
    >>>conn.rollback[]  //Roll back the transactions
    >>>conn.close[]  //closes the connection
    >>>conn.callproc[proc,param]  //call stored procedure for execution
    >>>conn.getsource[proc]  //fetches stored procedure code
    3
  2. Truy vấn Cập nhật SQL có thể được sử dụng để cập nhật các bản ghi hiện có trong cơ sở dữ liệu như hình bên dưới.
    >>>cur = conn.cursor[]  //creates new cursor object for executing SQL statements
    >>>conn.commit[]  //Commits the transactions
    >>>conn.rollback[]  //Roll back the transactions
    >>>conn.close[]  //closes the connection
    >>>conn.callproc[proc,param]  //call stored procedure for execution
    >>>conn.getsource[proc]  //fetches stored procedure code
    4
  3. Truy vấn Delete SQL có thể được sử dụng để xóa các bản ghi hiện có trong cơ sở dữ liệu như hình bên dưới.
    >>>cur = conn.cursor[]  //creates new cursor object for executing SQL statements
    >>>conn.commit[]  //Commits the transactions
    >>>conn.rollback[]  //Roll back the transactions
    >>>conn.close[]  //closes the connection
    >>>conn.callproc[proc,param]  //call stored procedure for execution
    >>>conn.getsource[proc]  //fetches stored procedure code
    5
  4. Các hàm tổng hợp có thể được sử dụng với Python DB-API trong cơ sở dữ liệu như hình bên dưới.
    >>>cur = conn.cursor[]  //creates new cursor object for executing SQL statements
    >>>conn.commit[]  //Commits the transactions
    >>>conn.rollback[]  //Roll back the transactions
    >>>conn.close[]  //closes the connection
    >>>conn.callproc[proc,param]  //call stored procedure for execution
    >>>conn.getsource[proc]  //fetches stored procedure code
    6

Tôi hy vọng bây giờ bạn sẽ tận hưởng sức mạnh của Python để lập trình cơ sở dữ liệu

Người giới thiệu

  • Trang web Python
  • Lập trình Python của Mark Lutz, O'Reilly Publications
  • Truy cập cơ sở dữ liệu bằng Python DBAPI-2. 0 của Federico Di Gregorio

Bài viết này được xuất bản lần đầu vào ngày 1 tháng 5 năm 2009 và được cập nhật gần đây vào ngày 10 tháng 4 năm 2019

  • THẺ
  • cơ sở dữ liệu
  • lập trình cơ sở dữ liệu
  • mysql
  • con trăn
  • SQL

Bài viết trước Sử dụng mmWave để mô phỏng mạng 5G

Bài viết tiếp theo Thị trường dịch vụ nguồn mở sẽ vượt mốc 47 tỷ đô la Mỹ vào năm 2024

Suhas Desai

Tác giả làm việc tại Tech Mahindra. Anh ấy viết về mã nguồn mở và bảo mật. Trong thời gian rảnh rỗi, anh ấy tình nguyện vì các mục đích xã hội

Mô-đun Python nào được sử dụng để kết nối với cơ sở dữ liệu?

SQLite có lẽ là cơ sở dữ liệu đơn giản nhất để kết nối với ứng dụng Python vì bạn không cần cài đặt bất kỳ mô-đun Python SQL bên ngoài nào để làm như vậy. Theo mặc định, bản cài đặt Python của bạn chứa thư viện Python SQL có tên sqlite3 mà bạn có thể sử dụng để tương tác với cơ sở dữ liệu SQLite.

Các bước để kết nối với cơ sở dữ liệu trong Python là gì?

Cách kết nối Cơ sở dữ liệu bằng Python .
Cài đặt trình điều khiển MySQL
Tạo đối tượng kết nối
Tạo đối tượng con trỏ
Thực hiện truy vấn

Phương pháp nào được sử dụng để kết nối cơ sở dữ liệu trong Python Mcq?

Trả lời. Để tạo kết nối giữa cơ sở dữ liệu MySQL và ứng dụng python, hàm connect[] được sử dụng.

Chúng ta có thể kết nối Python với cơ sở dữ liệu không?

Python có thể được sử dụng trong các ứng dụng cơ sở dữ liệu . Một trong những cơ sở dữ liệu phổ biến nhất là MySQL.

Chủ Đề