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程序设计有所帮助。

相关文章

wxPython学习之主框架实例

wxPython学习之主框架实例

本文实例讲述了wxPython主框架的简单用法,分享给大家供大家参考。具体如下: 程序代码如下: import wx class MyApp(wx.App): def O...

Python计算时间间隔(精确到微妙)的代码实例

Python计算时间间隔(精确到微妙)的代码实例

使用python中的datetime import datetime oldtime=datetime.datetime.now() print oldtime; x=1 while...

python判断计算机是否有网络连接的实例

先安装第三方库:pip install requests def isConnected(): import requests try: html = request...

Python、Javascript中的闭包比较

同为脚本语言,python和Javascript具有相似的变量作用域,不像php,函数的内部的所有变量和外部都是隔绝的,也就是说,函数要想处理其外部的数据,必须使用参数把需要处理的数据传...

django框架基于模板 生成 excel(xls) 文件操作示例

本文实例讲述了django框架基于模板 生成 excel(xls) 文件操作。分享给大家供大家参考,具体如下: 生成Excel 文件,很多人会采用一些开源的库来实现,比如python 自...