在以下位置查看相关问题:难以将 python3 连接到 mariadb - take2一个>
See related question at: Difficulty connecting python3 to a mariadb - take2
我在 OpenSuse (Leap 15.1) 上安装了 Python2.7 和 python3,并且我已经安装了 MariaDB
I have Python2.7 and python3 installed on an OpenSuse (Leap 15.1) and I have I have MariaDB installed
uname -a Linux TRANQUILITY 4.12.14-lp151.28.13-default #1 SMP Wed Aug 7 07:20:16 UTC 2019 (0c09ad2) x86_64 x86_64 x86_64 GNU/Linux
python2.7 --version Python 2.7.14
python3 --version Python 3.6.5
mysql --version mysql Ver 15.1 Distrib 10.2.25-MariaDB, for Linux (x86_64) using EditLine wrapper
我的程序旨在解析一个充满 csv 文件的文件夹,检查该文件是否为 CSV,如果是,则在数据库表中检查该文件是否已被解析过.不幸的是,它不会连接到数据库!
My program is meant to parse a folder full of csv files, check whether the file is a CSV and then, if it is, check in a DB table whether that file has been parsed before. Unfortunately, it will not connect to the DB!
在 bash 控制台上,我可以轻松连接到数据库,但运行我的 python 脚本会返回以下内容:
On a bash console, I can easily connect to the DB but running my python script returns the following:
Attempting to connect to DB: SunnyData2 <--my debug print
OK, going for it next! <--my debug print
DB read failed! <----python response
None <--my debug print
我已经搜索过 StackOverflow &谷歌,似乎我正在使用的 MySQLdb
驱动程序在 Python3 上不起作用,所以我更改了文件的顶部以调用 python 2.7,而不是 python 3
I have searched StackOverflow & Google and it appears that the MySQLdb
driver I'm using doesn't work on Python3, so I've changed the top of the file to invoke python 2.7, instead of python 3
#!/usr/bin/python2.7
来自
#!/usr/bin/python
但这没有帮助(相同的返回消息),所以我也尝试使用 python 3 导入 mysql-connector-python
而不是 MySQLdb
but that didn't help (same return message), so with python 3 I've also tried importing mysql-connector-python
instead of MySQLdb
但随后我收到警告说该驱动程序不存在/未被识别!
but then I got a warning that that driver didn't exist/wasn't recognised!
我的进口是:
from os import listdir
from datetime import datetime #could this just be 'import datetime'?
import MySQLdb
import shutil
import syslog
import os, sys
我建立数据库连接的功能是这样的:
and my function for establishing a db connection is this:
def connect_to_DB(action):
"""
Connect to the DB
"""
h="localhost", # host
u="root", # username
p="p/w redacted"
d = "db name redacted"
if action == 'connect':
#SD.close()
print 'Attempting to connect to DB: ' + d
try:
print "OK, going for it next!"
SD = MySQLdb.connect(host=h, user=u, password=p, database=d)
syslog.syslog('Connected to DB')
print "Connection Object: " + SD
return SD
except Exception as e:
syslog.syslog('Failed to connect to DB')
return False
elif action == 'disconnect':
try:
#mariadb_connection.close()
SD.close()
syslog.syslog('Disconnected from DB')
return 'closed'
except Exception as e:
syslog.syslog('Failed to disconnect from DB')
return False
有什么建议吗?
我现在做了以下建议的更改:
I have now made the following suggested changes:
try
&除了
SD = MySQLdb.connect(str(h), str(u), str(p), str(d))
和 SD = MySQLdb.connect(host='localhost', user='root', passwd='aPassword', db='blah')
python2.7 -v ./SunnyCSV2DB.py
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# /usr/lib64/python2.7/site.pyc matches /usr/lib64/python2.7/site.py
import site # precompiled from /usr/lib64/python2.7/site.pyc
# /usr/lib64/python2.7/os.pyc matches /usr/lib64/python2.7/os.py
import os # precompiled from /usr/lib64/python2.7/os.pyc
import errno # builtin
import posix # builtin
# /usr/lib64/python2.7/posixpath.pyc matches /usr/lib64/python2.7/posixpath.py
import posixpath # precompiled from /usr/lib64/python2.7/posixpath.pyc
# /usr/lib64/python2.7/stat.pyc matches /usr/lib64/python2.7/stat.py
import stat # precompiled from /usr/lib64/python2.7/stat.pyc
# /usr/lib64/python2.7/genericpath.pyc matches /usr/lib64/python2.7/genericpath.py
import genericpath # precompiled from /usr/lib64/python2.7/genericpath.pyc
# /usr/lib64/python2.7/warnings.pyc matches /usr/lib64/python2.7/warnings.py
import warnings # precompiled from /usr/lib64/python2.7/warnings.pyc
# /usr/lib64/python2.7/linecache.pyc matches /usr/lib64/python2.7/linecache.py
import linecache # precompiled from /usr/lib64/python2.7/linecache.pyc
# /usr/lib64/python2.7/types.pyc matches /usr/lib64/python2.7/types.py
import types # precompiled from /usr/lib64/python2.7/types.pyc
# /usr/lib64/python2.7/UserDict.pyc matches /usr/lib64/python2.7/UserDict.py
import UserDict # precompiled from /usr/lib64/python2.7/UserDict.pyc
# /usr/lib64/python2.7/_abcoll.pyc matches /usr/lib64/python2.7/_abcoll.py
import _abcoll # precompiled from /usr/lib64/python2.7/_abcoll.pyc
# /usr/lib64/python2.7/abc.pyc matches /usr/lib64/python2.7/abc.py
import abc # precompiled from /usr/lib64/python2.7/abc.pyc
# /usr/lib64/python2.7/_weakrefset.pyc matches /usr/lib64/python2.7/_weakrefset.py
import _weakrefset # precompiled from /usr/lib64/python2.7/_weakrefset.pyc
import _weakref # builtin
# /usr/lib64/python2.7/copy_reg.pyc matches /usr/lib64/python2.7/copy_reg.py
import copy_reg # precompiled from /usr/lib64/python2.7/copy_reg.pyc
# /usr/lib64/python2.7/traceback.pyc matches /usr/lib64/python2.7/traceback.py
import traceback # precompiled from /usr/lib64/python2.7/traceback.pyc
# /usr/lib64/python2.7/sysconfig.pyc matches /usr/lib64/python2.7/sysconfig.py
import sysconfig # precompiled from /usr/lib64/python2.7/sysconfig.pyc
# /usr/lib64/python2.7/re.pyc matches /usr/lib64/python2.7/re.py
import re # precompiled from /usr/lib64/python2.7/re.pyc
# /usr/lib64/python2.7/sre_compile.pyc matches /usr/lib64/python2.7/sre_compile.py
import sre_compile # precompiled from /usr/lib64/python2.7/sre_compile.pyc
import _sre # builtin
# /usr/lib64/python2.7/sre_parse.pyc matches /usr/lib64/python2.7/sre_parse.py
import sre_parse # precompiled from /usr/lib64/python2.7/sre_parse.pyc
# /usr/lib64/python2.7/sre_constants.pyc matches /usr/lib64/python2.7/sre_constants.py
import sre_constants # precompiled from /usr/lib64/python2.7/sre_constants.pyc
dlopen("/usr/lib64/python2.7/lib-dynload/_locale.so", 2);
import _locale # dynamically loaded from /usr/lib64/python2.7/lib-dynload/_locale.so
# /usr/lib64/python2.7/_sysconfigdata.pyc matches /usr/lib64/python2.7/_sysconfigdata.py
import _sysconfigdata # precompiled from /usr/lib64/python2.7/_sysconfigdata.pyc
import encodings # directory /usr/lib64/python2.7/encodings
# /usr/lib64/python2.7/encodings/__init__.pyc matches /usr/lib64/python2.7/encodings/__init__.py
import encodings # precompiled from /usr/lib64/python2.7/encodings/__init__.pyc
# /usr/lib64/python2.7/codecs.pyc matches /usr/lib64/python2.7/codecs.py
import codecs # precompiled from /usr/lib64/python2.7/codecs.pyc
import _codecs # builtin
# /usr/lib64/python2.7/encodings/aliases.pyc matches /usr/lib64/python2.7/encodings/aliases.py
import encodings.aliases # precompiled from /usr/lib64/python2.7/encodings/aliases.pyc
# /usr/lib64/python2.7/encodings/utf_8.pyc matches /usr/lib64/python2.7/encodings/utf_8.py
import encodings.utf_8 # precompiled from /usr/lib64/python2.7/encodings/utf_8.pyc
Python 2.7.14 (default, Oct 12 2017, 15:50:02) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
dlopen("/usr/lib64/python2.7/lib-dynload/datetime.so", 2);
import datetime # dynamically loaded from /usr/lib64/python2.7/lib-dynload/datetime.so
import MySQLdb # directory /usr/lib64/python2.7/site-packages/MySQLdb
# /usr/lib64/python2.7/site-packages/MySQLdb/__init__.pyc matches /usr/lib64/python2.7/site-packages/MySQLdb/__init__.py
import MySQLdb # precompiled from /usr/lib64/python2.7/site-packages/MySQLdb/__init__.pyc
# /usr/lib64/python2.7/site-packages/MySQLdb/release.pyc matches /usr/lib64/python2.7/site-packages/MySQLdb/release.py
import MySQLdb.release # precompiled from /usr/lib64/python2.7/site-packages/MySQLdb/release.pyc
dlopen("/usr/lib64/python2.7/site-packages/MySQLdb/_mysql.so", 2);
# /usr/lib64/python2.7/site-packages/MySQLdb/_exceptions.pyc matches /usr/lib64/python2.7/site-packages/MySQLdb/_exceptions.py
import MySQLdb._exceptions # precompiled from /usr/lib64/python2.7/site-packages/MySQLdb/_exceptions.pyc
# /usr/lib64/python2.7/site-packages/MySQLdb/compat.pyc matches /usr/lib64/python2.7/site-packages/MySQLdb/compat.py
import MySQLdb.compat # precompiled from /usr/lib64/python2.7/site-packages/MySQLdb/compat.pyc
import MySQLdb._mysql # dynamically loaded from /usr/lib64/python2.7/site-packages/MySQLdb/_mysql.so
import MySQLdb.constants # directory /usr/lib64/python2.7/site-packages/MySQLdb/constants
# /usr/lib64/python2.7/site-packages/MySQLdb/constants/__init__.pyc matches /usr/lib64/python2.7/site-packages/MySQLdb/constants/__init__.py
import MySQLdb.constants # precompiled from /usr/lib64/python2.7/site-packages/MySQLdb/constants/__init__.pyc
# /usr/lib64/python2.7/site-packages/MySQLdb/constants/FIELD_TYPE.pyc matches /usr/lib64/python2.7/site-packages/MySQLdb/constants/FIELD_TYPE.py
import MySQLdb.constants.FIELD_TYPE # precompiled from /usr/lib64/python2.7/site-packages/MySQLdb/constants/FIELD_TYPE.pyc
# /usr/lib64/python2.7/site-packages/MySQLdb/times.pyc matches /usr/lib64/python2.7/site-packages/MySQLdb/times.py
import MySQLdb.times # precompiled from /usr/lib64/python2.7/site-packages/MySQLdb/times.pyc
dlopen("/usr/lib64/python2.7/lib-dynload/time.so", 2);
import time # dynamically loaded from /usr/lib64/python2.7/lib-dynload/time.so
# /usr/lib64/python2.7/shutil.pyc matches /usr/lib64/python2.7/shutil.py
import shutil # precompiled from /usr/lib64/python2.7/shutil.pyc
# /usr/lib64/python2.7/fnmatch.pyc matches /usr/lib64/python2.7/fnmatch.py
import fnmatch # precompiled from /usr/lib64/python2.7/fnmatch.pyc
# /usr/lib64/python2.7/collections.pyc matches /usr/lib64/python2.7/collections.py
import collections # precompiled from /usr/lib64/python2.7/collections.pyc
dlopen("/usr/lib64/python2.7/lib-dynload/_collections.so", 2);
import _collections # dynamically loaded from /usr/lib64/python2.7/lib-dynload/_collections.so
dlopen("/usr/lib64/python2.7/lib-dynload/operator.so", 2);
import operator # dynamically loaded from /usr/lib64/python2.7/lib-dynload/operator.so
# /usr/lib64/python2.7/keyword.pyc matches /usr/lib64/python2.7/keyword.py
import keyword # precompiled from /usr/lib64/python2.7/keyword.pyc
# /usr/lib64/python2.7/heapq.pyc matches /usr/lib64/python2.7/heapq.py
import heapq # precompiled from /usr/lib64/python2.7/heapq.pyc
dlopen("/usr/lib64/python2.7/lib-dynload/itertools.so", 2);
import itertools # dynamically loaded from /usr/lib64/python2.7/lib-dynload/itertools.so
dlopen("/usr/lib64/python2.7/lib-dynload/_heapq.so", 2);
import _heapq # dynamically loaded from /usr/lib64/python2.7/lib-dynload/_heapq.so
import thread # builtin
dlopen("/usr/lib64/python2.7/lib-dynload/zlib.so", 2);
import zlib # dynamically loaded from /usr/lib64/python2.7/lib-dynload/zlib.so
dlopen("/usr/lib64/python2.7/lib-dynload/bz2.so", 2);
import bz2 # dynamically loaded from /usr/lib64/python2.7/lib-dynload/bz2.so
import pwd # builtin
dlopen("/usr/lib64/python2.7/lib-dynload/grp.so", 2);
import grp # dynamically loaded from /usr/lib64/python2.7/lib-dynload/grp.so
dlopen("/usr/lib64/python2.7/lib-dynload/syslog.so", 2);
import syslog # dynamically loaded from /usr/lib64/python2.7/lib-dynload/syslog.so
Attempting to connect to DB: SunnyData2
OK, going for it next!
# /usr/lib64/python2.7/site-packages/MySQLdb/connections.pyc matches /usr/lib64/python2.7/site-packages/MySQLdb/connections.py
import MySQLdb.connections # precompiled from /usr/lib64/python2.7/site-packages/MySQLdb/connections.pyc
# /usr/lib64/python2.7/site-packages/MySQLdb/cursors.pyc matches /usr/lib64/python2.7/site-packages/MySQLdb/cursors.py
import MySQLdb.cursors # precompiled from /usr/lib64/python2.7/site-packages/MySQLdb/cursors.pyc
# /usr/lib64/python2.7/__future__.pyc matches /usr/lib64/python2.7/__future__.py
import __future__ # precompiled from /usr/lib64/python2.7/__future__.pyc
# /usr/lib64/python2.7/functools.pyc matches /usr/lib64/python2.7/functools.py
import functools # precompiled from /usr/lib64/python2.7/functools.pyc
dlopen("/usr/lib64/python2.7/lib-dynload/_functools.so", 2);
import _functools # dynamically loaded from /usr/lib64/python2.7/lib-dynload/_functools.so
# /usr/lib64/python2.7/site-packages/MySQLdb/constants/CLIENT.pyc matches /usr/lib64/python2.7/site-packages/MySQLdb/constants/CLIENT.py
import MySQLdb.constants.CLIENT # precompiled from /usr/lib64/python2.7/site-packages/MySQLdb/constants/CLIENT.pyc
# /usr/lib64/python2.7/site-packages/MySQLdb/converters.pyc matches /usr/lib64/python2.7/site-packages/MySQLdb/converters.py
import MySQLdb.converters # precompiled from /usr/lib64/python2.7/site-packages/MySQLdb/converters.pyc
# /usr/lib64/python2.7/decimal.pyc matches /usr/lib64/python2.7/decimal.py
import decimal # precompiled from /usr/lib64/python2.7/decimal.pyc
dlopen("/usr/lib64/python2.7/lib-dynload/math.so", 2);
import math # dynamically loaded from /usr/lib64/python2.7/lib-dynload/math.so
# /usr/lib64/python2.7/numbers.pyc matches /usr/lib64/python2.7/numbers.py
import numbers # precompiled from /usr/lib64/python2.7/numbers.pyc
# /usr/lib64/python2.7/threading.pyc matches /usr/lib64/python2.7/threading.py
import threading # precompiled from /usr/lib64/python2.7/threading.pyc
# /usr/lib64/python2.7/locale.pyc matches /usr/lib64/python2.7/locale.py
import locale # precompiled from /usr/lib64/python2.7/locale.pyc
# /usr/lib64/python2.7/site-packages/MySQLdb/constants/FLAG.pyc matches /usr/lib64/python2.7/site-packages/MySQLdb/constants/FLAG.py
import MySQLdb.constants.FLAG # precompiled from /usr/lib64/python2.7/site-packages/MySQLdb/constants/FLAG.pyc
dlopen("/usr/lib64/python2.7/lib-dynload/array.so", 2);
import array # dynamically loaded from /usr/lib64/python2.7/lib-dynload/array.so
# /usr/lib64/python2.7/weakref.pyc matches /usr/lib64/python2.7/weakref.py
import weakref # precompiled from /usr/lib64/python2.7/weakref.pyc
DB read failed!
None
# clear __builtin__._
# clear sys.path
# clear sys.argv
# clear sys.ps1
# clear sys.ps2
# clear sys.exitfunc
# clear sys.exc_type
# clear sys.exc_value
# clear sys.exc_traceback
# clear sys.last_type
# clear sys.last_value
# clear sys.last_traceback
# clear sys.path_hooks
# clear sys.path_importer_cache
# clear sys.meta_path
# clear sys.flags
# clear sys.float_info
# restore sys.stdin
# restore sys.stdout
# restore sys.stderr
# cleanup __main__
# cleanup[1] datetime
# cleanup[1] sysconfig
# cleanup[1] exceptions
# cleanup[1] zipimport
# cleanup[1] decimal
# cleanup[1] threading
# cleanup[1] signal
# cleanup[1] google
# cleanup[1] locale
# cleanup[1] MySQLdb
# cleanup[1] encodings
# cleanup[1] abc
# cleanup[1] math
# cleanup[1] _functools
# cleanup[1] thread
# cleanup[1] weakref
# cleanup[1] itertools
# cleanup[1] __future__
# cleanup[1] _collections
# cleanup[1] operator
# cleanup[1] _heapq
# cleanup[1] sre_constants
# cleanup[1] _warnings
# cleanup[1] _codecs
# cleanup[1] pwd
# cleanup[1] _sysconfigdata
# cleanup[1] keyword
# cleanup[1] syslog
# cleanup[1] zlib
# cleanup[1] posix
# cleanup[1] encodings.aliases
# cleanup[1] MySQLdb.release
# cleanup[1] site
# cleanup[1] numbers
# cleanup[1] shutil
# cleanup[1] bz2
# cleanup[1] grp
# cleanup[1] MySQLdb.converters
# cleanup[1] MySQLdb.compat
# cleanup[1] MySQLdb.constants
# cleanup[1] _weakref
# cleanup[1] _weakrefset
# cleanup[1] time
# cleanup[1] MySQLdb.constants.FIELD_TYPE
# cleanup[1] functools
# cleanup[1] MySQLdb.times
# cleanup[1] collections
# cleanup[1] MySQLdb._exceptions
# cleanup[1] encodings.utf_8
# cleanup[1] MySQLdb.connections
# cleanup[1] fnmatch
# cleanup[1] codecs
# cleanup[1] MySQLdb.constants.FLAG
# cleanup[1] traceback
# cleanup[1] MySQLdb.constants.CLIENT
# cleanup[1] array
# cleanup[1] MySQLdb._mysql
# cleanup[1] MySQLdb.cursors
# cleanup[1] heapq
# cleanup[1] re
# cleanup[1] _locale
# cleanup[1] sre_compile
# cleanup[1] _sre
# cleanup[1] sre_parse
# cleanup[2] UserDict
# cleanup[2] posixpath
# cleanup[2] errno
# cleanup[2] os.path
# cleanup[2] copy_reg
# cleanup[2] linecache
# cleanup[2] _abcoll
# cleanup[2] genericpath
# cleanup[2] stat
# cleanup[2] warnings
# cleanup[2] types
# cleanup[2] os
# cleanup sys
# cleanup __builtin__
# cleanup ints: 40 unfreed ints
# cleanup floats: 3 unfreed floats
根据文档,您错误地命名了一些参数.这可能会让它误以为你正在给它一个元组,尽管不知道为什么.应该是 passwd
和 db
According to the docs, you've misnamed some of your parameters. That might be confusing it into thinking you're giving it a tuple, although not sure why. Should be passwd
and db
https://mysqlclient.readthedocs.io/user_guide.html#connection-objects
这篇关于将 Python3 连接到 MariaDB 的困难 - 取 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!