Python2.7简单连接与操作MySQL的方法

WBOY
Release: 2016-06-10 15:05:06
Original
1478 people have browsed it

本文实例讲述了Python2.7简单连接与操作MySQL的方法。分享给大家供大家参考,具体如下:

Python号称简单优雅,其实新手摆弄一些东西的时候还是挺麻烦的,比如使用Python2.7连接MySQL数据库时,真是有点麻烦。现将方法整理出来。

环境:Python2.7.2、MySQL5.5

1.安装MySQL驱动程序。下载自动安装包,双击安装即可,非常简单。

2.连接MySQL,下面是Python示例代码。

# -*- coding: utf8 -*-
import MySQLdb
conn=MySQLdb.connect(host='localhost',
          user='root',
          passwd='1',
          db='test')
cursor = conn.cursor()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone ()
print "server version:", row[0]
cursor.close()
conn.close()

Copy after login

3.输出:server version: 5.5.21

参考资料:

Writing MySQL Scripts with Python DB-API

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template