Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

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

$
0
0
 一:安装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数据库的方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。


Viewing all articles
Browse latest Browse all 9596

Trending Articles