请教关于 PYTHON3.6 下使用 MYSQL 数据库的问题

请教关于 PYTHON3.6 下使用 MYSQL 数据库的问题,第1张

1、和数据库建立连接

2、执行sql语句,接收返回值

3、关闭数据库连接

1、MySQL数据库要用MySQLdb模块,但Python用来链接MySQL的第三方库MySQLdb不支持Python3.x

特别说明:我在我的电脑上实验时,我的python是2.7.2版本,安装对应版本的MySQLdb之后直接可以运行,并与数据库连接成功,所以如果大家也像我一样顺利的话,下面的就不需要看了,直接跳过,看第2点如何执行sql语句即可!如果安装之后出现异常,可以参考一下下面的解决办法。

错误描述

python==3.5

django==1.7

django创建项目时报错如下:

Traceback (most recent call last):

File "/root/envs/django-test/bin/django-admin", line 11, in <module>

sys.exit(execute_from_command_line())

File "/root/envs/django-test/lib/python3.5/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line

utility.execute()

File "/root/envs/django-test/lib/python3.5/site-packages/django/core/management/__init__.py", line 354, in execute

django.setup()

File "/root/envs/django-test/lib/python3.5/site-packages/django/__init__.py", line 18, in setup

from django.utils.log import configure_logging

File "/root/envs/django-test/lib/python3.5/site-packages/django/utils/log.py", line 10, in <module>

from django.views.debug import ExceptionReporter, get_exception_reporter_filter

File "/root/envs/django-test/lib/python3.5/site-packages/django/views/debug.py", line 10, in <module>

from django.http import (HttpResponse, HttpResponseServerError,

File "/root/envs/django-test/lib/python3.5/site-packages/django/http/__init__.py", line 4, in <module>

from django.http.response import (HttpResponse, StreamingHttpResponse,

File "/root/envs/django-test/lib/python3.5/site-packages/django/http/response.py", line 13, in <module>

from django.core.serializers.json import DjangoJSONEncoder

File "/root/envs/django-test/lib/python3.5/site-packages/django/core/serializers/__init__.py", line 23, in <module>

from django.core.serializers.base import SerializerDoesNotExist

File "/root/envs/django-test/lib/python3.5/site-packages/django/core/serializers/base.py", line 6, in <module>

from django.db import models

File "/root/envs/django-test/lib/python3.5/site-packages/django/db/models/__init__.py", line 6, in <module>

from django.db.models.query import Q, QuerySet, Prefetch # NOQA

File "/root/envs/django-test/lib/python3.5/site-packages/django/db/models/query.py", line 13, in <module>

from django.db.models.fields import AutoField, Empty

File "/root/envs/django-test/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 18, in <module>

from django import forms

File "/root/envs/django-test/lib/python3.5/site-packages/django/forms/__init__.py", line 6, in <module>

from django.forms.fields import * # NOQA

File "/root/envs/django-test/lib/python3.5/site-packages/django/forms/fields.py", line 18, in <module>

from django.forms.utils import from_current_timezone, to_current_timezone

File "/root/envs/django-test/lib/python3.5/site-packages/django/forms/utils.py", line 15, in <module>

from django.utils.html import format_html, format_html_join, escape

File "/root/envs/django-test/lib/python3.5/site-packages/django/utils/html.py", line 16, in <module>

from .html_parser import HTMLParser, HTMLParseError

File "/root/envs/django-test/lib/python3.5/site-packages/django/utils/html_parser.py", line 12, in <module>

HTMLParseError = _html_parser.HTMLParseError

AttributeError: module 'html.parser' has no attribute 'HTMLParseError'

(django-test) root@localhost:~/source/djangotest# django-admin startproject superlists

Traceback (most recent call last):

File "/root/envs/django-test/bin/django-admin", line 11, in <module>

sys.exit(execute_from_command_line())

File "/root/envs/django-test/lib/python3.5/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line

utility.execute()

File "/root/envs/django-test/lib/python3.5/site-packages/django/core/management/__init__.py", line 354, in execute

django.setup()

File "/root/envs/django-test/lib/python3.5/site-packages/django/__init__.py", line 18, in setup

from django.utils.log import configure_logging

File "/root/envs/django-test/lib/python3.5/site-packages/django/utils/log.py", line 10, in <module>

from django.views.debug import ExceptionReporter, get_exception_reporter_filter

File "/root/envs/django-test/lib/python3.5/site-packages/django/views/debug.py", line 10, in <module>

from django.http import (HttpResponse, HttpResponseServerError,

File "/root/envs/django-test/lib/python3.5/site-packages/django/http/__init__.py", line 4, in <module>

from django.http.response import (HttpResponse, StreamingHttpResponse,

File "/root/envs/django-test/lib/python3.5/site-packages/django/http/response.py", line 13, in <module>

from django.core.serializers.json import DjangoJSONEncoder

File "/root/envs/django-test/lib/python3.5/site-packages/django/core/serializers/__init__.py", line 23, in <module>

from django.core.serializers.base import SerializerDoesNotExist

File "/root/envs/django-test/lib/python3.5/site-packages/django/core/serializers/base.py", line 6, in <module>

from django.db import models

File "/root/envs/django-test/lib/python3.5/site-packages/django/db/models/__init__.py", line 6, in <module>

from django.db.models.query import Q, QuerySet, Prefetch # NOQA

File "/root/envs/django-test/lib/python3.5/site-packages/django/db/models/query.py", line 13, in <module>

from django.db.models.fields import AutoField, Empty

File "/root/envs/django-test/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 18, in <module>

from django import forms

File "/root/envs/django-test/lib/python3.5/site-packages/django/forms/__init__.py", line 6, in <module>

from django.forms.fields import * # NOQA

File "/root/envs/django-test/lib/python3.5/site-packages/django/forms/fields.py", line 18, in <module>

from django.forms.utils import from_current_timezone, to_current_timezone

File "/root/envs/django-test/lib/python3.5/site-packages/django/forms/utils.py", line 15, in <module>

from django.utils.html import format_html, format_html_join, escape

File "/root/envs/django-test/lib/python3.5/site-packages/django/utils/html.py", line 16, in <module>

from .html_parser import HTMLParser, HTMLParseError

File "/root/envs/django-test/lib/python3.5/site-packages/django/utils/html_parser.py", line 12, in <module>

HTMLParseError = _html_parser.HTMLParseError

AttributeError: module 'html.parser' has no attribute 'HTMLParseError'12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576771234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677

原因

HTMLParseError在pythons3.5已经没有了

解决方法

将python版本回退到3.3或3.4

升级django版本


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6825855.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-28
下一篇 2023-03-28

发表评论

登录后才能评论

评论列表(0条)

保存