Reputation: 445
I'm planning to use Django framework for one of my projects. I've installed Django, watched some tutorials and now I'm trying to create a web server which will use a MySql database.
To begin with, I have a made a database with phpMyAdmin from xampp and now I want to use the data stored in that database and just to print some entries from that database.
On the tutorial, it was used sqlite.
Upvotes: 1
Views: 981
Reputation: 13993
First you need to install MySql for python using pip
which can be found here. After installing pip
say pip install MySQL-python
. Hopefully it should get you started with MySql. But you might also want to use Mysql python connector which can be found here.Be aware of the Python version you are using then download the connector.You might also want to install through which can be found here which will install with all pre-requisites like Python connector and server,etc..
you need to change DATABASES
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb',
'USER': 'root',
'PASSWORD': 'password',
'HOST': '127.0.0.1', # Use this if Database exists in your PC or replace it by IP address of PC where you are fetching the Database
'PORT': '3306',
},
'mydb': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb',
'USER': 'root',
'PASSWORD': 'password',
'HOST': '127.0.0.1', # Use this if Database exists in your PC or replace it by IP address of PC where you are fetching the Database
'PORT': '3306',
}
}
after which you should be able to access the MySql database.
Also import MySql in your database file as import MySQLdb
Upvotes: 1
Reputation: 8515
You can use MySQL as your database. In order to do this, you need to change your settings.py file with appropriate settings for MySQL:
ENGINE=django.db.backends.mysql
NAME='schema-name'
USER='username'
PASSWORD='password'
HOST='localhost'
PORT='3306'
With these settings, your django application should be able to talk to MySQL database. You can also verify by running python manage.py syncdb
command from the command prompt in your project folder. Further information regarding the databases can be found at https://docs.djangoproject.com/en/1.7/ref/settings/#std:setting-DATABASES
Upvotes: 1
Reputation: 2454
If you're using python3, I found that the best option is use pymysql
importing it in the settings file like so
import pymysql
pymysql.install_as_MySQLdb()
with django.db.backends.mysql
set as the ENGINE
.
I would also advise against using a pre-populated database: Django is very good at building tables, and I'm sure you read about makemigrations
and migrate
.
If you want to manually populate the database, you could use a small script or hop in the shell to do that before fetching and presenting data.
Upvotes: 1