mysql python2.7_python2.7中使用mysql (windows XP)

一.首先下载mysql—python模块,下载完毕之后会有一个MySQL-python-1.2.3.win32-py2.7.exe文件,点击安装一路next,ok。

二.编辑配置文件(setting.py)

DATABASES = {

'default': {

'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.

'NAME': 'blog', # Or path to database file if using sqlite3.

# The following settings are not used with sqlite3:

'USER': 'root',

'PASSWORD': '',

'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.

'PORT': '', # Set to empty string for default.

}

}

注意:我的一个数据库叫名叫blog

三.django连接

1.在cmd命令框中进入project目录,输入 python manage.py shell

2.输入: >>>from django.db import connection

>>>cursor=connection.cursor()

如果什么问题都没有,说明配置正确 。(别忘了开启你的mysql服务)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Windows使用 Python 3.8 连接 MySQL 数据库,可以使用 PythonMySQL 连接器,如 mysql-connector-python 或 PyMySQL。这里以 mysql-connector-python 为例,介绍连接 MySQL 数据库的步骤。 1. 安装 mysql-connector-python 可以使用 pip 命令安装 mysql-connector-python: ``` pip install mysql-connector-python ``` 2. 导入 MySQL 连接器 在 Python 脚本导入 mysql.connector 模块: ``` import mysql.connector ``` 3. 连接 MySQL 数据库 创建一个 MySQL 连接对象,使用 mysql.connector.connect() 方法连接 MySQL 数据库。需要指定 MySQL 数据库的主机名、用户名、密码和数据库名: ``` mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) ``` 4. 执行 SQL 查询 使用 MySQL 连接对象的 cursor() 方法创建游标对象,使用游标对象的 execute() 方法执行 SQL 查询: ``` mycursor = mydb.cursor() mycursor.execute("SELECT * FROM customers") myresult = mycursor.fetchall() for x in myresult: print(x) ``` 完整的代码示例: ``` import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() mycursor.execute("SELECT * FROM customers") myresult = mycursor.fetchall() for x in myresult: print(x) ``` 注意:在连接 MySQL 数据库时,需要替换 host、user、password 和 database 的值为实际的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值