[英]Python: TypeError: an integer is required (got type str)
here i am getting the error,在这里我收到错误,
File "C:\Users\DEEDEVTR01\Desktop\python\pyenv\lib\site-packages\MySQLdb\__init__.py",
line 84, in Connect
return Connection(*args, **kwargs) File "C:\Users\DEEDEVTR01\Desktop\python\pyenv\lib\site-packages\MySQLdb\connections.py",
line 179, in __init__
super(Connection, self).__init__(*args, **kwargs2) TypeError: an integer is required (got type str)
I think the connection is proper but I am not sure why I am getting this error, it would be great if anyone can help me figure out what I am doing wrong.我认为连接是正确的,但我不确定为什么会出现此错误,如果有人能帮我弄清楚我做错了什么,那就太好了。
settings.py设置.py
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.mysql',
'HOST': 'localhost',
'NAME': 'deebaco',
'USER': 'deebaco',
'PASSWORD': 'deebaco',
'PORT': '3306',
}
}
import MySQLdb
def connection():
conn = MySQLdb.connect(database="deebaco", user = "deebaco", password = "deebaco", host = "localhost", port = "3306")
print ("Opened database connect successfully")
cur = conn.cursor()
return cur, conn
views.py视图.py
from rest_framework import views
from rest_framework.views import APIView
import MySQLdb
from testpro.settings import connection
cur, conn = connection()
class Product_ListAPIView(APIView):
def get(self,request,format=None):
cur,conn = connection()
query = ''' SELECT * FROM products'''
try:
with conn.cursor(MySQLdb.cursors.DictCursor) as cursor:
cursor.execute(query)
result = cursor.fetchall()
data = list(result)
return Response({"data":result},status=status.HTTP_200_OK)
except Exception as e:
print(e)
I think the connection is proper but I am not sure why I am getting this error, it would be great if anyone can help me figure out what I am doing wrong我认为连接是正确的,但我不确定为什么会出现此错误,如果有人能帮我弄清楚我做错了什么,那就太好了
Does the port of the connection have quotes?连接的端口有引号吗?
If so, remove the quotes in order to solve this issue:如果是这样,请删除引号以解决此问题:
from:从:
port = "3306"
to到
port = 3306
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.