å¨ç¨Djangoæ¶ï¼æ°æ®åºå¦æéæ©MySQLï¼é£ä¹éè¦å®è£
MySQL-Pythonï¼ä»¥ä¸æ¯ç®æå®è£
æç¨ï¼
1. å°ä¸è½½å®è£
å
ï¼æªè³ç¬è
å稿ï¼MySQL-pythonççæ¬ä¸ºï¼MySQL-python-1.2.4b4.tar.gzã
2. 解åï¼ç¬è
解åå°äºï½/tempç®å½ä¸ï¼
[plain] view plain copy
$ tar -zxvf MySQL-python-1.2.4b4.tar.gz -C ~/temp/
3. å¨ç»ç«¯éé¢æ§è¡ä»¥ä¸å½ä»¤ï¼
[plain] view plain copy
$ mysql_config
å¦ææ示æªæ¾å°å½ä»¤ï¼åå®è£
ç»ç«¯æç¤ºä½ çå
ï¼å¦ä¸ï¼
åå®è£
ï¼
[plain] view plain copy
$ sudo apt-get install libmysqlclient-dev
4. æ们æå¼MySQL-python-1.2.4b4ç®å½ï¼æ¥éª¤2ä¸è§£åå¾å°çç®å½ï¼ä¸çsite.cfgæ件ï¼çå°æè¿æ ·ç说æï¼
ææå°±æ¯è¯´ï¼å¦æmysql_configèæ¬ä¸å¨PATHä¸ï¼æ们就解注éä¸é¢ç代ç ï¼å¹¶ä¿®æ¹è¿è¡ä»£ç åé¢çè·¯å¾ï¼ä»¥åç¥mysql_configèæ¬çä½ç½®ï¼å¦æmysql_configè¿ä¸ªèæ¬å¨PATHä¸ï¼å°±ä¸è¦æ¹äºï¼å¦åä¼åºç°ä¸äºåªå¤·ææçç°è±¡ãéµæ¨ï¼
5. 为äºç¥ææ们å°åºç¨ä¸ç¨æ¹site.cfgï¼å次å°è¯å¨ç»ç«¯æ§è¡ï¼
[plain] view plain copy
$ mysql_config
å¦æåºç°ä»¥ä¸ç»æï¼
å说æmysql_configèæ¬å¨PATHä¸ï¼æè
说å¨MySQL-pythonè¿ç¨ä¸æå®è½æ¾å°mysql_configèæ¬ï¼ï¼é£ä¹æ们就ä¸ç¨ä¿®æ¹site.cfgï¼
å¦ææªåºç°ï¼åå°±æçæ¥éª¤4æè¿°ä¿®æ¹site.cfgï¼å¯ä»¥å¨ç»ç«¯æ§è¡å¦ä¸å½ä»¤ï¼æ¥å¯»æ¾mysql_configè¿ä¸ªèæ¬çè·¯å¾ï¼
[plain] view plain copy
$ find / -name mysql_config
å¾å°mysql_configçè·¯å¾ï¼åºè¯¥å¾å¿«ï¼findå½ä»¤æ®æ¨æµåºè¯¥ä¼å
å¨PATHåéä¸çè·¯å¾å¯»æ¾ï¼åºæ¥ç¬¬ä¸æ¡ç»æåéæ¶ç»æ¢æ§è¡å³å¯ï¼ï¼ä¿®æ¹site.cfgã
6. è¥MySQLæ¯éè¿apt-getå®è£
çï¼é£ä¹æ们è¿è¦å®è£
python-devå
ï¼æ¥æä¾å®è£
è¿ç¨ä¸å¿
é¡»çPython.hè¿ä¸ªå¤´æ件ï¼
[plain] view plain copy
$ sudo apt-get install python-dev
7. åå¤å·¥ä½ç»äºåå®äºï¼ç°å¨èµ¶ç´§å»è§£åå¾å°çæ件夹MySQL-python-1.2.4b4ä¸ï¼è¿è¡ï¼
[plain] view plain copy
$ sudo python setup.py build
$ sudo python setup.py install
8. æµè¯ä¸ä¸ï¼å¨pythonå½ä»¤è¡ä¸æ§è¡ï¼
[plain] view plain copy
import MySQLdb
å¦æ没åºç°é误ï¼è¯´ææ们æåå®è£
äºMySQL-pythonã
éï¼Djangoä¸éªè¯æä¸é¡¹ç®æåè¿æ¥MySQLçæ¹æ³ï¼
æ ·ä¾settings.DATABASESï¼
[python] view plain copy
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'db_name', # Or path to database file if using sqlite3.
'USER': 'your_MySQL_uname', # Not used with sqlite3.
'PASSWORD': 'your_pwd', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
å¨é¡¹ç®ç®å½éè¿ä»¥ä¸å½ä»¤å¯å¨Python Shellï¼
[plain] view plain copy
$ python manage.py shell
注ï¼ä»¥ä¸å½ä»¤æ¯ä»¥æ£ç¡®é
ç½®å¯å¨Shellçä¸ç§æ¹æ³ã
æ¥çå¨Shellä¸è¾å
¥ä¸åå½ä»¤æµè¯MySQLè¿æ¥ï¼
[python] view plain copy
>>>from django.db import connection
>>>c = connection.cursor()
å¦æ没ææ¾ç¤ºä»ä¹é误信æ¯ï¼é£ä¹ä½ çæ°æ®åºé
ç½®æ¯æ£ç¡®çã
温馨提示:内容为网友见解,仅供参考