本文目錄一覽:
- 1、Python連接mysql資料庫報錯
- 2、安裝MySQL-python,報錯。
- 3、Python2.7安裝mysql-python報錯,求教
- 4、python3.6執行pymysql報錯
- 5、python 連接mysql資料庫報錯
Python連接mysql資料庫報錯
Python連接mysql資料庫報錯
這裡的意思是:資料庫連不上啊。
可能是網路問題,可能是防火牆問題,可能是3306埠沒開。你先排除這些問題吧。用一些mysql工具連接測試看,比如SQLyog 測試。
安裝MySQL-python,報錯。
因為你沒有正確安裝vc的編譯器,也沒有使用vcvars這個批處理進行環境變數設置。
你可以安裝VC後,運行它的VCVARS命令後再執行安裝。
不過建議你別從源代碼安裝mysql-python,直接找個別人做好的安裝包。因為這裡可能還需要引用mysql.h這樣的文件進行編譯,複雜度可能是你承受不了的。
Python2.7安裝mysql-python報錯,求教
# 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:
#
#
# modified by Valentine Gogichashvili as described in
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()
python3.6執行pymysql報錯
折騰好半天的資料庫連接,由於之前未安裝 pip ,而且自己用的Python 版本為3.6. 只能用 pymysql 來連接資料庫,下邊 簡單介紹一下 連接的過程,以及簡單的增刪改查操作。
1.通過 pip 安裝 pymysql
進入 cmd 輸入 pip install pymysql
回車等待安裝完成;
安裝完成後出現如圖相關信息,表示安裝成功。
2.測試連接
import pymysql #導入 pymysql ,如果編譯未出錯,即表示 pymysql 安裝成功
簡單的增刪改查操作
示例表結構
2.1查詢操作
[python] view plain copy
import pymysql #導入 pymysql
#打開資料庫連接
db= pymysql.connect(host=”localhost”,user=”root”,
password=”123456″,db=”test”,port=3307)
# 使用cursor()方法獲取操作游標
cur = db.cursor()
#1.查詢操作
# 編寫sql 查詢語句 user 對應我的表名
sql = “select * from user”
try:
cur.execute(sql) #執行sql語句
results = cur.fetchall() #獲取查詢的所有記錄
print(“id”,”name”,”password”)
#遍歷結果
for row in results :
id = row[0]
name = row[1]
password = row[2]
print(id,name,password)
except Exception as e:
raise e
finally:
db.close() #關閉連接
2.2插入操作
[python] view plain copy
import pymysql
#2.插入操作
db= pymysql.connect(host=”localhost”,user=”root”,
password=”123456″,db=”test”,port=3307)
# 使用cursor()方法獲取操作游標
cur = db.cursor()
sql_insert =”””insert into user(id,username,password) values(4,’liu’,’1234′)”””
try:
cur.execute(sql_insert)
#提交
db.commit()
except Exception as e:
#錯誤回滾
db.rollback()
finally:
db.close()
2.3更新操作
[python] view plain copy
import pymysql
#3.更新操作
db= pymysql.connect(host=”localhost”,user=”root”,
password=”123456″,db=”test”,port=3307)
# 使用cursor()方法獲取操作游標
cur = db.cursor()
sql_update =”update user set username = ‘%s’ where id = %d”
try:
cur.execute(sql_update % (“xiongda”,3)) #像sql語句傳遞參數
#提交
db.commit()
except Exception as e:
#錯誤回滾
db.rollback()
finally:
db.close()
2.4刪除操作
[python] view plain copy
import pymysql
#4.刪除操作
db= pymysql.connect(host=”localhost”,user=”root”,
password=”123456″,db=”test”,port=3307)
# 使用cursor()方法獲取操作游標
cur = db.cursor()
sql_delete =”delete from user where id = %d”
try:
cur.execute(sql_delete % (3)) #像sql語句傳遞參數
#提交
db.commit()
except Exception as e:
#錯誤回滾
db.rollback()
finally:
db.close()
python 連接mysql資料庫報錯
編輯mysql配置文件my.ini(在MySQLServer的安裝目錄),在[mysqld]這個條目下加入 skip-grant-tables
保存退出後重啟mysql
1.點擊「開始」-「運行」(快捷鍵Win+R)。
2.啟動:輸入 net stop mysql
3.停止:輸入 net start mysql
這時候在cmd裡面輸入mysql -u root -p就可以不用密碼登錄了,出現password:的時候直接回車可以進入,不會出現ERROR 1045 (28000),但很多操作都會受限制,因為我們不能grant(沒有許可權)。按下面的流程走(紅色部分為輸入部分,綠色的是執行後顯示的代碼不用輸入):
1.進入mysql資料庫:
mysql use mysql; Database changed
2.給root用戶設置新密碼,藍色部分自己輸入: mysql update user set password=password(“新密碼”) where user=”root”; Query OK, 1 rows affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0
3.刷新資料庫 mysql flush privileges; Query OK, 0 rows affected (0.00 sec)
4.退出mysql: mysql quit Bye
改好之後,再修改一下my.ini這個文件,把我們剛才加入的”skip-grant-tables”這行刪除,保存退出再重啟mysql就可以了。
總結:猜測根本原因就是簡單的密碼輸入錯誤,通過以上方法我們可以不驗證密碼來連接上mysql,然後設置新密碼。
原創文章,作者:BRJB,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/148241.html