Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python FAQ method of django connecting to mysql database

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report