Python/pymysql: Difference between revisions

From Wiki
No edit summary
No edit summary
Line 32: Line 32:
     print(e)
     print(e)
</pre>
</pre>
== Connection ==
== Cursor ==
== Documentation ==
* https://pymysql.readthedocs.io/en/latest/modules/index.html

Revision as of 18:28, 18 June 2020

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