我有一个表,我想执行一个将返回两行值的查询:
cursor.execute("""SELECT `egg_id`
FROM `groups`
WHERE `id` = %s;""", (req_id))
req_egg = str(cursor.fetchone())
print req_egg
Run Code Online (Sandbox Code Playgroud)
该列egg_id有两行它可以为该查询返回,但是上面的代码只会打印第一个结果——我希望它也显示第二个结果,我将如何获得这两个值?
编辑:有没有办法用 fetchmany 将每个变量存储在一个单独的变量中?
“不正确的字符串值错误”是从 MySQLdb 引发的。
_mysql_exceptions.OperationalError: (1366, "Incorrect string value: '\\xF0\\xA0\
\x84\\x8E\\xE2\\x8B...' for column 'from_url' at row 1")
Run Code Online (Sandbox Code Playgroud)
但是我已经将连接字符集和从 url 编码设置为 utf8。对于数百万以前的记录,它可以毫无问题地工作。
将导致异常的值:我认为问题与特殊字符 u'\U0002010e' (中文特殊字符“?”)有关
u'http://www.ettoday.net/news/20120227/27879.htm?fb_action_ids=305328666231772&
fb_action_types=og.likes&fb_source=aggregation&fb_aggregation_id=288381481237582
\u7c89\u53ef\u611b\U0002010e\u22ef http://www.ettoday.net/news/20120221/26254.h
tm?fb_action_ids=305330026231636&fb_action_types=og.likes&fb_source=aggregation&
fb_aggregation_id=288381481237582 \u597d\u840c\u53c8\u22ef'
Run Code Online (Sandbox Code Playgroud)
但是这个字符也可以在python中编码为utf8。
>>> u'\U0002010e'.encode('utf8')
'\xf0\xa0\x84\x8e'
Run Code Online (Sandbox Code Playgroud)
那么为什么 MySQL 不能接受这个字符呢?
我第一次使用 mysql-connector/python 并且遇到错误。net我想从名为 的 mysql 表中获取电子邮件地址blah。
cursor = cnx.cursor()
p = "net"
query = ("SELECT * FROM blah WHERE email LIKE %s limit 10", ("%" + p + "%",))
cursor.execute(query,(p,))
for row in cursor:
print row
cursor.close()
cnx.close()
Run Code Online (Sandbox Code Playgroud)
我收到这个错误
Traceback (most recent call last):
File "<input>", line 9, in <module>
File "C:\Python27\lib\site-packages\mysql\connector\cursor.py", line 491, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 683, in cmd_query
statement))
File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 601, in _handle_result
raise errors.get_exception(packet)
ProgrammingError: 1064 (42000): You …Run Code Online (Sandbox Code Playgroud) 当我用实际的列名称替换列变量时,这是有效的。不过我确实需要一个变量。当我使用变量时,出现 MySQL 语法错误。字段可以是变量吗?如果是的话,错误在哪里?
conn = self.create_connection()
cur = conn[0]
db = conn[1]
cur.execute('''
UPDATE coefficients
SET %s = %s
WHERE coef_id = %s
''' , (sql_col_name, fgi, ici))
db.commit()
Run Code Online (Sandbox Code Playgroud)
好的,这是回溯:
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''base_rpm' = 3500 WHERE coef_id = 460' at line 1")
Run Code Online (Sandbox Code Playgroud) 我试过
sudo pip3 install flask-mysqldb
Run Code Online (Sandbox Code Playgroud)
但出现以下错误:
命令“python setup.py Egg_info”在 /private/tmp/pip-build-3l4jec4_/mysqlclient/ 中失败,错误代码为 1
我对 Django 和一般的 Web 开发比较陌生。
我在尝试着
pip install mysqlclient
Run Code Online (Sandbox Code Playgroud)
在我的
virtualenv -p python3
Run Code Online (Sandbox Code Playgroud)
将 Django 2.0 连接到 mySQL。但是,我收到此错误:
Collecting mysqlclient
Using cached mysqlclient-1.3.12.tar.gz
Complete output from command python setup.py egg_info:
/bin/sh: mysql_config: command not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/43/md5vpqrx0mx8627sq04slbz00000gn/T/pip-build-l8ea3vja/mysqlclient/setup.py", line 17, in <module>
metadata, options = get_config()
File "/private/var/folders/43/md5vpqrx0mx8627sq04slbz00000gn/T/pip-build-l8ea3vja/mysqlclient/setup_posix.py", line 44, in get_config
libs = mysql_config("libs_r")
File "/private/var/folders/43/md5vpqrx0mx8627sq04slbz00000gn/T/pip-build-l8ea3vja/mysqlclient/setup_posix.py", line 26, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
OSError: mysql_config not …Run Code Online (Sandbox Code Playgroud) 我需要在我的 Python 程序中使用 MySQL 数据库,所以我需要将 Python 连接到 MySQL。
我正在使用 MacOsX Sierra,我尝试安装连接器、mysqlclient,但没有任何效果。
我使用 PyCharm,每当我输入
import MySQLdb
Run Code Online (Sandbox Code Playgroud)
或者
import mysql.connector
Run Code Online (Sandbox Code Playgroud)
我收到错误:回溯(最近一次通话):文件“/Users/muhammed/Desktop/PyhtonHomework/venv/homework.py”,第 1 行,导入 MySQLdb ModuleNotFoundError: No module named 'MySQLdb'
我该如何解决?我有一个学期项目,由于这个错误,我什至无法开始。
Python版本:3.6.4
我试图用scrapy将我抓取的数据存储到SQL数据库中,但我的代码在运行时没有提到任何错误,但没有发送任何内容。
我正在使用我的 sql 连接器,因为我无法安装 MySQL-python。我的 SQL 数据库似乎运行良好,当我运行代码时,流量 KB/s 增加。请在我的 pipelines.py 代码下方找到。
import mysql.connector
from mysql.connector import errorcode
class CleaningPipeline(object):
...
class DatabasePipeline(object):
def _init_(self):
self.create_connection()
self.create_table()
def create_connection(self):
self.conn = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '********',
database = 'lecturesinparis_db'
)
self.curr = self.conn.cursor()
def create_table(self):
self.curr.execute("""DROP TABLE IF EXISTS mdl""")
self.curr.execute("""create table mdl(
title text,
location text,
startdatetime text,
lenght text,
description text,
)""")
def process_item(self, item, spider):
self.store_db(item)
return item
def store_db(self, item):
self.curr.execute("""insert into …Run Code Online (Sandbox Code Playgroud) 我使用 python vitualenv,当我想安装任何软件包时,pip install <package name>我会出错:
ERROR: Command errored out with exit status 1:
command: /home/user/project/some_project_name/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-sifdsjjx/MySQL-python/setup.py'"'"'; __file__='"'"'/tmp/pip-install-sifdsjjx/MySQL-python/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-sifdsjjx/MySQL-python/pip-egg-info
cwd: /tmp/pip-install-sifdsjjx/MySQL-python/
Complete output (7 lines):
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-sifdsjjx/MySQL-python/setup.py", line 13, in <module>
from setup_posix import get_config
File "/tmp/pip-install-sifdsjjx/MySQL-python/setup_posix.py", line 2, in <module>
from ConfigParser import SafeConfigParser
ModuleNotFoundError: No module named 'ConfigParser'
----------------------------------------
ERROR: Command …Run Code Online (Sandbox Code Playgroud) 我不知道以什么方式解决这个错误。任何提示?我有简单的 Django 项目,并在尝试执行 python3 manage.py migrate 时收到此错误。这与应用程序中的任何编程错误有关,或者这可能与 mysql 的安装及其软件包的完整性有关吗?可能 manage.py 文件有什么错误?或者这可能是 Django 和 mysql 不兼容版本的情况?
Traceback (most recent call last):
File "manage.py", line 23, in <module>
execute_from_command_line(sys.argv)
File "/home/anna/.local/lib/python3.7/site-packages/django/core/management/__init__.py",
line 401, in execute_from_command_line
utility.execute()
File "/home/anna/.local/lib/python3.7/site-packages/django/core/management/__init__.py",
line 377, in execute
django.setup()
File "/home/anna/.local/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/anna/.local/lib/python3.7/site-packages/django/apps/registry.py", line 114, in
populate
app_config.import_models()
File "/home/anna/.local/lib/python3.7/site-packages/django/apps/config.py", line 211, in
import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in …Run Code Online (Sandbox Code Playgroud) mysql-python ×10
python ×9
mysql ×4
macos ×2
pip ×2
virtualenv ×2
django ×1
python-2.7 ×1
scrapy ×1
select ×1
sql ×1
utf-8 ×1