我正在运行Mac Lion.我安装了python 2.7.2和django 1.3.1.他们两个都很好.为了让python与mysql一起工作,我必须安装MySQL-python-1.2.3 - 但是我无法使构建工作.对于MySQL,我有XAMPP,我已经将XAMPP的bin设置为建筑物所需的mysql_config的系统环境路径.
SOFXGF-AIR:MySQL-python-1.2.3 Sofia$ python setup.py buildrunning build
running build_py
copying MySQLdb/release.py -> build/lib.macosx-10.6-intel-2.7/MySQLdb
running build_ext
building '_mysql' extension
gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -isysroot /Developer/SDKs/MacOSX10.6.sdk -g -O2 -DNDEBUG -g -O3 -Dversion_info=(1,2,3,'final',0) -D__version__=1.2.3 -I/Applications/XAMPP/xamppfiles/include/mysql -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.6-intel-2.7/_mysql.o -mmacosx-version-min=10.4 -arch i386 -arch ppc -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT -DDONT_DECLARE_CXA_PURE_VIRTUAL
In file included from /Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/unicodeobject.h:4,
from /Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/Python.h:85,
from pymemcompat.h:10,
from _mysql.c:29:
/Developer/SDKs/MacOSX10.6.sdk/usr/include/stdarg.h:4:25: error: stdarg.h: No such file or directory
_mysql.c:36:23: error: my_config.h: No such file or directory
_mysql.c:38:19: …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用python和MySQLdb运行ETL脚本,但我坚持使用初始提取查询的结果.当我想要Int和Float时,返回的类型都是Long和Decimal.我搜索了几个小时,试图找到答案但没有任何成功.
database = MySQLdb.connect(host='db',user='user',
passwd='password', db='db123')
database_cursor = database.cursor()
database_query = ("SELECT id, siteId, campaignId, hour, sum(impressions) AS impressions, "
"sum(clicks) AS clicks, sum(conversions) AS conversions, sum(costs/1000000) AS revenue "
"FROM database.DM_PublisherFact_Hourly WHERE time = '%s' GROUP BY siteId, campaignId;") %(today)
print database_query
database_cursor.execute(database_query)
result = database_cursor.fetchone()
database.close()
Run Code Online (Sandbox Code Playgroud)
返回值是Longs和Decimals的元组.我尝试将转换字典显式传递给connect对象,但没有运气.有帮助吗?
这是转换代码
"""MySQLdb type conversion module
This module handles all the type conversions for MySQL. If the default
type conversions aren't what you need, you can make your own. The
dictionary conversions …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的代码:
db = MySQLdb.connect(user='root', db='galaxy', passwd='devil', host='localhost')
cursor = db.cursor()
cursor.execute('SELECT username, password FROM galaxy_user')
names = [row[0] for row in cursor.fetchall()]
passw = [password[1] for password in cursor.fetchall()]
db.close()
Run Code Online (Sandbox Code Playgroud)
问题是我只能从以下代码访问名称或密码.有了这个,我只能得到用户名.我得到了passw的空列表.现在,如果我切换它:
passw = [row[1] for row in cursor.fetchall()]
names = [password[1] for password in cursor.fetchall()
Run Code Online (Sandbox Code Playgroud)
我得到了passw的值,但名字是空列表.发生了什么?
我正在编写一个Python脚本,用于从MySQL数据库中获取,处理和更新.
我最初开始用MySQL数据库的逗号分隔值转储来攻击这个问题,我将它扔进一个sqlite数据库(使用sqlite3).我在Python(2.7)中进行处理,创建输出的CSV文件,然后用另一个脚本将其上传回MySQL数据库.
那么,我想我会尝试直接从Python脚本拉/推到MySQL数据库.所以我安装了MySQLdb,然后去了镇上.
我现在发现的是,我INSERT从MySQL数据库(到sqlite数据库)的s不会像以前那样加入.整数的表示现在在末尾加上L,十进制值表示为类似的Decimal('4.00').
基本上,JOIN当我从CSV文件中插入它们时很好的东西现在不能很好地工作.
我的问题:我是否要求在这条道路上继续进行痛苦的世界,还是有一种简单的方法可以让MySQLdb和sqlite3库一起玩?如果没有,那么我将安装MySQL服务器并重构我的代码以仅使用MySQL.
我使用Mysqldb和Python(2.7)从一个带有charset latin1的Mysql数据库返回一堆值.
mysql数据库具有类型为bit(1)的值,当返回它们时,它们看起来像'\ x01'
十进制值返回如此十进制('0E-8')
他们剩下的价值和类型都很好.
示例数据库结果集:
{'test':'\ x01','currency':u'bla','balance':十进制('0E-8'),'first':u'John'}
这就是我连接数据库的方式:self.con = MySQLdb.connect(host = conn ['host'],user = conn ['user'],passwd = conn ['passwd'],db = conn ['db '],charset = conn ['charset'],port = int(conn ['port'])如果conn中的'port'3306)
我希望bit(1)值返回True或False.我环顾四周,我认为可能是MySQLdb.converters.conversions,但我不知道如何实现这一点.任何想法都会很棒.谢谢
我无权更改数据库中的类型.
当尝试为Django安装requirements.txt时,我在尝试安装Mysql-python时遇到以下错误
Traceback (most recent call last):
File "<string>", line 17, in <module>
File "/home/abercrrl/.virtualenvs/django_at/build/MySQL-python/setup.py", line 7, in <module>
use_setuptools()
File "distribute_setup.py", line 145, in use_setuptools
return _do_download(version, download_base, to_dir, download_delay)
File "distribute_setup.py", line 125, in _do_download
_build_egg(egg, tarball, to_dir)
File "distribute_setup.py", line 116, in _build_egg
raise IOError('Could not build the egg.')
IOError: Could not build the egg.
----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /home/abercrrl/.virtualenvs/django_at/build/MySQL-python
Storing debug log for failure in /home/abercrrl/.pip/pip.log
Run Code Online (Sandbox Code Playgroud)
请注意,我尝试过:
pip …Run Code Online (Sandbox Code Playgroud) 我使用python-2.7和newbie到mysql/mysql-python连接器.
我只是想通过以下查询来检索数据 -
SELECT d_id,d_link,d_name FROM d_details
但它给出/返回 无.以下是我的代码 -
def getdbconnection(self):
try:
self.cnx = mysql.connector.connect(user='abc',password='xxx',host = 'localhost',port = 'xxx', database='details',buffered=True)
print "Done"
self.cursor = self.cnx.cursor()
except MySQLdb.Error as error:
print "ERROR IN CONNECTION"
def selectst(self):
try:
print "S E L E C T"
self.d_deta = self.cursor.execute("SELECT d_id,d_link,d_name FROM `d_details`")
print self.d_deta
except MySQLdb.Error as error:
print "---------------------"""
print(error)
self.cnx.commit()
Run Code Online (Sandbox Code Playgroud)
和输出是
Done
S E L E C T
None
Run Code Online (Sandbox Code Playgroud)
虽然查询在工作台中运行良好
欢迎任何形式的帮助/指导.
我最近一直在学习Python,并学会了如何连接数据库并使用MYSQLdb从数据库中检索数据.但是,所有示例都显示了如何获取多行数据.我想知道如何只检索一行数据.
这是我目前的方法.
cur.execute("SELECT number, name FROM myTable WHERE id='" + id + "'")
results = cur.fetchall()
number = 0
name = ""
for result in results:
number = result['number']
name = result['name']
Run Code Online (Sandbox Code Playgroud)
这似乎是多余的,for result in results:因为我知道只有一个结果.
如何在不使用for循环的情况下获取一行数据?
我刚刚找到了用于将 Python 连接到 MySQL 数据库的 pymysql 模块。我有一个数据库,其中有一个名为“loot”的表,loot 包含一个名为“wins”的列。我的代码包含一个名为 'won' 的变量,该变量在 SQL 行之前被赋予一个值。我希望将变量 'won' 输入到 id=1 的 'wins' 列中。id=1 行已存在于数据库中。
下面的代码抛出一个错误 pymysql.err.InternalError: (1054, "Unknown column 'won' in 'field list'")
我的问题:为什么我会收到这个错误,我做错了什么?
编码:
import pymysql
# Open database connection
db = pymysql.connect(host='*******',user='******',password='*****',db='******')
# prepare a cursor object using cursor() method
cursor = db.cursor()
won=1
# Prepare SQL query to UPDATE required records
sql = "UPDATE loot SET wins = won WHERE id = 1"
# Execute the SQL command
cursor.execute(sql)
# Commit …Run Code Online (Sandbox Code Playgroud) 我有一个python脚本来抓取和网站它获得2个变量并将它们存储在2个列表中.然后我使用executemany使用一个变量更新MySQL数据库以匹配预先存在的行以插入另一个变量.
import mysql.connector
from bs4 import BeautifulSoup as soup
from selenium import webdriver
import time, re
mydb = mysql.connector.connect(
host="host",
user="user",
passwd="passwd",
database="database"
)
mycursor = mydb.cursor()
d = webdriver.Chrome('D:/Uskompuf/Downloads/chromedriver')
d.get('https://au.pcpartpicker.com/products/cpu/overall-list/#page=1')
def cpus(_source):
result = soup(_source, 'html.parser').find('ul', {'id':'category_content'}).find_all('li')
_titles = list(filter(None, [(lambda x:'' if x is None else x.text)(i.find('div', {'class':'title'})) for i in result]))
data = [list(filter(None, [re.findall('(?<=\().*?(?=\))', c.text) for c in i.find_all('div')])) for i in result]
return _titles, [a for *_, [a] in filter(None, data)]
_titles, …Run Code Online (Sandbox Code Playgroud) mysql-python ×10
python ×9
mysql ×4
python-3.x ×2
converter ×1
django ×1
long-integer ×1
python-2.7 ×1
sql ×1
sqlite ×1