热门IT资讯网

三分钟搞懂python_pymysql基本操作

发表于:2024-11-30 作者:热门IT资讯网编辑
编辑最后更新 2024年11月30日,下面讲讲关于python_pymysql基本操作,文字的奥妙在于贴近主题相关。所以,闲话就不谈了,我们直接看下文吧,相信看完python_pymysql基本操作这篇文章你一定会有所受益。import

下面讲讲关于python_pymysql基本操作,文字的奥妙在于贴近主题相关。所以,闲话就不谈了,我们直接看下文吧,相信看完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

对于以上python_pymysql基本操作相关内容,大家还有什么不明白的地方吗?或者想要了解更多相关,可以继续关注我们的行业资讯板块。

0