Python/pymysql: Difference between revisions

From Wiki
No edit summary
No edit summary
Line 38: Line 38:


== Connection ==
== Connection ==
<pre>
db = pymysql.connect(host=localhost, port=3306, user="root", password="1234")
</pre>
* parameters:
** host
** port (default: 3306)
** user
** password
** autocommit (default: false)
* db.commit()
* db.close()
* db.rollback() #rollback current transaction


== Cursor ==
== Cursor ==
* cursor.execute("CREATE DATABASE carsearch")
* cursor.close()
* cursor.fetchone()
* cursor.fetchmany()
* cursor.fetchall()





Revision as of 18:50, 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

db = pymysql.connect(host=localhost, port=3306, user="root", password="1234")
  • parameters:
    • host
    • port (default: 3306)
    • user
    • password
    • autocommit (default: false)
  • db.commit()
  • db.close()
  • db.rollback() #rollback current transaction

Cursor

  • cursor.execute("CREATE DATABASE carsearch")
  • cursor.close()
  • cursor.fetchone()
  • cursor.fetchmany()
  • cursor.fetchall()


Documentation