'how can i insert 2 or many different list in mysql using python [duplicate]
hello can some help me to add 2 list to a database(mysql) in python, here is the code but i can figure out where is the problem (i am new to mysql).
def scan(self):
"""Scan the pc for instaled app in `desktop`"""
file_location = []
file_name = []
ending = ('.url','.lnk','.rtf')
desktop = os.path.join(os.path.join(os.environ['USERPROFILE']),'OneDrive', 'Desktop')
exclude = set(['one drive'])
for r, dirs, f in os.walk(desktop, topdown=True):
dirs[:] = [d for d in dirs if d not in exclude]
for file in f:
if file.endswith(ending):
file_location.append(r + "\\" + file)
for f_name in os.listdir(desktop):
if f_name.endswith(tuple(ending)):
file_name.append(f_name)
query2 = ("INSERT INTO application (app_name) VALUES (%s)")
query3 = ("INSERT INTO application (app_location) VALUES (%s)")
my_cursor.executemany(query2, [(n, ) for n in file_name])
my_cursor.executemany(query3, [(l, ) for l in file_location])
mydb.commit()
and this is the error that i have:
TypeError: executemany() takes 3 positional arguments but 4 were given
the result that i need is to place file_name and file_location in the same row but different colon respectively
ex:
file_name | file_location
fornite | fortnite.exe
Solution 1:[1]
You can execute the query with only this:
i = 0
while i < len(file_location):
my_cursor.execute("INSERT INTO application app_name , app_location VALUES" + str(file_name[i]) + "," + str(file_location[i]))
i = i + 1
mydb.commit()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Matheus Leles |
