Django various database connection

By ukmodak | August 12th 2022 07:07:45 PM | viewed 165 times

For mysql

by default python connect sqlite . if you want to change then go to in our main projects settings.py. and run the following command

pip install mysqlclient

settings.py =>

DATABASES = {
	'default': {
		'ENGINE': 'django.db.backends.mysql',
		'NAME': 'dataflair',
		'USER': 'root',
		'PASSWORD': “”,
		'HOST': “”,
		'PORT': “”,
		'OPTIONS': {
		    'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
		}
	}
}

For oracle run the command on oracle database

GRANT EXECUTE ON SYS.DBMS_LOB TO user;
GRANT EXECUTE ON SYS.DBMS_RANDOM TO user;

Run the following command on django project

pip install cx_Oracle

settings.py =>

DATABASES = {
	'default': {
		'ENGINE': 'django.db.backends.oracle',
		'NAME': 'XE',
		'USER': 'System',
		'PASSWORD': 'Tiger',
		'HOST': 'Scott',
		'PORT': '1521',
	}
}

If you want to see the SQL statements required to implement the migrations that we have stored, then please execute the sqlmigrate command, as shown below.

python manage.py sqlmigrate customer 0001

to nserting the data into database

python manage.py shell

E:\Django Project2\django_db2>python manage.py shell
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD6
4)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from customer.models  import Customer_Info
>>> cust1 = Customer_Info(name='Shawn', address='271 N Monroe St', city='Tallaha
ssee', state='Florida', country='USA')
>>> cust1.save()
>>> customer_info_list = Customer_Info.objects.all()
>>> customer_info_list
]>
>>>
bONEandALL
Visitor

Total : 18980

Today :9

Today Visit Country :

  • Germany
  • Singapore
  • United States
  • Russia