Python/pymysql

From Wiki
< Python
Revision as of 18:28, 18 June 2020 by Marcluer (talk | contribs)

MYSQL related

Examples

  • connect
import pymysql

db = pymysql.connect(host=localhost, port=3306, user="root", password="1234")
  • show version
try:
    with db:
        cursor = db.cursor()
        cursor.execute("SELECT VERSION()")
        version = cursor.fetchone()
        print("Database version: {}".format(version[0]))
except Exception as e:
    print(e)
  • create database
try:
    with db:
        cursor = db.cursor()
        cursor.execute("CREATE DATABASE carsearch")
except Exception as e:
    print(e)

Connection

Cursor

Documentation