连接数据库(一)

首先要安装模块:MySQLdb

参考网址:https://jingyan.baidu.com/album/5d368d1ea2531f3f61c0577c.html?picindex=1

问题:

import MySQLdb
报错:Traceback (most recent call last):
File "", line 1, in
import MySQLdb
ModuleNotFoundError: No module named 'MySQLdb'

cmd里面执行python
报错:'python' 不是内部或外部命令,也不是可运行的程序
解决:在高级系统设置>环境变量>Path>编辑加入;C:\Users\ning\AppData\Local\Programs\Python\python37

又遇到问题

C:\>python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
  File "", line 1, in 
ModuleNotFoundError: No module named 'MySQLdb'
>>>

还是没这个模块
参考网址:https://jingyan.baidu.com/album/f3e34a12d869d2f5ea65357c.html?picindex=1
下载了python_mysql

问题:

安装python_mysql.exe时:软件安装时 找不到注册表
然后,给出了一大堆代码,注册表,好乱,换行空格怎么都是乱的,弄了好久,还是错的,又是一场失败的革命。

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!"

然后编写代码连接数库
最后就可以编写插入数据库的代码

你可能感兴趣的:(连接数据库(一))