How can I use executemany to insert into MySQL a list of dictionaries in Python

itemBank = [] for row in rows: itemBank.append(( tempRow2[‘Item_Name’], tempRow1[‘Item_Price’], tempRow3[‘Item_In_Stock’], tempRow4[‘Item_Max’], getTimeExtra )) #append data q = “”” insert ignore into TABLE1 ( Item_Name, Item_Price, Item_In_Stock, Item_Max, Observation_Date ) values (%s,%s,%s,%s,%s) “”” try: x.executemany(q, itemBank) conn.commit() except: conn.rollback() Hope it will help you

Python MYSQL update statement

It should be: cursor.execute (“”” UPDATE tblTableName SET Year=%s, Month=%s, Day=%s, Hour=%s, Minute=%s WHERE Server=%s “””, (Year, Month, Day, Hour, Minute, ServerID)) You can also do it with basic string manipulation, cursor.execute (“UPDATE tblTableName SET Year=%s, Month=%s, Day=%s, Hour=%s, Minute=%s WHERE Server=”%s” ” % (Year, Month, Day, Hour, Minute, ServerID)) but this way is discouraged … Read more

Installing mysqlclient in Python 3.6 in windows

Had the same problem, searched the web etc. Here this answer: mysql-python install error: Cannot open include file ‘config-win.h’ It has all the instructions. In short go to this site: https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient: At that website you will find mysqlclient‑1.3.13‑cp36‑cp36m‑win32.whl mysqlclient‑1.3.13‑cp36‑cp36m‑win_amd64.whl Download the correct file for your platform. Then use your downloaded wheels file with pip and … Read more

When to close cursors using MySQLdb

Instead of asking what is standard practice, since that’s often unclear and subjective, you might try looking to the module itself for guidance. In general, using the with keyword as another user suggested is a great idea, but in this specific circumstance it may not give you quite the functionality you expect. As of version … Read more

How to insert pandas dataframe via mysqldb into database?

Update: There is now a to_sql method, which is the preferred way to do this, rather than write_frame: df.to_sql(con=con, name=”table_name_for_df”, if_exists=”replace”, flavor=”mysql”) Also note: the syntax may change in pandas 0.14… You can set up the connection with MySQLdb: from pandas.io import sql import MySQLdb con = MySQLdb.connect() # may need to add some other … Read more