pycharm和MySQL

安装pymysql:pip install pymysql

建立连接:conn = pymysql.connect(host=‘ ‘, port=3306, user=‘ ‘, password=‘  ‘, db=‘  ‘, charset=‘utf8‘)

注意编码是utf8

cursor方法返回游标对象

with conn.sursor() as cursor:

  cursor.execute(str)

execute代表执行,str是SQL语句

默认情况下事务不自动提交,所以成功需要提交commit,失败就回滚

try:

  with conn.cursor() as cursor:

  result = cursor.execute(str)

  if resulr == 1:

    print(‘添加成功‘)

  conn.commit()

except pymysql.MySQLError as error:

  print(error)

  connect.rollback()

finally:

  conn.close()

 

运行成功返回1

 

pycharm和MySQL

上一篇:CentOS8下安装oracle客户端


下一篇:jQuery:jQuery选择器