Python Forum
how to insert list into database - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how to insert list into database (/thread-14165.html)



how to insert list into database - zubair - Nov-17-2018

I am storing <p> tag data into list. When i apply insert query then last line of the list stored in the database.
I want to store all data in database that is available in link.
Below is the code of adding <p> tags data into list:

 content_html_container=soup_loop.find('div', class_="template__main")
    content=content_html_container.find_all('div',class_="story__content")

    for list_p in content:
        p=list_p.find_all('p')
        for li in p:
              p=li.text
#              print(p)
              p_list.append(p)
below is the database query:

 news_detail_query="INSERT INTO news_detail (`date`, heading, content, id_authors, id_categories) VALUES (%s, %s, %s, %s, %s)"
    insert_news=(timestamp,heading,p,authors_id,categories_id)
    result  = cursor.execute(news_detail_query, insert_news)
    cnx.commit()



RE: how to insert list into database - Larz60+ - Nov-17-2018

see: https://stackoverflow.com/questions/3070384/how-to-store-a-list-in-a-column-of-a-database-table


RE: how to insert list into database - rajesh1997 - Feb-05-2019

X = "HELLO"
Y = "RAJESH"
list = [X,Y]

INSERT INTO table VALUES ('%s') %s list