我问了一个标题相同的上一个问题,但所有建议均无效。我认为,将所有尝试的方法都精炼成一个补充性问题,比起将其遗漏到原始帖子中(而不是保留原始反馈:Difficulty connecting Python3 to a MariaDB - take 1的所有反馈)来提出一个更清晰的问题-请让我知道(礼貌地)是否有替代的首选方法
我在OpenSuse(Leap 15.1)上安装了Python2.7和python3,并且我已经安装了MariaDB
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,然后(如果是)在数据库表中检查该文件之前是否已被解析。 python脚本(权限:754
)和所有csv文件(权限:644
)都位于同一文件夹中。
很遗憾,它不会从脚本连接到数据库!
使用完全相同的凭据,并且使用相同的路径(即包含csvs和py脚本的文件夹):
me@server:/project/dataCSVs> mysql -u root -p
Enter password: <myPW>
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 45
Server version: 10.2.25-MariaDB SUSE package
<snip>
MariaDB [(none)]> use myDB;
<snip>
Database changed
MariaDB [myDB]> show tables;
+----------------------+
| Tables_in_myDB |
+----------------------+
| DTP |
| Files |
+----------------------+
2 rows in set (0.00 sec)
import pymysql
,我可以轻松地连接到数据库me@server:/project/dataCSVs> python3
Python 3.6.5 (default, Mar 31 2018, 19:45:04) [GCC] on linux
<snip>
>>> import pymysql;
>>> SD = pymysql.connect(host='127.0.0.1', user='root', password='myPW', database='myDB', port=3306);
>>> print(SD);
<pymysql.connections.Connection object at 0x7fae9d277978>
#!/usr/bin/python2.7
和import MySQLdb
连接到数据库#!/usr/bin/python3
和import pymysql
连接到数据库专注于pymysql
驱动程序和python3
,我的代码中尝试连接的功能和相关附加位是:
from os import listdir
from datetime import datetime
import pymysql
import shutil
import syslog
import os, sys
<snipped>
def connect_to_DB(action):
"""
Connect to the DB
"""
h = "127.0.0.1", # host
u = "root", # username
p = "myPW" # password
d = "myDB" # database
Pt = 3306 # port
if action == 'connect':
print('Attempting to connect to DB: ' + d)
print("OK, going for it next!")
# tried all three of these connect options
SD = pymysql.connect(host=str(h), user=str(u), passwd=str(p), db=str(d), port=Pt)
#SD = pymysql.connect(host='127.0.0.1', user='root', passwd='myPW', db='myDB', port=3306)
#SD = pymysql.connect(str(h), str(u), str(p), str(d), Pt)
syslog.syslog('Connected to DB')
return SD
elif action == 'disconnect':
SD.close()
syslog.syslog('Disconnected from DB')
return 'closed'
<snipped>
# Main start of program
<snipped - commented out everything apart from connect and disconnect to DB>
connection = connect_to_DB('connect')
<snipped>
disconnection = connect_to_DB('disconnect')
用python3 myScript.py
调用,我得到:
me@MyServer:/project/dataCSVs> python3 myScript.py
Attempting to connect to DB: myDB
OK, going for it next!
DB read failed!
None
使用python3 -v myScript.py
调用,我得到了(我只粘贴了看起来相关的部分,即对pymysql的引用):
me@server:/project/dataCSVs> python3 -v myScript.py
import _frozen_importlib # frozen
<snipped>
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/__init__.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/__init__.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/__init__.cpython-36.pyc'
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/_compat.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/_compat.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/_compat.cpython-36.pyc'
import 'pymysql._compat' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37cca9240>
# /usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/__init__.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/constants/__init__.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/__init__.cpython-36.pyc'
import 'pymysql.constants' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37cca9358>
# /usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/FIELD_TYPE.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/constants/FIELD_TYPE.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/FIELD_TYPE.cpython-36.pyc'
import 'pymysql.constants.FIELD_TYPE' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37cca94e0>
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/converters.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/converters.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/converters.cpython-36.pyc'
# /usr/lib64/python3.6/__pycache__/decimal.cpython-36.pyc matches /usr/lib64/python3.6/decimal.py
<snipped>
import 're' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37cc92b38>
# /usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/FLAG.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/constants/FLAG.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/FLAG.cpython-36.pyc'
import 'pymysql.constants.FLAG' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37c8179e8>
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/charset.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/charset.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/charset.cpython-36.pyc'
import 'pymysql.charset' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37c817b38>
import 'pymysql.converters' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37cca96a0>
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/err.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/err.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/err.cpython-36.pyc'
# /usr/lib64/python3.6/__pycache__/struct.cpython-36.pyc matches /usr/lib64/python3.6/struct.py
# code object from '/usr/lib64/python3.6/__pycache__/struct.cpython-36.pyc'
# extension module '_struct' loaded from '/usr/lib64/python3.6/lib-dynload/_struct.cpython-36m-x86_64-linux-gnu.so'
# extension module '_struct' executed from '/usr/lib64/python3.6/lib-dynload/_struct.cpython-36m-x86_64-linux-gnu.so'
import '_struct' # <_frozen_importlib_external.ExtensionFileLoader object at 0x7fc37c55eac8>
import 'struct' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37c55e898>
# /usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/ER.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/constants/ER.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/ER.cpython-36.pyc'
import 'pymysql.constants.ER' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37c55ec50>
import 'pymysql.err' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37cca9b00>
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/times.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/times.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/times.cpython-36.pyc'
import 'pymysql.times' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37cca9be0>
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/connections.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/connections.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/connections.cpython-36.pyc'
# /usr/lib64/python3.6/__pycache__/__future__.cpython-36.pyc matches /usr/lib64/python3.6/__future__.py
<snipped>
import 'traceback' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37c594278>
# /usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/CLIENT.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/constants/CLIENT.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/CLIENT.cpython-36.pyc'
import 'pymysql.constants.CLIENT' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37b5f8da0>
# /usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/COMMAND.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/constants/COMMAND.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/COMMAND.cpython-36.pyc'
import 'pymysql.constants.COMMAND' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37b5fe080>
# /usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/CR.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/constants/CR.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/CR.cpython-36.pyc'
import 'pymysql.constants.CR' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37b5fe128>
# /usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/SERVER_STATUS.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/constants/SERVER_STATUS.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/SERVER_STATUS.cpython-36.pyc'
import 'pymysql.constants.SERVER_STATUS' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37b5fe0f0>
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/cursors.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/cursors.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/cursors.cpython-36.pyc'
import 'pymysql.cursors' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37b5fe208>
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/optionfile.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/optionfile.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/optionfile.cpython-36.pyc'
# /usr/lib64/python3.6/__pycache__/configparser.cpython-36.pyc matches /usr/lib64/python3.6/configparser.py
# code object from '/usr/lib64/python3.6/__pycache__/configparser.cpython-36.pyc'
import 'configparser' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37b171320>
import 'pymysql.optionfile' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37b1710f0>
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/util.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/util.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/util.cpython-36.pyc'
import 'pymysql.util' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37b11d6a0>
# /usr/lib64/python3.6/__pycache__/ssl.cpython-36.pyc matches /usr/lib64/python3.6/ssl.py
<snipped>
import 'getpass' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37b11dc88>
import 'pymysql.connections' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37c55eba8>
import 'pymysql' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37ccc8240>
# /usr/lib64/python3.6/__pycache__/shutil.cpython-36.pyc matches /usr/lib64/python3.6/shutil.py
<snipped>
import 'syslog' # <_frozen_importlib_external.ExtensionFileLoader object at 0x7fc37c58d4e0>
Attempting to connect to DB: SunnyData2
OK, going for it next!
# /usr/lib64/python3.6/encodings/__pycache__/idna.cpython-36.pyc matches /usr/lib64/python3.6/encodings/idna.py
# code object from '/usr/lib64/python3.6/encodings/__pycache__/idna.cpython-36.pyc'
# /usr/lib64/python3.6/__pycache__/stringprep.cpython-36.pyc matches /usr/lib64/python3.6/stringprep.py
# code object from '/usr/lib64/python3.6/__pycache__/stringprep.cpython-36.pyc'
# extension module 'unicodedata' loaded from '/usr/lib64/python3.6/lib-dynload/unicodedata.cpython-36m-x86_64-linux-gnu.so'
# extension module 'unicodedata' executed from '/usr/lib64/python3.6/lib-dynload/unicodedata.cpython-36m-x86_64-linux-gnu.so'
import 'unicodedata' # <_frozen_importlib_external.ExtensionFileLoader object at 0x7fc379d65550>
import 'stringprep' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc379d601d0>
import 'encodings.idna' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37aa4add8>
DB read failed!
None