在这里我得到了错误
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)
我认为连接是正确的,但是我不确定为什么会出现此错误,如果有人可以帮助我弄清楚我在做什么错,那将是很好的选择。
settings.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
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)
我认为连接是正确的,但是我不确定为什么会出现此错误,如果有人可以帮助我弄清楚我在做什么错,那将是很棒的事情
答案 0 :(得分:2)
连接端口是否带有引号?
如果是这样,请删除引号以解决此问题:
来自:
port = "3306"
到
port = 3306