python - pymysql无法连接:pymysql.err.ProgrammingError

查看:1390
本文介绍了python - pymysql无法连接:pymysql.err.ProgrammingError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

最近开始学习mysql,百度到python3.5只能用pymysql,自己网上找了些教程学习,可是各种错误,来论坛找各位大神学习一下。

不废话了。这是网上copy下来的代码,我一直不清楚里面的db是填写什么,是Connection Name吗?还是Connection Name项目下的databases,我一开始用的是db=Connection Name,后来改成了databases name但是都没有用处。

from datetime import date, datetime, timedelta
import pymysql.cursors

# 连接配置信息
config = {
    'host': '127.0.0.1',
    'port': 3306,
    'user': 'root',
    'password': 'mysql123',
    'db': 'new_schema',
    'charset': 'utf8mb4',
    'cursorclass': pymysql.cursors.DictCursor,
}
# 创建连接
connection = pymysql.connect(**config)

# 获取明天的时间
tomorrow = datetime.now().date() + timedelta(days=1)

# 执行sql语句
try:
    with connection.cursor() as cursor:
        # 执行sql语句,插入记录
        sql = 'INSERT INTO employees (first_name, last_name, hire_date, gender, birth_date) VALUES (%s, %s, %s, %s, %s)'
        cursor.execute(sql, ('Robin', 'Zhyea', tomorrow, 'M', date(1989, 6, 14)));
    # 没有设置默认自动提交,需要主动提交,以保存所执行的语句
    connection.commit()

finally:
    connection.close();

附上我的pycharm

附上我的mysql workbench截图,

解决方案

你new_schemes这个数据库建了,但是employees这个表没建吧

这篇关于python - pymysql无法连接:pymysql.err.ProgrammingError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆