1. 安装pymysql 库
pip install pymysql
2.实例本地连接mysql库
#!/usr/bin/python # encoding: utf-8 """ @version: v1.0 @author: dabao @license: Apache Licence @contact: robbile@163.com @site: http://www.cnblogs.com/EWWE/ @software: PyCharm Community Edition @file: 1225.2.mysql.py @time: 2017/12/25 22:07 """ import pymysql try: db = pymysql.connect("127.0.0.1", "root", "123456") cursor = db.cursor()#数据游标 cursor.execute("show databases")#查询所有数据库 cursor.execute("use world")#使用world数据库 cursor.execute("show tables")#查询所有表 cursor.execute("show columns from city")#查询表信息 cursor.execute("select * from city where id<10")#查询city表中ID小于10的数据 date = cursor.fetchall()#查询结果 print date#打印查询结果 db.close() print "登陆成功" except: print "登陆失败"