python_pymysql基本操作

import pymysql.cursors


# Connect to the database

connection = pymysql.connect(host='localhost',

                             user='root',

                             password='password',

                             db='mysql_test',

                             charset='utf8mb4',

                             cursorclass=pymysql.cursors.DictCursor)


try:

    with connection.cursor() as cursor:

        # Create a new record

        sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"

        cursor.execute(sql, ('[email protected]', 'test123!'))


    # connection is not autocommit by default. So you must commit to save

    # your changes.

    connection.commit()


    with connection.cursor() as cursor:

        # Read a single record

        sql = "SELECT `id`, `email`,`password` FROM `users` WHERE `email`=%s"

        cursor.execute(sql, ('[email protected]',))

        result = cursor.fetchone()

        print(result)

finally:

    connection.close()


https://pypi.org/project/PyMySQL/#documentation


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章