Get the App
SLTechnology News&Howtos  ›  Database  › 

Python FAQ method of django connecting to mysql database

Shulou Source: shulou.com Published: 2022-06-01 19:27:20 10月30日 Update

This article mainly introduces the python FAQ django connection mysql database method, the content of the article is carefully selected and edited by the author, with a certain pertinence, for everyone's reference significance is still relatively great, the following with the author to understand the python FAQ django connection mysql database method topic content.

Python connects to the mysql database:

First, download the mysql driver, I use pycham IDEA. Because I found a lot of people who directly use commands on the Internet. But it's not for me.

Configure DATABASES

DATABASES = {'default': {' ENGINE': 'django.db.backends.mysql',' NAME': 'test',' USER': 'root',' PASSWORD':', 'HOST':' 127.0.0.1, 'PORT':' 3306,}}

MysqlDB needs to be referenced. For example, a normal import MysqlDB will report an error.

All need to be in _ _ init__py. Add

Import pymysqlpymysql.install_as_MySQLdb ()

In this way, you can connect to django and connect to mysql.

We have to run a test.

Cursor = connection.cursor () # use the execute method to execute the SQL statement cursor.execute ("SELECT * from sys_user") # use the fetchone () method to get a database. Data = cursor.fetchone () # close database connection connection.close () u_content_dic = {'username':data [0],' password':data [1]} # # pass a dictionary as Content_typecursor = connection.cursor () return render (request, "html/firstWeb.html", {'data': json.dumps (u_content_dic),})

This is the method I wrote at that time, you can refer to it.

Html received as:

Var data = {{data | safe}}

Data is the object passed from the background.

After reading the above about python FAQ django connection mysql database method, many readers must have some understanding, if you need to get more industry knowledge and information, you can continue to follow our industry information column.

Tags: Data database method FAQ FAQ content author industry reference topic information background command dictionary object meaning article more friends Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info Apple vpn Shulou Technology