python -- MySQLdb连接mysql数据库

import MySQLdb

# 打开数据库连接
db = MySQLdb.connect(host="vpca-1.vm.elenet.me",port=9707, user="vpca_user",passwd="123456",db="vpca_db")

# 使用cursor()方法获取操作游标
cursor = db.cursor()

# 使用execute方法执行SQL语句
cursor.execute("SELECT * FROM tb_name limit 1;")

# 使用 fetchone() 方法获取一条数据库。
data = cursor.fetchone()

print data

# 关闭数据库连接
db.close()

时间: 2024-08-04 23:24:55

python -- MySQLdb连接mysql数据库的相关文章

python MySQLdb连接mysql失败(转载)

最近了解了一下django,数据库选用了mysql, 在连接数据库的过程中,遇到一点小问题,在这里记录一下,希望能够对遇到同样的问题的朋友有所帮助,少走一些弯路.关于django,想在这里也额外说一句.django是很优秀的基于python的web开发框架,对于有python基础的后台程序员,如果有要做一些前台的需求,强烈推荐django.下面言归正传. 1. 问题的现象 下面是连接数据库的代码,用的是python的MySQLdb模块: 1 2 3 4 5 db = MySQLdb.connec

python MySQLdb连接mysql失败

Traceback (most recent call last):   File "./test_db.py", line 12, in < module>     db='mysite')   File "build/bdist.linux-x86_64/egg/MySQLdb/__init__.py", line 81, in Connect   File "build/bdist.linux-x86_64/egg/MySQLdb/con

Mysqldb连接Mysql数据库(转)

python操作mysql数据库 Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库: GadFly mSQL MySQL PostgreSQL Microsoft SQL Server 2000 Informix Interbase Oracle Sybase 你可以访问Python数据库接口及API查看详细的支持数据库列表. 不同的数据库你需要下载

python 简单连接mysql数据库

1. 安装pymysql 库 pip install pymysql 2.实例本地连接mysql库 #!/usr/bin/python # encoding: utf-8 """ @version: v1.0 @author: dabao @license: Apache Licence @contact: [email protected] @site: http://www.cnblogs.com/EWWE/ @software: PyCharm Community Ed

python MySQLdb连接mysql时报错

故障现象: >>> import MySQLdb >>> conn = MySQLdb.connect(host="localhost",user="root",passwd="tiange1003",db="tian",port=3306,charset="utf8") Traceback (most recent call last): File "<

python第一次连接mysql数据库

文件结构图 源码 jing_dong.py 1 # coding:utf-8 2 from mysql_connect import MySql 3 4 5 class JD(object): 6 def __init__(self): 7 self.mysql = MySql('localhost', 3306, 'root', '', 'ksoa_tdyy_test', 'utf8') 8 9 def show_items_info(self): 10 """查询商品信息

python3.4连接mysql数据库的方法

python3.4连接mysql数据库的方法 发布时间:2014-08-04编辑:www.jbxue.com 本文介绍了python3.4连接mysql数据库的方法,在python3.4中不能用mysqldb连接mysql,可以使用pymysql完成连接mysql数据库,需要的朋友参考下. 在python3.4中用原来python2.7的mysqldb已不能连接mysql数据库了. 喜欢mysqldb的朋友,可以参考: Python实例 mysqldb操作数据库 python MySQLdb操作

Python连接Mysql数据库(Debian)

Python连接Mysql数据库(Debian) 以下是Python 2.*版本的安装方法,MySQL-python暂不支持Python 3.*版本 提前要做的工作: 安装setuptools,在终端中运行 wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python 安装pip,下载“get_pip.py”,运行 python get_pip.py 运行如下命令装好必要的包 sudo apt-get install python-d

[笔记]--在Ubuntu系统用Python连接Mysql数据库

环境:Ubuntu11.10,Python2.7,Mysql5.0.95 在Ubuntu终端输入命令安装Python的Mysql模块 sudo apt-get install python-mysqldb 就这么简单: 运行一下脚本: #!/usr/bin/python #-*-coding=utf-8# # import MySQLdb cn = MySQLdb.Connection(host="192.168.88.124",user="root",passwd