1.Ubuntu下给python安装mysql驱动,方法如下:
zdd@ubuntu:~/djcode/mysite$ python manage.py shell
在终端中输入:sudo apt-get install python-mysqldb
测试一下
zdd@ubuntu:~/djcode/mysite$ python
Python 2.7.3 (default, Feb 27 2014, 20:00:17)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> conn=MySQLdb.connect(host="localhost",user="zdd",passwd="zdd",db="test",charset="utf8")
>>> cursor=conn.cursor()
>>> n =cursor.execute("select * from student")
>>> for row in cursor.fetchall():
... for r in row:
... print r
...
5
5
alen
1
56
6
6
Aer
2
67
7
7
emiyt
1
87
8
8
allen
1
56
9
9
n
1
56
10
10
name10
1
89
>>> cursor.close()
>>> conn.close()
2.django配置mysql
在settings.py中配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'USER': 'zdd',
'PASSWORD': 'zdd',
'HOST': 'localhost',
'PORT': '3306',
}
}
测试一下
zdd@ubuntu:~/djcode/mysite$ python
Python 2.7.3 (default, Feb 27 2014, 20:00:17)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> conn=MySQLdb.connect(host="localhost",user="zdd",passwd="zdd",db="test",charset="utf8")
>>> cursor=conn.cursor()
>>> n =cursor.execute("select * from student")
>>> for row in cursor.fetchall():
... for r in row:
... print r
...
5
5
alen
1
56
6
6
Aer
2
67
7
7
emiyt
1
87
8
8
allen
1
56
9
9
n
1
56
10
10
name10
1
89
>>> cursor.close()
>>> conn.close()
2.django配置mysql
在settings.py中配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'USER': 'zdd',
'PASSWORD': 'zdd',
'HOST': 'localhost',
'PORT': '3306',
}
}