python3使用Django框架連接mysql(python3+Django+MySQL+pymysql)


改掉系統默認的sqlite3數據庫,

# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
'ENGINE': 'django.db.backends.mysql',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'NAME':'djangoDB',
'USER':'root',
'PASSWORD':'123456',
'port':'3306',
}
}

然后啟動報錯,

ImportError: No module named 'MySQLdb'

據說要裝MySQL-python

可是MySQL-python一直無法安裝成功。

又報錯ImportError: No module named 'ConfigParser'

因為python3中叫做configparser,是小寫。

好吧,聽說不用MySQL-python可以用pymsql代替,然后就趕緊下載pip install pymysql

然后再啟動,還是不行啊,還是ImportError: No module named 'MySQLdb'

百度了下,看這篇文章http://www.jianshu.com/p/82781add8449

加了之后就好了。

這里說下MySQL-python吧。

MySQL-python的解釋看官方說法

MySQLdb is an interface to the popular MySQL database server for Python. The design goals are:

  • Compliance with Python database API version 2.0 [PEP-0249]
  • Thread-safety
  • Thread-friendliness (threads will not block each other)

MySQL-3.23 through 5.5 and Python-2.4 through 2.7 are currently supported. Python-3.0 will be supported in a future release. PyPy is supported.

說白了就是連接mysql的python接口,有了他就可以用python來操作數據庫了。類似java的JDBC那一套東西把。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM