Python/pymysql: Difference between revisions

From Wiki
No edit summary
No edit summary
Line 1: Line 1:
== MYSQL related ==
{| class="wikitable" style="float:right; margin-left: 10px;" width="400px"
| Quick links
|-
|
* [[Mysql/CheatSheet]]
* [[Mysql/CheatSheet]]
* [[Mysql]]
* [[Mysql]]
|}


== Examples ==
== Examples ==

Revision as of 18:28, 18 June 2020

Quick links

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