Python: writing TXT files to MySQL
Current environment:
Windwos 10Python 2.7MySQL 5.5PyCharm
Problems encountered
ImportError: No module named MySQLdb
Install MySQLdb, note that it is under the windows command line, not under the python environment pip install MySQLdberror: Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27
Visual C++ 9.0 was reported missing when installing MySQLdb. Solution: download and install MySQL-python 1.2.5. Https://pypi.python.org/pypi/MySQL-python/1.2.5
Note: for 64-bit systems, please install 64-bit ones, otherwise an error will be reported
Error installing MySQL-python Times: Python version 2.7required, which was not found in the registry
Reason: this is because python2.7 is not recognized in the registry, because windows is 64-bit and installed python is 32-bit
Solution:
1. Create a new register.py file in any drive folder and copy the following code into it: # # script to register Python 2.0 or later for use with win32all# and other extensions that require Python registry settings## written by Joakim Loew for Secret Labs AB / PythonWare## source:# http://www.pythonware.com/products/works/articles/regpy20.htm## modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html
Import sys
From _ winreg import *
Tweak as necessary
Version = sys.version [: 3]
Installpath = sys.prefix
Regpath = "SOFTWARE\ Python\ Pythoncore\% s\"% (version)
Installkey = "InstallPath"
Pythonkey = "PythonPath"
Pythonpath = "% s% s\ Lib\;% s\ DLLs\"% (
Installpath, installpath, installpath
)
Def RegisterPy ():
Try:
Reg = OpenKey (HKEY_CURRENT_USER, regpath)
Except EnvironmentError as e:
Try:
Reg = CreateKey (HKEY_CURRENT_USER, regpath)
SetValue (reg, installkey, REG_SZ, installpath)
SetValue (reg, pythonkey, REG_SZ, pythonpath)
CloseKey (reg)
Except:
Print "Unable to register!"
Return
Print "- Python", version, "is now registered!"
Return
If (QueryValue (reg, installkey) = = installpath and
QueryValue (reg, pythonkey) = = pythonpath):
CloseKey (reg)
Print "= = Python", version, "is already registered!"
Return
CloseKey (reg)
Print "Unable to register!"
Print "* You probably have another Python installation!"
If name = = "main":
RegisterPy ()
two。 Navigate to the directory where the file is located and run python register.py. Prompt Python 2.7is now registered! It means success. Just continue to execute MySQL-python.