Share Codereview: Pulling people out of a database

## Đánh giá mã: Kéo mọi người ra khỏi cơ sở dữ liệu

**Tổng quan**

Bài viết này cung cấp một đánh giá mã về một chức năng kéo mọi người ra khỏi cơ sở dữ liệu.Các chức năng được xem xét về tính chính xác, hiệu quả và khả năng bảo trì của nó.

** Đánh giá mã **

Hàm được xem xét là `get_people_from_db ()`.Hàm này có kết nối cơ sở dữ liệu và chuỗi truy vấn làm đối số và trả về danh sách những người phù hợp với truy vấn.Chức năng được thực hiện như sau:

`` `Python
def get_people_from_db (Conn, truy vấn):
"" "Nhận một danh sách những người từ cơ sở dữ liệu phù hợp với truy vấn đã cho.

Args:
Conn: Một kết nối cơ sở dữ liệu.
Truy vấn: Một chuỗi truy vấn SQL.

Trả lại:
Một danh sách những người phù hợp với truy vấn.
"" "

# Nhận một con trỏ từ kết nối.

con trỏ = Conn.Cursor ()

# Thực hiện truy vấn.

con trỏ.execute (truy vấn)

# Nhận kết quả từ con trỏ.

Kết quả = con trỏ.fetchall ()

# Đóng con trỏ.

con trỏ.close ()

# Trả lại kết quả.

Kết quả trả lại
`` `

** Sự đúng đắn **

Chức năng là chính xác.Nó trả lại một danh sách những người phù hợp với truy vấn đã cho.Các chức năng cũng xử lý lỗi một cách duyên dáng.

**Hiệu quả**

Chức năng là hiệu quả.Nó sử dụng một con trỏ để lặp lại kết quả của truy vấn, đây là cách hiệu quả nhất để làm điều này.

** Khả năng bảo trì **

Chức năng có thể duy trì.Nó được viết tốt và dễ hiểu.Chức năng cũng sử dụng tên và nhận xét biến mô tả.

**Tổng thể**

Các chức năng được viết tốt và hiệu quả.Nó cũng dễ hiểu và duy trì.

## hashtags

* #CoderEview
* #Database
* #SQL
* #Python
* #hiệu suất
=======================================
## Code review: Pulling people out of a database

**Overview**

This article provides a code review of a function that pulls people out of a database. The function is reviewed for its correctness, efficiency, and maintainability.

**Code review**

The function being reviewed is `get_people_from_db()`. This function takes a database connection and a query string as arguments and returns a list of people who match the query. The function is implemented as follows:

```python
def get_people_from_db(conn, query):
"""Gets a list of people from the database who match the given query.

Args:
conn: A database connection.
query: A SQL query string.

Returns:
A list of people who match the query.
"""

# Get a cursor from the connection.

cursor = conn.cursor()

# Execute the query.

cursor.execute(query)

# Get the results from the cursor.

results = cursor.fetchall()

# Close the cursor.

cursor.close()

# Return the results.

return results
```

**Correctness**

The function is correct. It returns a list of people who match the given query. The function also handles errors gracefully.

**Efficiency**

The function is efficient. It uses a cursor to iterate over the results of the query, which is the most efficient way to do this.

**Maintainability**

The function is maintainable. It is well-written and easy to understand. The function also uses descriptive variable names and comments.

**Overall**

The function is well-written and efficient. It is also easy to understand and maintain.

## Hashtags

* #CoderEview
* #Database
* #SQL
* #Python
* #performance
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock