我可以从python连接到我的本地mysql数据库,我可以创建,选择和插入单个行.
我的问题是:我可以直接指示mysqldb获取整个数据帧并将其插入现有表中,还是我需要遍历行?
在任何一种情况下,对于一个包含ID和两个数据列以及匹配数据帧的非常简单的表,python脚本会是什么样子?
我在更新MySQL数据库中的某行时遇到了一些麻烦.这是我正在尝试运行的代码:
import MySQLdb
conn=MySQLdb.connect(host="localhost", user="root", passwd="pass", db="dbname")
cursor=conn.cursor()
cursor.execute("UPDATE compinfo SET Co_num=4 WHERE ID=100")
cursor.execute("SELECT Co_num FROM compinfo WHERE ID=100")
results = cursor.fetchall()
for row in results:
print row[0]
print "Number of rows updated: %d" % cursor.rowcount
cursor.close()
conn.close()
Run Code Online (Sandbox Code Playgroud)
我运行这个程序时得到的输出是:
4
更新的行数:1
它似乎正在工作,但如果我从MySQL命令行界面(CLI)查询数据库,我发现它根本没有更新.但是,如果从CLI我输入UPDATE compinfo SET Co_num=4 WHERE ID=100;数据库按预期更新.
我的问题是什么?我正在Windows机器上运行带有MySQL 5.1.30的Python 2.5.2.
两种方法都返回查询返回项的列表,我在这里错过了什么吗?
或者他们确实有相同的用法?
任何性能差异?
我有一份清单,例如[['a','b'],['c','d']].
我有一个称为表T和两个字段F1,F2.字段列表中的第一项映射到F1,第二项F2.
如何在单个命令或调用中为每个内部列表插入行,而不是像这样使用for循环?
for i in [['a','b'],['c','d']]:
c.execute("insert into T (F1,F2) values (%s, %s)", (i[0], i[1]))
Run Code Online (Sandbox Code Playgroud) 请帮我.我正在运行一个简单的python程序,它将以tkinter形式显示mySQL数据库中的数据...
from Tkinter import *
import MySQLdb
def button_click():
root.destroy()
root = Tk()
root.geometry("600x500+10+10")
root.title("Ariba")
myContainer = Frame(root)
myContainer.pack(side=TOP, expand=YES, fill=BOTH)
db = MySQLdb.connect ("localhost","root","","chocoholics")
s = "Select * from member"
cursor = db.cursor()
cursor.execute(s)
rows = cursor.fetchall()
x = rows[1][1] + " " + rows[1][2]
myLabel1 = Label(myContainer, text = x)
y = rows[2][1] + " " + rows[2][2]
myLabel2 = Label(myContainer, text = y)
btn = Button(myContainer, text = "Quit", command=button_click, height=1, width=6)
myLabel1.pack(side=TOP, expand=NO, fill=BOTH)
myLabel2.pack(side=TOP, expand=NO, …Run Code Online (Sandbox Code Playgroud) 我正在使用OSX 10.8和PyCharm来处理Python开发项目.我已经使用网站上的说明为mac安装了MySQL-python
http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found
但是,运行该项目会给我这个错误:
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Users/ashishagarwal/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so, 2): Symbol not found: _mysql_affected_rows
Referenced from: /Users/ashishagarwal/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so
Expected in: flat namespace
in /Users/ashishagarwal/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so
Run Code Online (Sandbox Code Playgroud)
错误中提到的文件存在于该位置 - /Users/ashishagarwal/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so
整个错误消息是 -
/usr/local/bin/python2.7-32 /Users/ashishagarwal/Optimus/MashPotato/backend/mashpotato/manage.py testserver --addrport 8000
Running on development server
Traceback (most recent call last):
File "/Users/ashishagarwal/Optimus/MashPotato/backend/mashpotato/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 77, in load_command_class …Run Code Online (Sandbox Code Playgroud) 在 python 中运行类似以下内容的建议方法是什么:
self.cursor.execute('SET FOREIGN_KEY_CHECKS=0; DROP TABLE IF EXISTS %s; SET FOREIGN_KEY_CHECKS=1' % (table_name,))
Run Code Online (Sandbox Code Playgroud)
例如,这应该是三个单独的self.cursor.execute(...)语句吗?除了cursor.execute(...)做这样的事情之外,是否应该使用特定的方法,或者这样做的建议做法是什么?目前我的代码如下:
self.cursor.execute('SET FOREIGN_KEY_CHECKS=0;')
self.cursor.execute('DROP TABLE IF EXISTS %s;' % (table_name,))
self.cursor.execute('SET FOREIGN_KEY_CHECKS=1;')
self.cursor.execute('CREATE TABLE %s select * from mytable;' % (table_name,))
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,一切都是单独运行的……所以我不确定这是否是一个好主意(或者更确切地说 - 执行上述操作的最佳方法是什么)。也许BEGIN...END?
我正在努力在系统范围内或在安装了MariaDB 10的Ubuntu 14.04上的venv中安装mysql-python pip.还尝试使用MariaDB 5.5并获得相同的错误.我没有安装vanilla mysql-server这个问题.
我通过apt-get安装了以下内容:
最初我认为这是一个安装到venv中的问题,但我后来发现mysql-python也不会在全系统安装.下面是我用来安装在venv中的cmds.
virtualenv venv
. venv/bin/activate
pip install mysql-python==1.2.5
In file included from _mysql.c:44:0:
/usr/include/mysql/my_config.h:439:0: warning: "HAVE_WCSCOLL" redefined [enabled by default]
#define HAVE_WCSCOLL
^
In file included from /usr/include/python2.7/pyconfig.h:3:0,
from /usr/include/python2.7/Python.h:8,
from _mysql.c:29:
/usr/include/x86_64-linux-gnu/python2.7/pyconfig.h:911:0: note: this is the location of the previous definition
#define HAVE_WCSCOLL 1
^x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib/x86_64-linux-gnu -lmariadbclient_r -lpthread …Run Code Online (Sandbox Code Playgroud) 我想在列表中获取fetchall操作的结果,而不是元组或元组元组的元组.例如,
cursor = connection.cursor() #Cursor could be a normal cursor or dict cursor
query = "Select id from bs"
cursor.execute(query)
row = cursor.fetchall()
Run Code Online (Sandbox Code Playgroud)
现在,问题是结果行是((123,),(234,))或({'id':123},{'id':234})我要找的是(123,234)或[ 123234].如果我可以保存解析结果集,那就最好了.提前致谢
我有一个SQL数据库,我想知道你用什么命令来获取该数据库中的表名列表.
mysql-python ×10
python ×9
mysql ×5
python-2.7 ×2
cursor ×1
django ×1
macos ×1
mariadb ×1
pandas ×1
pip ×1
virtualenv ×1