当前位置:主页 > 软件编程 > Python代码 >

win7上python2.7连接mysql数据库的方法

时间:2021-05-24 08:48:50 | 栏目:Python代码 | 点击:

 一:安装MySQL-python驱动

 pip install mysql

这里写图片描述

二:连接到MySQL服务器的test数据库

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import mysql.connector
if __name__ == "__main__":
   # 打开数据库连接
  conn=mysql.connector.connect(host='IP',
       user='root',
       passwd='1234',
       db='dbName')

  # 使用cursor()方法获取操作游标
  cursor=conn.cursor()

  # 使用execute方法执行SQL语句
  cursor.execute('select * from tset where 1=1') #表查询

  # 使用 fetchone() 方法获取一条数据库。
  values=cursor.fetchall()

  print(values)

  # 关闭数据库连接
  cursor.close()

这里写图片描述

您可能感兴趣的文章:

相关文章