从python使用mysql时,INSERT语句不是INSERTING [英] INSERT statements not INSERTING when using mysql from python

查看:80
本文介绍了从python使用mysql时,INSERT语句不是INSERTING的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有人可以提供帮助,但我有一个奇怪的问题。我有一个PSP(Spyce)脚本,它可以调用很多数据库来填充数据库。他们

所有工作没有任何问题,除了一个声明。


我首先连接到数据库...


self.con = MySQLdb.connect(user = username,passwd = password)

self.cursor = self.con.cursor()

self.cursor.execute (SET max_error_count = 0)


创建所有必要的表...


self.cursor.execute("创建数据库,如果不是EXISTS" + name)

self.cursor.execute(" USE" + name)


self.cursor.execute( CREATE TABLE IF NOT NOT EXISTS networks(SM

varchar(20),DMC int,DM varchar(50),NOS int,OS varchar(50),NID

varchar(20))


然后我在

各种表中的各种不同循环中执行多个insert语句,所有表都没问题,并导致多个表

条目。下面的一个也执行了很多次。而且似乎

与其余的相同。打印语句输出到th e浏览器

窗口,并反复出现,因此必须反复调用查询




print"< ; p>< b>执行SQL查询< / b>< p>"

self.cursor.execute(" INSERT INTO networks VALUES('''',''" ; + i +

'',''c'',''2'','''',''f'',''g'')")

打印"< p>< b> SQL查询已执行< / b>< p>"


我有,用于调试,设置i作为计数器变量。


没有给出错误,但是最终数据库中出现的唯一条目

是INSERT的最终执行声明(我的最后一个

值)


我怀疑这对任何人来说都是模糊的,但是如果

任何人都有任何想法我会非常感激的:-)


如果我能访问可能是
$ b的mysql查询日志,我发现它$ b help,但我不确定如何使用python启用MysQL的日志记录。


干杯,


Ben

I don''t know whether anyone can help, but I have an odd problem. I have
a PSP (Spyce) script that makes many calls to populate a database. They
all work without any problem except for one statement.

I first connect to the database...

self.con = MySQLdb.connect(user=username, passwd =password)
self.cursor = self.con.cursor()
self.cursor.execute("SET max_error_count=0")

All the neccesary tables are created...

self.cursor.execute("CREATE DATABASE IF NOT EXISTS "+name)
self.cursor.execute("USE "+name)

self.cursor.execute("CREATE TABLE IF NOT EXISTS networks (SM
varchar(20),DMC int,DM varchar(50),NOS int,OS varchar(50),NID
varchar(20))

Then I execute many insert statements in various different loops on
various tables, all of which are fine, and result in multiple table
entries. The following one is executed many times also. and seems
identical to the rest. The print statements output to the browser
window, and appear repeatedly, so the query must be being called
repeatedly also:

print "<p><b>SQL query executing</b><p>"
self.cursor.execute("INSERT INTO networks VALUES (''a'','' "+i+"
'',''c'',''2'',''e'',''f'',''g'')")
print "<p><b>SQL query executed</b><p>"

I have, for debugging, set "i" up as a counter variable.

No errors are given, but the only entry to appear in the final database
is that from the final execution of the INSERT statement (the last
value of i)

I suspect that this is to vague for anyone to be able to help, but if
anyone has any ideas I''d be really grateful :-)

It occured to me that if I could access the mysql query log that might
help, but I was unsure how to enable logging for MysQL with python.

Cheers,

Ben

推荐答案

这很奇怪......


如果我将完全相同的Insert语句放在elswhere中程序它

按预期工作。

这表明它永远不会在旧的位置上运行,而是

语句的任何一方它正在印刷...

Ben写道:
Well that''s odd...

If I place the exact same Insert statement elswhere in the program it
works as intended.
That would suggest it is never being run in its old position, but the
statements either side of it are printing...
Ben wrote:

I不知道是否有人可以提供帮助,但我有一个奇怪的问题。我有一个PSP(Spyce)脚本,它可以调用很多数据库来填充数据库。他们

所有工作没有任何问题,除了一个声明。


我首先连接到数据库...


self.con = MySQLdb.connect(user = username,passwd = password)

self.cursor = self.con.cursor()

self.cursor.execute (SET max_error_count = 0)


创建所有必要的表...


self.cursor.execute("创建数据库,如果不是EXISTS" + name)

self.cursor.execute(" USE" + name)


self.cursor.execute( CREATE TABLE IF NOT NOT EXISTS networks(SM

varchar(20),DMC int,DM varchar(50),NOS int,OS varchar(50),NID

varchar(20))


然后我在

各种表中的各种不同循环中执行多个insert语句,所有表都没问题,并导致多个表

条目。下面的一个也执行了很多次。而且似乎

与其余的相同。打印语句输出到眉毛ser

窗口,并且反复出现,因此必须反复调用查询




print"< p>< b>执行SQL查询< / b>< p>"

self.cursor.execute(" INSERT INTO networks VALUES('''',''" + i +"

'',''c'',''''','''',''''',''g'')")

打印"< p>< b> SQL查询已执行< / b>< p>"


我有,用于调试,设置" I"作为计数器变量。


没有给出错误,但是最终数据库中出现的唯一条目

是INSERT的最终执行声明(我的最后一个

值)


我怀疑这对任何人来说都是模糊的,但是如果

任何人都有任何想法我会非常感激的:-)


如果我能访问可能是
$ b的mysql查询日志,我发现它$ b help,但我不确定如何使用python启用MysQL的日志记录。


干杯,


Ben
I don''t know whether anyone can help, but I have an odd problem. I have
a PSP (Spyce) script that makes many calls to populate a database. They
all work without any problem except for one statement.

I first connect to the database...

self.con = MySQLdb.connect(user=username, passwd =password)
self.cursor = self.con.cursor()
self.cursor.execute("SET max_error_count=0")

All the neccesary tables are created...

self.cursor.execute("CREATE DATABASE IF NOT EXISTS "+name)
self.cursor.execute("USE "+name)

self.cursor.execute("CREATE TABLE IF NOT EXISTS networks (SM
varchar(20),DMC int,DM varchar(50),NOS int,OS varchar(50),NID
varchar(20))

Then I execute many insert statements in various different loops on
various tables, all of which are fine, and result in multiple table
entries. The following one is executed many times also. and seems
identical to the rest. The print statements output to the browser
window, and appear repeatedly, so the query must be being called
repeatedly also:

print "<p><b>SQL query executing</b><p>"
self.cursor.execute("INSERT INTO networks VALUES (''a'','' "+i+"
'',''c'',''2'',''e'',''f'',''g'')")
print "<p><b>SQL query executed</b><p>"

I have, for debugging, set "i" up as a counter variable.

No errors are given, but the only entry to appear in the final database
is that from the final execution of the INSERT statement (the last
value of i)

I suspect that this is to vague for anyone to be able to help, but if
anyone has any ideas I''d be really grateful :-)

It occured to me that if I could access the mysql query log that might
help, but I was unsure how to enable logging for MysQL with python.

Cheers,

Ben


Ben写道:
Ben wrote:

我不知道是否有人可以提供帮助,但我有一个奇怪的问题。我有一个PSP(Spyce)脚本,它可以调用很多数据库来填充数据库。他们

所有工作没有任何问题,除了一个声明。


我首先连接到数据库...


self.con = MySQLdb.connect(user = username,passwd = password)

self.cursor = self.con.cursor()

self.cursor.execute (SET max_error_count = 0)


创建所有必要的表...


self.cursor.execute("创建数据库,如果不是EXISTS" + name)

self.cursor.execute(" USE" + name)


self.cursor.execute( CREATE TABLE IF NOT NOT EXISTS networks(SM

varchar(20),DMC int,DM varchar(50),NOS int,OS varchar(50),NID

varchar(20))


然后我在

各种表中的各种不同循环中执行多个insert语句,所有表都没问题,并导致多个表

条目。下面的一个也执行了很多次。而且似乎

与其余的相同。打印语句输出到眉毛ser

窗口,并且反复出现,因此必须反复调用查询




print"< p>< b>执行SQL查询< / b>< p>"

self.cursor.execute(" INSERT INTO networks VALUES('''',''" + i +"

'',''c'',''''','''',''''',''g'')")

打印"< p>< b> SQL查询已执行< / b>< p>"


我有,用于调试,设置" I"作为计数器变量。


没有给出错误,但是最终数据库中出现的唯一条目

是INSERT的最终执行声明(我的最后一个

值)


我怀疑这对任何人来说都是模糊的,但是如果

任何人都有任何想法我会非常感激的:-)


如果我能访问可能是
$ b的mysql查询日志,我发现它$ b help,但我不确定如何使用python启用MysQL的日志记录。


干杯,


Ben
I don''t know whether anyone can help, but I have an odd problem. I have
a PSP (Spyce) script that makes many calls to populate a database. They
all work without any problem except for one statement.

I first connect to the database...

self.con = MySQLdb.connect(user=username, passwd =password)
self.cursor = self.con.cursor()
self.cursor.execute("SET max_error_count=0")

All the neccesary tables are created...

self.cursor.execute("CREATE DATABASE IF NOT EXISTS "+name)
self.cursor.execute("USE "+name)

self.cursor.execute("CREATE TABLE IF NOT EXISTS networks (SM
varchar(20),DMC int,DM varchar(50),NOS int,OS varchar(50),NID
varchar(20))

Then I execute many insert statements in various different loops on
various tables, all of which are fine, and result in multiple table
entries. The following one is executed many times also. and seems
identical to the rest. The print statements output to the browser
window, and appear repeatedly, so the query must be being called
repeatedly also:

print "<p><b>SQL query executing</b><p>"
self.cursor.execute("INSERT INTO networks VALUES (''a'','' "+i+"
'',''c'',''2'',''e'',''f'',''g'')")
print "<p><b>SQL query executed</b><p>"

I have, for debugging, set "i" up as a counter variable.

No errors are given, but the only entry to appear in the final database
is that from the final execution of the INSERT statement (the last
value of i)

I suspect that this is to vague for anyone to be able to help, but if
anyone has any ideas I''d be really grateful :-)

It occured to me that if I could access the mysql query log that might
help, but I was unsure how to enable logging for MysQL with python.

Cheers,

Ben



不确定这会有所帮助,但提交在哪里?我不使用MySQL,但是大多数SQL引擎需要提交。

Johnf

Not sure this will help but where is the "commit"? I don''t use MySQL but
most SQL engines require a commit.
Johnf



Ben写道:

Ben wrote:

那很奇怪......


如果我确切地说同样的插入声明elswhere在程序中

按预期工作。

这表明它永远不会在旧位置运行,但

声明任何一方都在打印...


Ben写道:
Well that''s odd...

If I place the exact same Insert statement elswhere in the program it
works as intended.
That would suggest it is never being run in its old position, but the
statements either side of it are printing...
Ben wrote:

我不知道是否有人可以帮忙,但我有一个奇怪的问题。我有一个PSP(Spyce)脚本,它可以调用很多数据库来填充数据库。他们

所有工作没有任何问题,除了一个声明。


我首先连接到数据库...


self.con = MySQLdb.connect(user = username,passwd = password)

self.cursor = self.con.cursor()

self.cursor.execute (SET max_error_count = 0)


创建所有必要的表...


self.cursor.execute("创建数据库,如果不是EXISTS" + name)

self.cursor.execute(" USE" + name)


self.cursor.execute( CREATE TABLE IF NOT NOT EXISTS networks(SM

varchar(20),DMC int,DM varchar(50),NOS int,OS varchar(50),NID

varchar(20))


然后我在

各种表中的各种不同循环中执行多个insert语句,所有表都没问题,并导致多个表

条目。下面的一个也被多次执行。并且看起来和其他人一样.b
与其他人相同。 ts输出到浏览器

窗口,并反复出现,所以必须反复调用查询




打印"< p>< b>执行SQL查询< / b>< p>"

self.cursor.execute(" INSERT INTO networks VALUES(''''', ''" + i +"

'',''c'',''''','''',''f'',''g'') ")

打印"< p>< b> SQL查询已执行< / b>< p>"


我有,为了调试,设置i作为计数器变量。


没有给出错误,但是最终数据库中出现的唯一条目

是INSERT的最终执行声明(我的最后一个

值)


我怀疑这对任何人来说都是模糊的,但是如果

任何人都有任何想法我会非常感激的:-)


如果我能访问可能是
$ b的mysql查询日志,我发现它$ b help,但我不确定如何使用python启用MysQL的日志记录。


干杯,


Ben
I don''t know whether anyone can help, but I have an odd problem. I have
a PSP (Spyce) script that makes many calls to populate a database. They
all work without any problem except for one statement.

I first connect to the database...

self.con = MySQLdb.connect(user=username, passwd =password)
self.cursor = self.con.cursor()
self.cursor.execute("SET max_error_count=0")

All the neccesary tables are created...

self.cursor.execute("CREATE DATABASE IF NOT EXISTS "+name)
self.cursor.execute("USE "+name)

self.cursor.execute("CREATE TABLE IF NOT EXISTS networks (SM
varchar(20),DMC int,DM varchar(50),NOS int,OS varchar(50),NID
varchar(20))

Then I execute many insert statements in various different loops on
various tables, all of which are fine, and result in multiple table
entries. The following one is executed many times also. and seems
identical to the rest. The print statements output to the browser
window, and appear repeatedly, so the query must be being called
repeatedly also:

print "<p><b>SQL query executing</b><p>"
self.cursor.execute("INSERT INTO networks VALUES (''a'','' "+i+"
'',''c'',''2'',''e'',''f'',''g'')")
print "<p><b>SQL query executed</b><p>"

I have, for debugging, set "i" up as a counter variable.

No errors are given, but the only entry to appear in the final database
is that from the final execution of the INSERT statement (the last
value of i)

I suspect that this is to vague for anyone to be able to help, but if
anyone has any ideas I''d be really grateful :-)

It occured to me that if I could access the mysql query log that might
help, but I was unsure how to enable logging for MysQL with python.

Cheers,

Ben



好​​吧,它似乎是某种自动提问问题。当我从数据库断开连接时,我已经关闭了
autocommit off,总线已提交。

出于某种原因,虽然所有其他声明似乎没问题,但是多个

那个循环中的陈述并不是每个人都有的。目前我已经开始自动提交了自动提交,这已经解决了问题,但它减慢了

,所以我会尝试修复它: - )


Ben

Well, it would appear to be some kind of autocommit problem. I had
autocommit off, bus committed when I disconnected from the database.
For some reason although all the other statements seemed ok, multiple
statements in that loop were''nt every commiting. For the moment I''ve
turned autocommit on and that has sorted things out, but it slows
things down too so I''ll try to fix it :-)

Ben


这篇关于从python使用mysql时,INSERT语句不是INSERTING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆