Home  >  Article  >  Database  >  python连接mysql数据库出错,已经试了网上的几种方法,,求助

python连接mysql数据库出错,已经试了网上的几种方法,,求助

WBOY
WBOYOriginal
2016-06-07 15:52:351294browse

http://blog.csdn.net/lishan9133/article/details/7024989 之前是按照上面的教程安装了mysqldb模块,可是在实际操作过程中,找不到对应行的代码,所以并没有按照上面的改。 打开数据库连接 db = MySQLdb.connect( host='192.188.80.41', port=3306, user='r

http://blog.csdn.net/lishan9133/article/details/7024989
之前是按照上面的教程安装了mysqldb模块,可是在实际操作过程中,找不到对应行的代码,所以并没有按照上面的改。

打开数据库连接

db = MySQLdb.connect(
host='192.188.80.41',
port=3306,
user='root',
passwd='123456',
db='db_zhuanjia',
charset='utf8')

使用cursor()方法获取操作游标

cursor = db.cursor()

SQL 插入语句

sql = """ INSERT INTO EMPLOYEE(Sex)
VALUES (ans2(1))"""
try:
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
db.commit()
except:
# Rollback in case there is any error
db.rollback()

关闭数据库连接

db.close()

报错代码:
Traceback (most recent call last):
File "G:\python\1\craweler2.py", line 44, in
charset='utf8')
File "G:\python\1\lib\site-packages\MySQLdb__init__.py", line 80, in Connect
return Connection(*args, **kwargs)
File "G:\python\1\lib\site-packages\MySQLdb\connections.py", line 187, in init
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on '192.188.80.41' (10060)")

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn