I want to store the data extracted from the list in sqlite3 (database)

Asked 2 months ago, Updated 2 months ago, 3 views

■ Data after extraction: Variable name: name
['nameA', 'nameB', 'nameC', 'nameD']

■ sqlite3:table name:user
header:name

■ After storing sqlite3
name
nameA
nameB
nameC
nameD

I want to store it as above.
I tried as below, but the variable name goes straight into one line.
I want to store the namesA-D in the database one line at a time.

# variable
name = '\n'.join(name)

# DB processing

dbpath="user.db"
con=sqlite3.connect(dbpath)
cursor=con.cursor()

sql = 'INSERT INTO user(name) VALUES(?)'
cursor.execute(sql,(name,))
con.commit()
con.close

python database sqlite

2022-09-30 11:32

1 Answers

Do not process name='\n'.join(name), but try the cursor.execute(sql,(name,)) part as follows:

for entry in name:
    cursor.execute(sql, (entry,))


2022-09-30 11:32

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.