Python mysql xampp - python

i installed xampp server. I want to access MySQL database through python. I tried to install MySQL-python package. It needs visual studio which I don't want to install.
I tried through MySQL connectors, but it is not allowing to do so.
How can I access MySQL database through python?

You can try like this,
db=MySQLdb.connect(user="user_name",passwd="password",db="database_name",unix_socket="mysql.sock file path")
OR try running, python manage.py runserver
OR once check your default setting of database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'database_name', # Or path to database file if using sqlite3.
'USER': 'user_name', # Not used with sqlite3.
'PASSWORD': 'password', # Not used with sqlite3.
'HOST': '127.0.0.1', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '3306' # Set to empty string for default. Not used with sqlite3.
}
}

Related

Django error when using mysql-connector-python

I want to set the mysql database using mysql-connector-python for setting up a web site made with Django using a database backend but I get this error:
django.core.exceptions.ImproperlyConfigured: 'mysql.connector.django' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
My credentials (name, user, password, host, port) works fine, so the problem isn't here.
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'library',
'USER': 'username',
'PASSWORD': 'password',
'HOST': '127.0.0.1',
'PORT': '3306',
'OPTIONS': {
'autocommit': True,
'use_pure': True,
},
}
}
I'm using python 3.8.5 and mysql-connector-python 8.0.21 and I understand that mysql-connector-python, starting from version 8.0.13, has a bug, which makes it impossible to use with Django and to avoid the bug I added ‘use_pure’: True in database options. But it doesn't work.
I found this question but it doesn't say anything about using ‘use_pure’: True.
If the mysql-connector-python isn't compatible with python 3, what can I use instead? (I can't wait for the support release).
you do not have the option to use mysql connector, you have to install mysql-client from https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient and try installing all the wheels incase you get errors, while installing using pip install

Issue with Connecting Django to Postgresql database

When I try to connect my app to postgresql db I always get this same error:
django.core.exceptions.ImproperlyConfigured: 'django.db.backends.postgresql' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'sqlite3'
Here is my code in settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydb',
'USER': 'postgres',
'PASSWORD': 'pass123',
'HOST': 'localhost',
'PORT': '5432'
}
}
I am using django version 3.0.2 and my psycopg2 version 2.8.5 . I did downgrade to earlier version of django and still the problem persists. Also my python version is 3.8.
I tried doing this too
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USER': 'postgres',
'PASSWORD': 'pass123',
'HOST': 'localhost',
'PORT': '5432'
}
}
I thought it was the issue with the virtual env so I decided to perform on other projects too but still does not work. Can anyone please help me.
I fixed it by uninstalling the psycopg2. I installed psycopg2-binary.
pip install psycopg2-binary

How to configure MySQL with Django

Am trying to use MySQL with Django 2.0, I have installed it using "pip install mysqlclient" and it installed successfully. What do i need to do next?
In settings.py you'll need:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name',
'USER': 'user_name',
'PASSWORD': 'user_password',
'HOST': 'localhost',
'PORT': '3306',
}
You'll need to create the db and user and grant permissions within MySQL.
You'll also need to ensure you have MySQL setup and running.

How to connect MySQL Database in django 1.10.6

import MySQLDB as Database
File "c:\pythonprojects\env\lib\site-packages\mysql_python-1.2.2-py3.6-win32.egg\mysqldb__init__py", line 22
raise ImportError, "This is MySQL Version %s, But _mysql is version %r"
SyntaxError: invalid syntax
Make sure you have pymysql installed. Then in settings.py do this
try:
import pymysql
pymysql.install_as_MySQLdb()
except:
pass
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db-name',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
i) First you have to install the mysql server in your machine, if you already have mysql server up and running then jump to next step.
ii) Create a user in mysql (you can use the root user, but I would suggest create a separate one) you can refer mysql documentation for creating a new user. Let, you have created a user with:- username = testuser , password = testpass#123
iii) Create a database in mysql , let named it testDB
iv) Now you have to install mysqlclient-python, to install it from PyPI you can do so by typing pip install mysqlclient (by default pip the python package manager points python2 series if you are setting up your django project on python3 then you have to use pip3) for more information on how to install mysqlclient-python on your particular OS you can refer this link: https://github.com/PyMySQL/mysqlclient-python
v) Now In settings.py under key DATABASES make these changes:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # for mysql engine
'NAME': 'testDB',
'USER': 'testuser',
'PASSWORD': 'testpass#123',
'HOST': 'localhost', # if you want to run your project on your localmachine
}
}
vi) Now change your working directory to your django project directory: " cd yourDjangoProject" and run these django commands
python manage.py makemigrations
python manage.py migrate
Now I hope that above django commands will run successfully and you would be able to use to your mysql database in your django project.

connect mysql database with python

i am very new to python Django. i need to connect the MySQL database with python, i really don't have any idea about that ..
i am using python 1.7.
tell me how to connect MySQL database with python. what settings i need to fill in settings.py.
i have goggled it but i am not getting a proper answer please tell me from the beaning how to achieve it.
i have run this command now what are the next steps ?
sudo apt-get install python-mysqldb
i really don't have any idea about the further steps, please guide me
Well you just installed MySQL Database interface for python
Make sure to run pip freeze and see MySQL-python==1.2.5 is there or not .)
And include below snippet in your settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'yourdatabasename',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'localhost',
'PORT': '',
}
}

Categories