Error encountered during data insertion in Python with MySQL database

I need assistance with inserting values into a MySQL database after extracting them using Python from a website. However, I encounter an error while trying to insert them into the database. The error message is as follows: mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2.

Any help would be greatly appreciated. Thank you in advance. Below is the code snippet:


conn = mysql.connector.connect(user='root', password='root', host='localhost', database ='crawler_database')
mycursor = conn.cursor()
source_code = requests.get(item_url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text)
my_list=[]
# SCRAPE DATA FROM THE WEB PAGE
for product_name in soup.findAll('span', {'itemprop': 'name'}):
    prod_name = " " + product_name.string
    my_list.append(prod_name)
    print("Product Name: " + prod_name)
for low_price in soup.findAll('span', {'itemprop': 'lowPrice'}):
    l_price = " " + low_price.string
    my_list.append(l_price)
    print("Lowest Price: " + low_price.string)
for high_price in soup.findAll('span', {'itemprop': 'highPrice'}):
    h_price = " " + high_price.string
    my_list.append(h_price)
    print("Highest Price: " + high_price.string)
# FORMAT AND CLEANSE THE DATA
for specs in soup.findAll('ul', {'class': 'c-spec'}):
    crop_str1 = str(specs).replace("<li>", "")
    crop_str2 = crop_str1.replace("</li>", ",")
    crop_str3 = crop_str2.replace("</ul>", "%")
    crop_str4 = crop_str3.replace('<ul class="c-spec">', '$')
    crop_str5 = crop_str4.partition('$')[-1].rpartition('%')[0]
    my_list2 = crop_str5.split(",")
    merged_list = my_list + my_list2
    print(merged_list)
    d0 = " " + str(merged_list[0])
    d1 = " " + str(merged_list[1])
    d2 = " " + str(merged_list[2])
    d3 = " " + str(merged_list[3])
    d4 = " " + str(merged_list[4])
    d5 = " " + str(merged_list[5])
    d6 = " " + str(merged_list[6])
    print("Android OS: " + merged_list[3])
    print("Camera: " + merged_list[4])
    print("SIM: " + merged_list[5])
    print("Display Size: " + merged_list[6])
    mycursor.execute("""INSERT INTO mobile_spec(prod_name, low_price, high_price, android_os, camera, sim, d_size) VALUES (%s, %s, %s, %s, %s, %s, %s)""",(d0, d1, d2, d3, d4, d5, d6))
    conn.commit()

Answer №1

When sending data to MySQL, make sure to pass values instead of variable names such as "d0". To achieve this, use placeholders in your query like so:

mycursor.execute("""INSERT INTO mobile_spec(android_os, camera, sim, d_size)
                 VALUES (%s, %s, %s , %s)""", (d0, d1, d2, d3))

If you already have the parameters stored in a list, you can simplfy your code further by directly passing that list to MySQL without individual d* variables.

mycursor.execute("""INSERT INTO mobile_spec(android_os, camera, sim, d_size)
                 VALUES (%s, %s, %s , %s)""", my_list)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to clearing input fields and displaying an error message through Ajax when the requested data is not found within a MySQL database

I am looking for a way to clear the textboxes and display an alert message if the data is not found in the MySQL table. I encountered an issue when using type: 'json' as removing it makes the alert work, but then the data from the MySQL table doe ...

If an <a> element is missing its href attribute, it will be non-clickable and unable to

I'm in the process of using Python/Selenium to automate the extraction of calendar appointment information from a specific website. When interacting with the webpage, triggering an action displays a popup containing detailed calendar appointment info ...

Implementing a primary key into an already established MySQL table using alembic

Attempting to add a primary key column with an 'id' identifier to an existing MySQL table using alembic. The process involved the following steps... op.add_column('mytable', sa.Column('id', sa.Integer(), nullable=False)) op.a ...

How can a single value from one table be allocated to multiple values in another table?

There are 3 distinct tables that I am working with: process table: | projectNo | process | studio | +-----------+---------+--------+ | 170001 | ANM BLD | APEX | | 170001 | ANM BLD | CANVAS | | 170002 | CGI MOD | APEX | | 170003 | CGI MOD | ...

What is the best way to create a Python test case for a function with numerous print statements being executed?

I recently decided to dive into writing my own unit test cases. I successfully created tests for basic integer addition and subtraction functions, but now I wanted to tackle something more complex like a Password Checker. Here is the code snippet I've ...

Encountering a "File Not Found" error when attempting to load and read a JSON file within a Jupyter notebook in

I am currently working within the IBM Cloud Watson Studio environment. In my project, I have uploaded a JSON file containing updated population data per municipality in Norway. However, when attempting to call and read this file using "with open" ...

Can search criteria be saved for later use?

I operate a classifieds website that allows users to search through ads. I am contemplating ways to save search criteria so that users can easily reuse them for future searches without having to input the same details over and over again. For instance, th ...

Error message: "The maximum image resolution in skimage has been exceeded, causing a

Currently, I am utilizing scikit-image to process images in Python. However, through my testing, I have discovered that scikit-image is unable to handle images with high resolutions. When attempting to use an image with a resolution of 3024 x 4032, a Memor ...

List of tuples indicating the index of tuples that are incomplete

Issue at hand: I currently possess a series of tuples (a,b,c) and I am seeking to locate the index of the first tuple commencing with specified 'a' and 'b'. For instance: list = [(1,2,3), (3,4,5), (5,6,7)] In this case, if a = 1 and ...

When PyCharm is not in debug mode, it runs smoothly without any issues, but as soon as debug

UPDATE: After reverting back to PyCharm version 2017.2.4, the script started working again. It seems that the issue was with the IDE and not the script itself. Recently, my script has been running smoothly until today. Strangely, it only runs without any ...

The error UnboundLocalError occurs as the variable 'vectorizer' is being referenced before it has been assigned a value

Can anyone assist me in identifying the error in this code: UnboundLocalError: local variable 'vectorizer' referenced before assignment def find_n_grams(data, labels, ntrain, mn=1, mx=1, nm=500, binary = False, donorm = False, stopwords = ...

Ways to prevent a Chrome tab controlled by Selenium/Chromedriver from closing

Is there a way to prevent the google chrome tab from closing immediately after it's opened? Could time.sleep or while loop be used in some manner? from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager from selenium.we ...

What is the process for obtaining the most recent stock price through Fidelity's screening tool?

Trying to extract the latest stock price using Fidelity's screener. For instance, the current market value of AAPL stands at $165.02, accessible via this link. Upon inspecting the webpage, the price is displayed within this tag: <div _ngcontent-cx ...

"Exploring the Concept of Defining a Driver in Pytest

Having recently transitioned to using Python for Selenium tests after working on Java and C# projects, I find myself a bit confused. Unlike in Java where you define the driver with a specific Type, how do I go about defining it in Python? # How can I set u ...

Obtaining fresh data with Peewee and Flask only upon restarting

I'm encountering a peculiar issue with my Flask + Peewee application. I have a web app that inserts data into a MySQL database. My Flask app also connects to this database using Peewee. The problem arises when I insert data with the web app - subse ...

After installing MSYS2, pip is not found in the path and shows a command not found error

I just completed the installation of pip on MSYS2 by executing the command below within the MSYS2 shell: pacman -S mingw-w64-x86_64-python-pip (web page for the package: ) Upon successful installation, my attempt to run pip in the MSYS2 shell returned an ...

Why are the options at the end of group(1) placed at the start of group(2) in re.search? I need help fixing my regex structure

I'm attempting to categorize text paragraphs into data fields based on common patterns. Here's an example: tstStr = 'Locations of performance are California, North Carolina and Pennsylvania, with a Sept. 14, 2017, performance completion dat ...

Discrepancy in output between Tflite model on Android (ML Vision) and Python platforms

While implementing ML Vision API to generate embeddings from a FaceNet model and comparing cosine distances between the two embeddings, I have noticed significant discrepancies in the output between the Android and Python versions. Surprisingly, the Python ...

Steps to remove the smallest number from an array: if there are multiple smallest numbers, remove the first one

I am currently working on a script that takes an array of random numbers as input. I have successfully implemented code to remove the lowest number in the array, but I'm facing an issue when there are multiple occurrences of this number. How can I ens ...

extract the content of CSS pseudo-elements using Python or Selenium

Currently, I am working on automating a web service using Selenium and Python. My ultimate goal is to extract the text "test" located below. However, I am facing some challenges in figuring out if this is feasible through Selenium or any Python library. & ...