在Python中,mysqldb我可以将游标声明为字典光标,如下所示:
cursor = db.cursor(MySQLdb.cursors.DictCursor)
Run Code Online (Sandbox Code Playgroud)
这将使我能够cursor按名称引用循环中的列,如下所示:
for row in cursor: # Using the cursor as iterator
city = row["city"]
state = row["state"]
Run Code Online (Sandbox Code Playgroud)
是否可以使用此MySQL连接器创建字典光标? http://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-select.html
他们的例子只返回一个元组.
我想MySQL的创造者最终会为我们做这件事吗?
我有python 3模块安装的一些问题.我正在尝试使用pip安装mysql:
python -m pip install mysql
Run Code Online (Sandbox Code Playgroud)
对于python 3.4.4,这是我得到的错误:
error: command 'C:\\Program Files (x86\\Microsoft Visual Studio 10.0\\VC\\BIN\\cl.exe' failed with exit status 2
我之前遇到过模块安装的其他问题,我通过安装Visual Studio C++ 2010解决了这些问题
顺便说一句,其他模块的安装工作正常.
我的规格:Windows 10 x64
Python 3.4.4 x86
当我在虚拟环境中时,我尝试运行:
pip install MySQL-python
Run Code Online (Sandbox Code Playgroud)
这不起作用,所以我尝试下载包并通过运行安装它:
python setup.py install
Run Code Online (Sandbox Code Playgroud)
这将返回以下错误:
% python setup.py install ~VIRTUAL_ENV/build/MySQL-python running install install_dir /home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/ running bdist_egg running egg_info writing MySQL_python.egg-info/PKG-INFO writing top-level names to MySQL_python.egg-info/top_level.txt writing dependency_links to MySQL_python.egg-info/dependency_links.txt reading manifest file 'MySQL_python.egg-info/SOURCES.txt' reading manifest template 'MANIFEST.in' warning: no files found matching 'MANIFEST' warning: no files found matching 'ChangeLog' warning: no files found matching 'GPL' writing manifest file 'MySQL_python.egg-info/SOURCES.txt' installing library code to build/bdist.linux-x86_64/egg running install_lib running build_py copying MySQLdb/release.py -> build/lib.linux-x86_64-2.6/MySQLdb running build_ext building '_mysql' …
ERROR 2006(HY000)第1行:MySQL服务器已经消失
我面临同样的问题.我正在尝试将mysqldump文件恢复到我的机器.该文件的大小为2.7 MB.我得到"ERROR 2006(HY000)在第1行:MySQL服务器已经消失"错误.我增加了max_allowed_packet和wait_timeout的大小.但没有任何效果.
以下是我的文件(my-medium.ini)的内容,请告诉我我错在哪里.我的安装有各种ini文件,我修改过,my-large,my-medium,my-small,my-huge,my-innodb-heavy-4G.提前致谢!!
# Example MySQL config file for medium systems.
#
# This is for a system with little memory (32M - 64M) where MySQL plays
# an important part, or systems up to 128M where MySQL is used together with
# other programs (such as a web server)
#
# You can copy this file to
# /etc/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options (in this
# installation this directory is …Run Code Online (Sandbox Code Playgroud) 我在OS X 10.10.5 Yosemite上用Homebrew安装了MySQL和Python.我的Python 2.7 python -> ../Cellar/python/2.7.9/bin/python带有一个符号链接/usr/local/bin/python.
在/usr/local/bin有一个符号链接:
mysql -> ../Cellar/mysql/5.7.9/bin/mysql
在Python shell中:
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/_mysql.so, 2): Library not loaded: /usr/local/lib/libmysqlclient.18.dylib
Referenced from: /usr/local/lib/python2.7/site-packages/_mysql.so
Reason: image not found
Run Code Online (Sandbox Code Playgroud)
所以我尝试过:
$ sudo unlink /usr/local/lib/libmysqlclient.18.dylib
其次是:
DYLD_LIBRARY_PATH=/usr/local/mysql/lib/:$DYLD_LIBRARY_PATH
然后(对理性的绝望):
$ export DYLD_LIBRARY_PATH=/usr/local/Cellar/mysql/5.7.9/lib
但在这两种情况下import MySQLdb仍然试图导入libmysqlclient.18.dylib.
然后我尝试了:
$ pip install -U MySQL-python …
尝试在Python 3.6.0上使用pip3安装mysqlclient
$ pip3 install mysqlclient
Collecting mysqlclient
Using cached mysqlclient-1.3.10.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/3k/08g3yx_12kg99kyfs989md600000gn/T/pip-build-1qv_89jc/mysqlclient/setup.py", line 17, in <module>
metadata, options = get_config()
File "/private/var/folders/3k/08g3yx_12kg99kyfs989md600000gn/T/pip-build-1qv_89jc/mysqlclient/setup_posix.py", line 54, in get_config
libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]
File "/private/var/folders/3k/08g3yx_12kg99kyfs989md600000gn/T/pip-build-1qv_89jc/mysqlclient/setup_posix.py", line 54, in <listcomp>
libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]
File "/private/var/folders/3k/08g3yx_12kg99kyfs989md600000gn/T/pip-build-1qv_89jc/mysqlclient/setup_posix.py", line 12, in dequote
if s[0] in "\"'" and …Run Code Online (Sandbox Code Playgroud) 我安装MySQLdb之后
sudo apt-get install python-mysqldb
Run Code Online (Sandbox Code Playgroud)
我可以在使用Python 2.6时导入和使用它,但不能使用2.7.(ImportError:没有名为MySQLdb的模块)
我认为apt-get在版本2.6中安装了MySQLdb,但我不知道如何使用python 2.7.请帮我.非常感谢!
我收到很多同样的警告 -
Warning: Truncated incorrect DOUBLE value: '512121500B'
Run Code Online (Sandbox Code Playgroud)
- 但无法弄清楚原因.
MySQL表dr_snapshot看起来像这样:
PremiseID char(10) Primary Key
cycle SMALLINT(6)
last_read DATETIME
reading INTEGER
DeviceID INTEGER
Run Code Online (Sandbox Code Playgroud)
我想通过PremiseID删除很多行.这是一个特别的:'512121500B'.
这是我的功能,由PremiseID删除:
def delDrSnapRow(premiseID, db):
sql_cmd = " ".join([
"delete ",
"from dr_snapshot ",
"where PremiseID = " + str(premiseID) + " ; "])
del_cur = db.cursor()
rc = del_cur.execute(sql_cmd)
del_cur.close()
return rc
Run Code Online (Sandbox Code Playgroud)
PremiseID是一个字符串,而不是double.我在这里错过了什么?
非常感谢.
修改我的删除程序后使用try:..除了,我没有看到警告.我相信我没有看到警告,因为我没有看到警告显示 - print(e)- 在除了部分.
我得到的结论是尝试:除了以某种方式删除了错误.
def delDrSnapRow(premiseID, db):
sql_cmd = " ".join([
"delete ",
"from dr_snapshot ",
"where PremiseID = …Run Code Online (Sandbox Code Playgroud) 我是Python的新手,并尝试设置一个Django项目来使用MySql.我已阅读了有关该主题的文档以及其他一些StackOverflow帖子,但我仍然无法使其工作.
当我尝试在Django中运行迁移时,我收到以下错误:
Error loading MySQLdb module: No module named 'MySQLdb'
Run Code Online (Sandbox Code Playgroud)
我已经安装了推荐的MySql Python Connector(2.0.1),选择了Ubuntu(因为我在Mint Linux上).它安装正确.我仍然得到错误.我不需要将这个库添加到项目或其他任何东西,对吗?看起来python应该只是意识到这一点并成功运行.我能做什么?谢谢.
编辑:我忘了提到:我正在运行Python 3.4 - 许多典型的解决方案仍然使用2.7,所以它们不起作用,所有3.0的解决方案我都试过没有成功.
我在OSX El Capitan上,使用Python 2.7(Anaconda).启动命令会pip install MySQL-python产生:
Collecting MySQL-python
Using cached MySQL-python-1.2.5.zip
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/d2/gd004m2s35z5dlyz9mfn6sc40000gn/T/pip-build-FYvb_T/MySQL-python/setup.py", line 17, in <module>
metadata, options = get_config()
File "setup_posix.py", line 53, in get_config
libraries = [ dequote(i[2:]) for i in libs if i.startswith(compiler_flag("l")) ]
File "setup_posix.py", line 8, in dequote
if s[0] in "\"'" and s[0] == s[-1]:
IndexError: string index out of range
Run Code Online (Sandbox Code Playgroud)
这些都不起作用(以相同的错误退出).
pip …mysql-python ×10
python ×8
mysql ×6
pip ×3
python-3.x ×2
ubuntu ×2
django ×1
homebrew ×1
macos ×1
macos-sierra ×1
virtualenv ×1