网络编程
位置:首页>> 网络编程>> 数据库>> win7上python2.7连接mysql数据库的方法

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

作者:silentwolfyh  发布时间:2024-01-25 18:28:25 

标签:python,mysql

 一:安装MySQL-python驱动


pip install mysql

win7上python2.7连接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()

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

来源:http://blog.csdn.net/silentwolfyh/article/details/54407951

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com