Advertisement
Guest User

Untitled

a guest
Apr 14th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. import pymysql.cursors
  2.  
  3. # Connect to the database
  4. connection = pymysql.connect(host='localhost',
  5. user='root',
  6. #password='passwd',
  7. db='db',
  8. charset='utf8mb4',
  9. cursorclass=pymysql.cursors.DictCursor)
  10.  
  11. cursor = connection.cursor()
  12.  
  13. # SELECT statement
  14. sql = "SELECT * FROM table;"
  15. cursor.execute(sql)
  16.  
  17. result = cursor.fetchone() # Results are returned as a dictionary
  18. print(result)
  19.  
  20. # INSERT statement
  21. sql = "INSERT INTO table ('email', 'password') VALUES (%s, %s)"
  22. cursor.execute(sql, ('test@example.com', 'password'))
  23. # Statements that change the content
  24. # of a table MUST be committed!
  25. connection.commit()
  26.  
  27. # Close connection
  28. connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement