python连接MySQL数据库实例分析

yipeiwu_com6年前Python基础

本文实例讲述了python连接MySQL数据库的方法。分享给大家供大家参考。具体实现方法如下:

import MySQLdb
conn = MySQLdb.connect(host="localhost", 
            user="root", 
            passwd="123456", 
            db="test") 
cursor = conn.cursor() 
cursor.execute("select * from hard") 
res = cursor.fetchall() 
for x in res:
 print x 
cursor.close() 
conn.close()

运行结果如下:

希望本文所述对大家的python程序设计有所帮助。

相关文章

Python模块_PyLibTiff读取tif文件的实例

Usage example (libtiff wrapper) from libtiff import TIFF # to open a tiff file for reading:...

在Python中实现shuffle给列表洗牌

在Python中实现shuffle给列表洗牌

如下所示: # Copyright (c)2018, 东北大学软件学院学生 # All rightsreserved # 文件名称:a.py # 作 者:孔云 #问...

python将pandas datarame保存为txt文件的实例

CSV means Comma Separated Values. It is plain text (ansi). The CSV ("Comma Separated Value")...

PyCharm 2019.3发布增加了新功能一览

PyCharm 2019.3发布增加了新功能一览

Python的IDE(Integrated Development Environment 集成开发环境)非常多,如:VS Code、Sublime、NotePad、Python自带编辑...

python中反射用法实例

本文实例讲述了python中反射用法。分享给大家供大家参考。具体如下: import sys, types,new def _get_mod(modulePath): try:...