mysqldb

Python MySQLdb: connection.close() VS. cursor.close()

匿名 (未验证) 提交于 2019-12-03 01:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: If I use MySQLdb to connect to MySQL-Server through python. I create a connection and a cursor like this connection = MySQLdb.connect(...) cursor = connection.cursor() # process When the MySQL-processing is done one should close the connection. Now I was wondering: Is it sufficient to close the connection bydoing connection.close() or do I have to close the cursor first and then the connection? Like this: cursor.close() connection.close() 回答1: Think less. Use tools more. from contextlib import closing with closing( connection.cursor() ) as

Python: use mysqldb to import a MySQL table as a dictionary?

匿名 (未验证) 提交于 2019-12-03 01:17:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Anyone know how I can use mysqldb to turn a MySQL table, with lots of rows, into a list of dictionary objects in Python? I mean turning a set of MySQL rows, with columns 'a', 'b', and 'c', into a Python object that that looks like this: data = [ { 'a':'A', 'b':(2, 4), 'c':3.0 }, { 'a':'Q', 'b':(1, 4), 'c':5.0 }, { 'a':'T', 'b':(2, 8), 'c':6.1 } ] Thanks :) 回答1: MySQLdb has a separate cursor class for this, the DictCursor. You can pass the cursor class you want to use to MySQLdb.connect(): import MySQLdb.cursors MySQLdb.connect(host='...',

Python MySQLdb TypeError: not all arguments converted during string formatting

匿名 (未验证) 提交于 2019-12-03 01:16:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Upon running this script: #! /usr/bin/env python import MySQLdb as mdb import sys class Test: def check(self, search): try: con = mdb.connect('localhost', 'root', 'password', 'recordsdb'); cur = con.cursor() cur.execute( "SELECT * FROM records WHERE email LIKE '%s'", search ) ver = cur.fetchone() print "Output : %s " % ver except mdb.Error, e: print "Error %d: %s" % (e.args[0],e.args[1]) sys.exit(1) finally: if con: con.close() test = Test() test.check("test") I get an error of: ./lookup Traceback (most recent call last): File "./lookup",

ImportError: No module named MySQLdb

匿名 (未验证) 提交于 2019-12-03 01:13:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am referring the following tutorial to make a login page for my web application. http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982 I am having issue with the database. I am getting an ImportError: No module named MySQLdb when I execute http://127.0.0.1:5000/testdb I have tried all possible ways to install python mysql, the one mentioned in the tutorial, easy_install, sudo apt-get install. I have installed mysql in my virtual env. My directory structure is just the same as whats explained in the tutorial. The

pip install mysqlclient returns \"fatal error C1083: Cannot open file: 'mysql.h': No such file or directory

匿名 (未验证) 提交于 2019-12-03 01:13:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Here is this issue: I attempt to install mysqlclient like so C:\Users\amccommon349>pip install mysqlclient Collecting mysqlclient Using cached https://files.pythonhosted.org/packages/ec/fd/83329b9d3e14f7344d1 cb31f128e6dbba70c5975c9e57896815dbb1988ad/mysqlclient-1.3.13.tar.gz Installing collected packages: mysqlclient Running setup.py install for mysqlclient ... error Complete output from command c:\users\amccommon349\appdata\local\programs\python\python36\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\AMCCOM~1\\AppData\

About MySQLdb conn.autocommit(True)

匿名 (未验证) 提交于 2019-12-03 00:56:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have installed python 2.7 64bit,MySQL-python-1.2.3.win-amd64-py2.7.exe. I use the following code to insert data : class postcon: def POST(self): conn=MySQLdb.connect(host="localhost",user="root",passwd="mysql",db="dang",charset="utf8") cursor = conn.cursor() n = cursor.execute("insert into d_message (mid,title,content,image) values(2,'xx','ccc','fff')") cursor.close() conn.close() if n: raise web.seeother('/') This results in printing n as 1, but in mysql client data aren't visible. google says I must add conn.autocommit(True) . but I don

MySQLdb in Python: “Can't connect to MySQL server on 'localhost'”

匿名 (未验证) 提交于 2019-12-03 00:48:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have installed MySQLdb for Python and I am able to import MySQLdb. Now I try to connect to the MySQL Community Server on my local machine, using this code: db=MySQLdb.connect( host="localhost", user="br_admin", passwd="blabla", db="br_brain" ) This code fails with this error: Traceback (most recent call last): File " ", line 5, in db="brainse_brain" File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect return Connection(*args, **kwargs) File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init

Python操作Mysql

情到浓时终转凉″ 提交于 2019-12-03 00:35:59
首先,安装需要的环境,Mysql和Python就不说了,必备的东西。 主要是安装的MySQLdb,可以去sf.net下载,具体地址是http://sourceforge.net/projects/mysql-python/ 如果用Ubuntu,直接 sudo apt-get install python-mysqldb 安装完成之后可以在Python解释器中测试一下 输入 Python代码 import MySQLdb #注意大小写!! 如果不报错,就证明安装成功了,可能继续了 MySQLdb在Python中也就相当于JAVA中的MySQL的JDBC Driver,Python也有类似的数据接口规范Python DB API,MySQLdb就是Mysql的实现。操作也比较简单和其它平台或语言操作数据库一样,就是建立和数据库系统的连接,然后给数据库输入SQL,再从数据库获取结果。 先写一个最简单的,创建一个数据库: #!/usr/bin/env python #coding=utf-8 ################################### # @author peng # @date 2012-05-17 ################################## #MySQLdb 示例 # ################################

ImportError: this is MySQLdb version (1, 2, 5, 'final', 1), but _mysql is version (1, 4, 4, 'final', 0)

匿名 (未验证) 提交于 2019-12-02 22:51:30
git:(master) DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support Looking in indexes: http://pypi.douban.com/simple ERROR: Could not find a version that satisfies the requirement mysqlclient==1.2.5 (from versions: 1.3.0, 1.3.1, 1.3.2, 1.3.3, 1.3.4, 1.3.5, 1.3.6, 1.3.7, 1.3.8, 1.3.9, 1.3.10, 1.3.11rc1, 1