Python数据库创建-MySQL错误

标签 python mysql database creation

我一直在尝试使用 MySQL 模块在 python 中创建数据库。代码如下:

import MySQLdb
conn = MySQLdb.connect(host = '127.0.0.1', user='root', passwd='', db='test', port='3306')
cursor = conn.cursor()
cursor.execute("SELECT VERSION()")
row = cursor.fetchone()
print "server version:",row[0]
cursor.close()
conn.close()

我在与主机名相关的初始步骤中遇到了错误,我将主机名从 localhost 更改为本地 IP,并添加了一个端口关键字,该关键字似乎仅适用于引号。除此之外,我遇到以下错误:

Traceback (most recent call last):
  File "C:\Python27\server_version", line 4, in <module>
    conn = MySQLdb.connect(host = '127.0.0.1', user='root', passwd='', db='bhavin', port='3306')
  File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
TypeError: an integer is required

最佳答案

port 应该是整数,而不是字符串:

conn = MySQLdb.connect(host='127.0.0.1', 
                       user='root', 
                       passwd='', 
                       db='test', 
                       port=3306)

不过,请注意 3306default port如果未明确提供,则使用:

port

TCP port of MySQL server. Default: standard port (3306).

关于Python数据库创建-MySQL错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27738698/

相关文章:

java - 在 SQLite 数据库中按数组存储数组

ios - 来自核心数据的 SQL 类型提取请求

python - 打开 excel 文件 python 所以它是可见的

MYSQL 和 LabVIEW

python - 在 pyspark 数据帧的其余列中搜索 column1 中的值

python - Django DeleteView 不会从数据中删除对象

php - 执行此 PHP/SQL/Sort 操作的最快方法是什么

database - 从一个 DBMS 迁移到另一个

python - 使用 TensorFlow Dataset API 的纪元计数器

python - 使用 chmod +x python 将文件添加到 tar