连接wⅰfn(呃连接连接)

lxf2023-08-26 05:30:01

Python学习入门(4)-连接MySQL下载MySQL for Python,MySQL-python-1.2.4b4.tar.gz最新版本1)提前安装:mysql_config环境或python setup.py build会提示“环境错误:找不到MySQL _ config”找不到。安装命令如下:sudo apt-ge。

Python学习入门(四)-连接MySQL
下载MySQL for Python,MySQL-python-1.2.4b4.tar.gz最新版本。

1)预先安装:mysql_config环境

否则python setup.py build会提示“环境错误:找不到MySQL _ config”找不到。安装命令如下:

sudo apt-get install libmysql client-dev

2)然后,安装MySQLdb。

$ tar zxvf MySQL-python-1 . 2 . 2 . 2 . tar . gz
$ CD MySQL-python-1 . 2 . 2
$ sudo python setup . py build
$ sudo python setup . py install

3)验证安装是否成功

homer @ Ubuntu:~/my code/Python $ Python
Python 2 . 7 . 3(默认,2012年8月1日05:14:39)
[GCC 4.6.3]在linux2上
键入" help "、" copyright "、" credits "或" license "以获取更多信息。
> > >导入MySQLdb
> >

导入MySQLdb没有错误,说明安装成功!

python连接mysql示例:

#################### # IT-Homer # 2013-05-10 #################### import MySQLdb db = MySQLdb.connect(host="localhost", user="root", passwd="abcd1234", db="testDB") cursor = db.cursor() cursor.execute("Select * from gameTestDB limit 10") result = cursor.fetchall() for row in result: #print row #print row[0], row[1], row[2] #print '%s, %s, %s' % (row[0], row[1], row[2]) print ', '.join([str(row[0]), str(row[1]), str(row[2])]) cursor.close() ''' import sys import MySQLdb reload(sys) sys.setdefaultencoding('utf-8') db = MySQLdb.connect(user='root', passwd='abcd1234', charset='utf8') cur = db.cursor() cur.execute('use testDB') cur.execute('select * from gameTestDB limit 10') f = file("/home/homer/tmp_mysql.txt", 'w') for row in cur.fetchall(): f.write(str(row)) f.write("\n") f.close() cur.close() '''

#################### # IT-Homer # 2013-05-10 #################### import MySQLdb # local mysql # db = MySQLdb.connect(host="localhost", user="root", passwd="abcd1234", db="testDB") # aws rds mysql db = MySQLdb.connect(host="ithomer.aliyun.com", user="ithomer", passwd="abcd1234", db="dman") cursor = db.cursor() cursor.execute("Select * from score limit 10") result = cursor.fetchall() for row in result: #print row #print row[0], row[1], row[2] #print '%s, %s, %s' % (row[0], row[1], row[2]) print ', '.join([str(row[0]), str(row[1]), str(row[2])]) cursor.close() ''' import sys import MySQLdb reload(sys) sys.setdefaultencoding('utf-8') db = MySQLdb.connect(user='root', passwd='abcd1234', charset='utf8') cur = db.cursor() cur.execute('use testDB') cur.execute('select * from gameTestDB limit 10') f = file("/home/homer/tmp_mysql.txt", 'w') for row in cur.fetchall(): f.write(str(row)) f.write("\n") f.close() cur.close()

参考推荐:

Python连接MySQL

MySQLdb用户指南

Python字符串操作

未找到mysql_config(堆栈流)

Python创建了mysql数据库

adminjs.cn是一个以CSS、JavaScript、Vue、HTML为核心的前端开发技术网站。我们致力于为广大前端开发者提供专业、全面、实用的前端开发知识和技术支持。 在本网站中,您可以学习到最新的前端开发技术,了解前端开发的最新趋势和最佳实践。我们提供丰富的教程和案例,让您可以快速掌握前端开发的核心技术和流程。 Adminjs.cn还提供一系列实用的工具和插件,帮助您更加高效地进行前端开发工作。我们提供的工具和插件都经过精心设计和优化,可以帮助您节省时间和精力,提升开发效率。 在Adminjs.cn中,您可以找到您需要的一切前端开发资源,让您成为一名更加优秀的前端开发者。欢迎您加入我们的大家庭,一起探索前端开发的无限可能!