我正在尝试使用easy_install来安装MySQL-python.它几乎立即失败:
_mysql.c:36:23:错误:my_config.h:没有这样的文件或目录
_mysql.c:38:19:错误:mysql.h:没有这样的文件或目录
_mysql.c:39:26:错误:mysqld_error. h:没有这样的文件或目录
_mysql.c:40:20:错误:errmsg.h:没有这样的文件或目录
它找不到标题.我安装了标头,它们只是从/ opt中的源代码安装.它显然不是在寻找那里.我该如何看待它?例如,如果这是配置,我可以做类似"--with-mysql =/opt/mysql"的操作.看来easy_install没有这样的选项.还在研究,如果我找到答案,我会在这里发布.
我正在使用远程数据库将数据导入我的Django proyect的数据库.
在MySQLdb我的帮助下, 我轻松地创建了一个导入函数,如下所示:
def connect_and_get_data(useful_string):
CONNECTION = MySQLdb.connect(host=..., port=...,
user=..., passwd=..., db=...,
cursorclass=MySQLdb.cursors.DictCursor,
charset = "utf8")
cursor = CONNECTION.cursor()
cursor.execute("SELECT ... FROM ... WHERE ... AND some_field=%s", (useful_string))
result = cursor.fetchall()
cursor.close()
Run Code Online (Sandbox Code Playgroud)
非常满意,按预期工作.
但继续使用代码,我注意到有时我需要再次连接到数据库,以便执行其他不同的查询.
对我来说,第一个想法是非常符合逻辑的:对于我需要的每个查询,定义一个函数,该函数connect_and_get_data使用给定的查询作为参数进行调用...类似这样:
def get_data_about_first_amazing_topic(useful_string):
query = "SELECT ... FROM ... WHERE ... AND some_field=%s" %(useful_string)
connect_and_get_data(query)
...
def get_data_about_second_amazing_topic(other_useful_string):
query = "SELECT ... FROM ... WHERE ... AND some_field=%s" %(other_useful_string)
connect_and_get_data(query)
...
Run Code Online (Sandbox Code Playgroud)
通过以下修改connect_and_get_data:
def connect_and_get_data(query):
...
cursor.execute(query)
...
Run Code Online (Sandbox Code Playgroud)
正如您可能想象的那样,此解决方案失败了.
阅读mluebke对 …
我在文档中读到:
...因为事务在游标执行查询时开始,但在Connection对象执行COMMIT或ROLLBACK时结束.
import MySQLdb
db = MySQLdb.connect(user="root", db="test")
c = db.cursor()
c.execute("SELECT * FROM books")
print c.fetchall()
Run Code Online (Sandbox Code Playgroud)
我怀疑MySQLdb甚至在不修改数据的查询(如SELECT)上启动事务,因为很难知道查询是否只读取数据而不写入数据.
cursor.commit()在每次查询后执行,以确保没有表被锁定?谢谢
这个问题可能很无聊.
而不是硬编码MySQLdb连接对象:例如,
db = MySQLdb.connect('localhost','name','pwrd','db_name')
Run Code Online (Sandbox Code Playgroud)
如何设置它以便我可以从列表或其他变量中指定db_name(或连接对象的任何其他部分).例如:
for NAME from list_of_names:
db = MySQLdb.connect('localhost', 'name', 'pwrd', NAME)
Run Code Online (Sandbox Code Playgroud) 我安装了python 2.7 64bit,MySQL-python-1.2.3.win-amd64-py2.7.exe.
我使用以下代码插入数据:
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('/')
Run Code Online (Sandbox Code Playgroud)
这导致打印n为1,但在mysql中客户端数据不可见.
谷歌说我必须添加conn.autocommit(True).
但我不知道为什么MySQLdb将其关闭;
我不是python用户,虽然我使用过MySQL,但我不是专家.另外,我主要是一个Windows用户,我对运行命令行脚本了解不多.所以这可能是一个愚蠢的问题,但无论如何我想问...
(BTW我的目的是安装'棒球棒')
我安装了Python(Windows 7机器上的3.3版本).
接下来它说要安装pymysql.所以我从https://github.com/petehunt/PyMySQL/下载了.zip文件
自述文件说"只需从本地目录运行build-py3k.sh脚本.它将在./py3k目录中构建一个工作包." 我完全不知道这意味着什么.我该怎么办?我尝试在Python命令行界面中键入它,它只是给了我一个错误.
我在Django中使用innoDB表和mysql数据库.在调查错误期间
OperationalError:(1213,'尝试获取锁时发现死锁;尝试重启事务')
客户端应自动重试.
我试图将这个逻辑放在代码中,但同时在django中可以直接使用任何钩子.这样我们就可以设置3次自动重试,以防死锁.此外,如果有人可以举例说明将这个逻辑放在代码中(我使用的是django过滤器).
PS:我可以在Omry的答案下面问这个问题,但我低于50分并想把它带给django专家.
我正在迭代SELECT结果,像这样:
import MySQLdb
conn = MySQLdb.connect(host = 127.0.0.1, user = ...) # and so on
cur = conn.cursor()
cur.execute("SELECT * FROM some_table")
for row in cur:
# some stuff I'm doing
# sometimes I need to perform another SELECT here
Run Code Online (Sandbox Code Playgroud)
问题是,我可以cur在for循环内再次使用,还是我必须创建另一个游标(甚至更多 - 另一个连接)?
我想我在这里缺少一些关于数据库或Python的基本知识......我实际上对这两者都很新.此外,我试图谷歌答案失败了.
我甚至会猜测自己必须创建另一个光标,但我认为我已经实际使用了它一段时间之后才意识到这可能是错误的并且似乎有效.但我现在有点困惑,无法保证.所以我只是想确定一下.
谢谢.
我只是想安装Mysql-python,但每次都会发生错误.我已经尝试了所有提到的解决方案作为安装轮,使用easy_install,更新Visual C++ for python,安装mysql连接器,甚至安装最新的mysql本身,以及其他所有事情都出现在我的视线和我的脑海中.我在Windows 10上安装了python 2.7.11.我安装了大多数Windows更新.总是会发生:
easy_install Mysql-python
Searching for Mysql-python
Reading https://pypi.python.org/simple/Mysql-python/
Best match: MySQL-python 1.2.5
Downloading https://pypi.python.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip#md5=654f75b302db6ed8dc5a898c625e030c
Processing MySQL-python-1.2.5.zip
Writing c:\users\aref\appdata\local\temp\easy_install-nhmvzf\MySQL-python-1.2.5\setup.cfg
Running MySQL-python-1.2.5\setup.py -q bdist_egg --dist-dir c:\users\aref\appdata\local\temp\easy_install-nhmvzf\MySQL-python-1.2.5\egg-dist-tmp-idut0x
_mysql.c
C:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2\include\config-win.h(203) : warning C4005: 'SIZEOF_OFF_T' : macro redefinition
c:\python27\include\pyconfig.h(353) : see previous definition of 'SIZEOF_OFF_T'
C:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2\include\config-win.h(301) : warning C4005: 'HAVE_STDDEF_H' : macro redefinition
c:\python27\include\pyconfig.h(693) : see previous definition of 'HAVE_STDDEF_H'
_mysql.c(287) : warning C4244: '=' : conversion from 'Py_ssize_t' to …Run Code Online (Sandbox Code Playgroud) 我有一个使用Flask和MySQL的应用程序。该应用程序未从Flask应用程序连接到MySQL容器,但可以使用具有相同凭据的Sequel Pro对其进行访问。
Docker撰写文件
version: '2'
services:
web:
build: flask-app
ports:
- "5000:5000"
volumes:
- .:/code
mysql:
build: mysql-server
environment:
MYSQL_DATABASE: test
MYSQL_ROOT_PASSWORD: root
MYSQL_ROOT_HOST: 0.0.0.0
MYSQL_USER: testing
MYSQL_PASSWORD: testing
ports:
- "3306:3306"
Run Code Online (Sandbox Code Playgroud)
MySQL的Docker文件
MySQL的docker文件将从test.dump文件中添加架构。
FROM mysql/mysql-server
ADD test.sql /docker-entrypoint-initdb.d
Run Code Online (Sandbox Code Playgroud)
Flask的Docker文件
FROM python:latest
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["app.py"]
Run Code Online (Sandbox Code Playgroud)
初始点 app.py
from flask import Flask, request, jsonify, Response
import json
import mysql.connector
from flask_cors import CORS, cross_origin
app = Flask(__name__)
def …Run Code Online (Sandbox Code Playgroud) mysql-python ×10
python ×7
mysql ×4
django ×2
cursor ×1
database ×1
docker ×1
easy-install ×1
header-files ×1
pymysql ×1
python-3.x ×1