daily pastebin goal
23%
SHARE
TWEET

Untitled

a guest Jan 8th, 2019 107 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pandas
  2. import MySQLdb
  3.  
  4.  
  5. mydb = MySQLdb.connect(
  6.     host='localhost',
  7.     user='',
  8.     passwd='',
  9.     db='mydb',
  10. )
  11.  
  12. cursor = mydb.cursor()
  13. my_csv = pandas.read_csv("test.csv", usecols=['id', 'title', 'body', 'author_id'])
  14. insert_stmt = '''INSERT INTO `blog_post` (`Id`,`Title`,`Body`,`Author_id`) VALUES (%s, %s, %s, %s)'''
  15.  
  16.  
  17. # for row in my_csv:
  18. #     print(row)
  19. #     cursor.execute(insert_stmt, row)
  20.  
  21. cursor.executemany(insert_stmt, my_csv[1:]
  22.  
  23. mydb.commit()
  24. cursor.close()
  25. print("done")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top