0
CODE:
import mysql.connector
from bs4 import BeautifulSoup
import requests
URL = "https://parade.com/937586/parade/life-quotes/"

web_page = requests.get(URL)

soup = BeautifulSoup(web_page.text, "html.parser")

quote = [(x.get_text(strip=True, separator=" ")) for x in soup.select(
        'span[data-parade-type="promoarea"] .figure_block ~ p')]

db = mysql.connector.Connect(
        host="127.0.0.1",
        user="root",
        password="Demon@",
        database="my_quotes",
        charset="utf8mb4"
    )

mycursor = db.cursor()

sql = "INSERT INTO quoteslist (id, Quotes) VALUES (%S,%S)"
mycursor.execute(sql, quote)
db.commit()
db.close()

OUTPUT ERROR:

line 32, in <module>mycursor.execute(sql, quote)
mysql.connector.errors.ProgrammingError: Not all parameters were 
used in the SQL statement

In this code, I done WebScraping all quotes in URL and I want to store data to msql.So, I create connection python server to mysql server and i created DATABASE , created table column Quotes and id, When come to insert data, I try using both execute or executemany to store data multiple rows. But its getting error, Can anyone suggest me which line code went wrong.

See image showing NULL in cloumn Not insert data

1 Answers1

0

In your original code, the 'quote' variable is just a list of strings. However, your insert requires two VALUES. Therefore you need to split the string into its component parts - i.e. the ID and the actual quote. However, there is a problem in this particular dataset because the first token in a quote isn't necessarily a number - so you need to allow for that. Try this:-

import mysql.connector as MYSQL
import requests
URL = 'https://parade.com/937586/parade/life-quotes/'
SQL = 'INSERT INTO quoteslist (id, Quotes) VALUES (%s,%s)'
CONFIG = {
    'user': 'root',
    'password': 'Demon@',
    'host': '127.0.0.1',
    'database': 'my_quotes',
    'charset': 'utf8mb4'
}
SELECT = 'span[data-parade-type="promoarea"] .figure_block ~ p'
GT = {'strip': True, 'separator': ' '}
with requests.Session() as session:
    web_page = session.get(URL)
    web_page.raise_for_status()
    soup = BeautifulSoup(web_page.text, "html.parser")
    quote = [(x.get_text(**GT)) for x in soup.select(SELECT)]
    with MYSQL.Connect(**CONFIG) as db:
        mycursor = db.cursor()
        for q in quote:
            idx = q.split()[0]
            if idx[0].isdigit():
                text = q[len(idx):].strip()
                params = (idx.replace('.', ''), text)
                mycursor.execute(SQL, params)
        db.commit()

Note that the first token in a quote (when it's a viable index number) ends with period ('.'), so I've removed that

  • OK - Well, I had to code "blind" because I obviously can't run it. What's the error? –  Aug 09 '21 at 11:52
  • Yes I did but there was no issue at line 9 in the original version. I just changed it to use a context manager for the database connection which is much cleaner. I've also created a table in my MySQL database and executed this and it works perfectly –  Aug 09 '21 at 12:40
  • error line 29, in mycursor.execute(SQL, params) line 559, in execute raise errors.ProgrammingError(mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement, after u edited i got this error .Before not edited code i try again there is no errors but data is not inserted. –  Aug 09 '21 at 12:40
  • That was because your SQL INSERT string was wrong. You should use %s and not %S –  Aug 09 '21 at 12:41
  • I suggest that you literally copy/paste my answer. Assuming you have the correct mysql module installed and your table is constructed appropriately, this will work –  Aug 09 '21 at 12:45
  • error showing line 29, in mycursor.execute(SQL, params) mysql.connector.errors.DataError: 1406 (22001): Data too long for column 'Quotes' at row 1, How to modify this error? –  Aug 09 '21 at 13:45
  • As I said in my previous comment, make sure your table is constructed appropriately. Chances are that your VARCHAR specification doesn't allow for very long strings. When I tested this I set the Quotes column as VARCHAR(1024) and that works with your data –  Aug 09 '21 at 14:11
  • now that how to change to VARCHAR(1024) , I think i kept VARCHAR(255) , or should i create new table? or from starting new database i should create? help me to modify varchar –  Aug 09 '21 at 14:35
  • ALTER TABLE quoteslist MODIFY Quotes VARCHAR(1024); –  Aug 09 '21 at 15:46
  • SQL= 'AlTER TABLE quotelist MODIFY Quotes VARCHAR(1024)' this line is correct to atler table? –  Aug 09 '21 at 16:03
  • I changed and I got inserted data .Now how to download file? –  Aug 09 '21 at 16:22
  • **GT and **CONFIG What it means? I didn't get proper understanding in google.Can u explain me? –  Aug 09 '21 at 16:33
  • What it means **? u use in code and i ask u previous comment u did'nt explain me –  Aug 10 '21 at 07:00
  • This isn't a tutorial –  Aug 10 '21 at 07:25
  • Don't be over strictly just I ask u say in one line no need paragraph . If u want to help others help that is good move . Because I didn't find in google also . That's y I ask u . –  Aug 10 '21 at 09:45
  • https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters –  Aug 10 '21 at 10:10