Answer the question
In order to leave comments, you need to log in
How to add elements from a list to a database?
Hello!
I have a list of url addresses (more than 1000) that need to be placed in a database table. The first column should contain the url addresses themselves, and the right column should just contain the 'url' designation. As a result, nothing happened. Please tell me what is wrong.
import sqlite3
con = sqlite3.connect("base.db")
cur = con.cursor()
with con:
cur.execute("""
CREATE TABLE base_url (
url TEXT,
name TEXT
);
""")
url = ['url', 'url1']
for element in url:
con = sqlite3.connect("base.db")
sql = 'INSERT INTO base_url(url, name) VALUES({}, url)'.format(element) # здесь element является url адресом, а url это просто обозначение для второго столбца name
con.execute(sql)
con.commit()
Answer the question
In order to leave comments, you need to log in
Quotes are needed to insert text values. And also messed up with cur and con
cur = con.cursor()
sql = 'INSERT INTO base_url(url, name) VALUES("{}", "url")'.format(element) # здесь element является url адресом, а url это просто обозначение для второго столбца name
print (sql)
cur.execute(sql)
con.commit()
con.close()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question