Last active
May 29, 2019 20:02
-
-
Save kuzminT/dc77fd4daa657c3f2f79df9a1cf13148 to your computer and use it in GitHub Desktop.
Examples for pymysql lib with python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""" | |
:link: https://github.com/PyMySQL/PyMySQL | |
""""" | |
import pymysql | |
import pymysql.cursors | |
connection = pymysql.connect(host="localhost", | |
user="root", | |
password="1234", | |
db="test", | |
charset="utf8mb4", | |
cursorclass=pymysql.cursors.DictCursor | |
) | |
try: | |
# Example for multibatching to insert many values in the same time | |
with connection.cursor() as cursor: | |
sql = "INSERT INTO projects (title, description) VALUES (%s, %s)" | |
args = (('test project 1', "this project added by python"), | |
('test project 2', "this project added by python 2")) | |
cursor.executemany(sql, args) | |
connection.commit() | |
with connection.cursor() as cursor: | |
sql = "SELECT title FROM projects" | |
cursor.execute(sql) | |
result = cursor.fetchall() | |
print(result) | |
finally: | |
connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment