简体   繁体   English

Python:类型错误:需要一个整数(得到类型 str)

[英]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.

相关问题 TypeError:Python中需要一个整数(类型为str) - TypeError: an integer is required (got type str) in python Python:TypeError 需要 integer(获取类型 str) - Python: TypeError an integer is required(got type str) Python 2-如何修复TypeError:必须为整数(类型为str) - Python 2 - How to fix TypeError: an integer is required (got type str) TypeError:函数联接时需要一个整数(got type str)-python - TypeError: an integer is required (got type str) on function join - python TypeError:Python Selenium Webdri中需要一个整数(类型为str) - TypeError: an integer is required (got type str) in python selenium webdri python / flask:TypeError:需要一个整数(得到类型str) - python/flask: TypeError: an integer is required (got type str) Python Flask-TypeError:必须为整数(类型为str) - Python Flask - TypeError: an integer is required (got type str) 类型错误:在 Python 中打开文件时需要 integer(获取类型 str) - TypeError: an integer is required (got type str) when opening file in Python Pyarrow:TypeError:需要一个整数(得到类型 str) - Pyarrow: TypeError: an integer is required (got type str) TypeError:在Tkinter中需要一个整数(got类型str) - TypeError: an integer is required (got type str) in Tkinter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM