Django / Python 链接MySQL数据库
https://www.cnblogs.com/wupeiqi/articles/5237704.html
python (Django)中使用MySQL
首先python3中没有 MySQLdb
需要安装模块 pymysql
执行下面的代码:
import pymysql pymysql.install_as_MySQLdb()
然后在Django的Setting中设置DATABASE:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME':'数据库名称', 'USER': 'root', 'PASSWORD': '******', 'HOST': '主机号', 'PORT': '端口(默认3306)', } }
通过上面的代码就可以完成Django中的数据库的链接
下面介绍Python直接使用MySQL数据库:
__author__ = "WSX" import pymysql conn = pymysql.connect(host = "localhost" , port = 3306 , user = "root" , passwd = "******" , db = "wsxdb",charset = "utf8") cursor = conn.cursor() user_data = cursor.execute("select * from login_user") #查找 row1 = cursor.fetchall() #插入数据 data = [ ("z","123"), ("l","123"), ("w","123"), ] cursor.executemany("insert into login_user(name , password) values ( %s, %s)",data) conn.commit() print(user_data) print(row1)