使用django自带的connection
作者:互联网
from django.conf import settings import django import pymysql pymysql.install_as_MySQLdb() INSTALLED_APPS = [] DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'xxx', 'USER': 'root', 'PASSWORD': 'root', 'HOST': '127.0.0.1', 'PORT': '3306', } } settings.configure(DATABASES=DATABASES, INSTALLED_APPS=INSTALLED_APPS) django.setup()
在外部进行 import 该文件名称 如 django_connect
from django.db import connection
import django_connect from django.db import connection with connection.cursor() as cursor: sql = """ select * from base_indexinfo """ cursor.execute(sql) columns = [col[0] for col in cursor.description] datas = [dict(zip(columns, row)) for row in cursor.fetchall()] print(datas)
2022-03-25
标签:APPS,INSTALLED,cursor,connection,import,自带,django 来源: https://www.cnblogs.com/hany-postq473111315/p/16054124.html