7 của đối tượngCode language: Python [python]
from mysql.connector import MySQLConnection, Error from python_mysql_dbconfig import read_db_config def insert_books[books]: query = "INSERT INTO books[title,isbn] " \ "VALUES[%s,%s]" try: db_config = read_db_config[] conn = MySQLConnection[**db_config] cursor = conn.cursor[] cursor.executemany[query, books] conn.commit[] except Error as e: print['Error:', e] finally: cursor.close[] conn.close[] def main[]: books = [['Harry Potter And The Order Of The Phoenix', '9780439358071'], ['Gone with the Wind', '9780446675536'], ['Pride and Prejudice [Modern Library Classics]', '9780679783268']] insert_books[books] if __name__ == '__main__': main[]
MySQLCursor
, MySQL Connector/Python dịch câu lệnh 3 thành câu lệnh chứa nhiều danh sách giá trị
INSERT INTO books[title,isbn] VALUES['Harry Potter And The Order Of The Phoenix', '9780439358071'], ['Gone with the Wind', '9780446675536'], ['Pride and Prejudice [Modern Library Classics]', '9780679783268'];
Code language: SQL [Structured Query Language] [sql]
Chào buổi tối techgods. , tự hỏi nếu bạn có thể giúp tôi ít tuổi. ] mới sử dụng python và tôi đang học cách cạo từ video, tôi có thể lấy thẻ của nó hoạt động như một bản xuất sang excel, điều tôi muốn làm bây giờ là kết nối nó với mariadb, vì tôi đang sử dụng . ] có vẻ như nó đang kết nối tốt, chỉ là không thích thứ gì đó và tôi cảm thấy bối rối
vì vậy tôi lấy cùng một mã và thực hiện một chút thay đổi cú pháp và tôi tiếp tục gặp lỗi sau
Tập tin "C. /Users/scott/PycharmProjects/untitled/test. py", dòng 37, trong con trỏ. thực hiện [truy vấn] mariadb. Lỗi lập trình. Bạn có lỗi trong cú pháp SQL của mình;
import requests
from bs4 import BeautifulSoup
import mariadb
url = '//dc.urbanturf.com/pipeline'
response = requests.get[url]
soup = BeautifulSoup[response.content, 'html.parser']
pipeline_items = soup.find_all['div', attrs={'class': 'pipeline-item'}]
rows = []
columns = ['listing title', 'listing url', 'listing image url', 'location', 'Project type', 'Status', 'Size']
for item in pipeline_items:
# title, image url, listing url
listing_title = item.a['title']
listing_url = item.a['href']
listing_image_url = item.a.img['src']
for p_tag in item.find_all['p']:
if not p_tag.h2:
if p_tag.span.text == 'Location:':
p_tag.span.extract[]
property_location = p_tag.text.strip[]
elif p_tag.span.text == 'Project type:':
p_tag.span.extract[]
property_type = p_tag.text.strip[]
elif p_tag.span.text == 'Status:':
p_tag.span.extract[]
property_status = p_tag.text.strip[]
elif p_tag.span.text == 'Size:':
p_tag.span.extract[]
property_size = p_tag.text.strip[]
conn = mariadb.connect[host="xxxxxxxx", port=3306, user="xxxxxxxxxx", passwd="xxxxxxxxxxxxx", db="test"]
# Creating a cursor object using the cursor[] method
cursor = conn.cursor[]
query = "INSERT INTO Properties[listing_title, listing_url, listing_image_url, property_location,
property_type, property_status, property_size]"
cursor.execute[query]
conn.commit[]
print["Data inserted"]
conn.close[]
Bất kỳ trợ giúp nào cũng được đánh giá rất cao. ]
Mã mới. không còn lỗi nữa, dường như không thể lấy dữ liệu để nhập. nó tạo ra các hàng trống. tên của bảng khớp với mã hiện tại
Quan trọng. Chú ý tuyên bố. mydb.commit[]
. Cần phải thực hiện các thay đổi, nếu không thì không có thay đổi nào được thực hiện đối với bảng
Chèn nhiều hàng
Để chèn nhiều hàng vào một bảng, hãy sử dụng phương thức executemany[]
Tham số thứ hai của phương thức executemany[]
là danh sách các bộ, chứa dữ liệu bạn muốn chèn