似乎无法将行插入MySQL表中 [英] Can't seem to insert rows into a MySQL table

查看:78
本文介绍了似乎无法将行插入MySQL表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用insert向MySQL表添加一行。以下是代码:


connection = MySQLdb.connect(host =" localhost",user =" root",passwd =" pw",

db =" japanese")

cursor = connection.cursor()

cursor.execute(" INSERT INTO edict(kanji,kana,含义)VALUES( %s,%s,

%s)",(a,b,c)

connection.close()


运行后,表上的SELECT *显示没有添加新行。使用MySQL客户端添加

行可以正常工作。没有Python脚本。

没有引发异常或任何输出。执行为1后,

游标的行数为1,并且对于每次完成插入,表的'auto_increment值

都会增加。有人可以帮忙吗?我是一个相当新的人,所以我担心这对我来说是一个愚蠢的疏忽。谢谢

你。

I''m trying to add a row to a MySQL table using insert. Here is the code:

connection = MySQLdb.connect(host="localhost", user="root", passwd="pw",
db="japanese")
cursor = connection.cursor()
cursor.execute("INSERT INTO edict (kanji, kana, meaning) VALUES (%s, %s,
%s)", ("a", "b", "c") )
connection.close()

After running, a SELECT * on the table shows no new rows added. Adding
rows using the MySQL client works fine. With the Python script, nothing.
There are no exceptions raised or any output at all. The rowcount of the
cursor is 1 after the execute is 1 and the table''s auto_increment value
is increased for each insert done. Can anybody help? I''m fairly new to
MySQL so I''m afraid its going to be a stupid oversight on my part. Thank
you.

推荐答案

grumfish写道:
grumfish wrote:
connection = MySQLdb .connect(host =" localhost",user =" root",passwd =" pw",
db =" japanese")
cursor = connection.cursor()
cursor.execute(" INSERT INTO edict(kanji,kana,含义)VALUES(%s,%s,
%s)",(a,b"," c") )
connection.close()
connection = MySQLdb.connect(host="localhost", user="root", passwd="pw",
db="japanese")
cursor = connection.cursor()
cursor.execute("INSERT INTO edict (kanji, kana, meaning) VALUES (%s, %s,
%s)", ("a", "b", "c") )
connection.close()




只是一个猜测在黑暗中 (我不使用MySQL):是提交隐含的,或者

你自己要加吗?


-pu



Just a guess "in the dark" (I don''t use MySQL): is "commit" implicit, or
do you have to add it yourself?

-pu


grumfish写道:

执行为1后,
grumfish wrote:
The rowcount of the
光标的行数为1,并且对于每次插入,表的'auto_increment值
都会增加。
cursor is 1 after the execute is 1 and the table''s auto_increment value
is increased for each insert done.




如果auto_increment增加了,那么行似乎插入了

。你确定问题不在你的SELECT尝试吗?

只是一个猜测,但似乎我第一次使用MySQLdb,我被

混淆了需要做一个fetchall() (或者fetchone()或

fetchmany())执行SELECT后。


这样的事情(未经测试) :


result = cursor.execute(SELECT * FROM edict WHERE kanji =''a'')

print result.fetchall()


HTH,
Steve P.



If the auto_increment is increased, then it seems like the row was
inserted. Are you sure the problem is not with your SELECT attempt?
Just a guess, but it seems like the first time I used MySQLdb, I was
confused by the need to do a "fetchall()" (or "fetchone()" or
"fetchmany()") after executing the SELECT.

Something like this (not tested):

result = cursor.execute(SELECT * FROM edict WHERE kanji = ''a'')
print result.fetchall()

HTH,
Steve P.


2005年3月12日星期六18:24:10 GMT,grumfish < no **** @ nowhere.com>

在comp.lang.python中声明了以下内容:
On Sat, 12 Mar 2005 18:24:10 GMT, grumfish <no****@nowhere.com>
declaimed the following in comp.lang.python:
我正在尝试添加一个使用insert将行转换为MySQL表。以下是代码:

connection = MySQLdb.connect(host =" localhost",user =" root",passwd =" pw",
db =" japanese" )
cursor = connection.cursor()
cursor.execute(" INSERT INTO edict(kanji,kana,含义)VALUES(%s,%s,
%s)", (a,b,c))
connection.close()

正在使用的表的定义是什么?独特的指数等


我的测试,使用以下测试表定义:


mysql>显示来自法令的栏目;

+ --------- + ------------------ + ------ + ----- + --------- + ---------------- +

|领域|输入|空|钥匙|默认|额外的|

+ --------- + ------------------ + ------ + --- - + --------- + ---------------- +

|汉字| char(50)| | | | |

|假名| char(50)| | | | |

|意义| char(50)| | | | |

| ID | int(10)unsigned | | PRI | NULL | auto_increment |

+ --------- + ------------------ + ------ + --- - + --------- + ---------------- +

4行(0.17秒)


和这段代码:


- = - = - = - = - = - = - = - = -

import MySQLdb


cn = MySQLdb.connect(user =" wulfraed",

passwd =" tr3th4mk4r",

db =" test",$ / b $ b host =" localhost")

cr = cn.cursor()


res = cr.execute(" insert into edict(kanji,kana,meaning)values(%s,

%s,%s)",

(" a" ;,b,c))


打印res


## cr.close()

##

## cr = cn.cursor()

res = cr.execute(" select * from edict")


打印res


for cr.fetchall():

print rw


cr.close()

cn.close()

- = - = - = - = - = - = - = -


给出以下内容(保留每次运行时都添加一行)


G:\ My Documents> python t.py

1

5

('''',''b'',''c'',1L)

('''',''b'', '''',2L)

('''',''b'',''c'',3L)

(''a' ',''b'',''c'',4L)

('''',''b'',''c'',5L)


G:\我的文件>

- ========================== ======================== ============<
wl ***** @ ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG<
wu******@dm.net | Bestiaria支持人员<
========================================= ========= ============<
主页:< http://www.dm.net/~wulfraed/> <
溢出页面:< http://wlfraed.home.netcom.com/> <
I''m trying to add a row to a MySQL table using insert. Here is the code:

connection = MySQLdb.connect(host="localhost", user="root", passwd="pw",
db="japanese")
cursor = connection.cursor()
cursor.execute("INSERT INTO edict (kanji, kana, meaning) VALUES (%s, %s,
%s)", ("a", "b", "c") )
connection.close()
What is the definition of the table in use? Unique indices, etc.

My test, using the following test table definition:

mysql> show columns from edict;
+---------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+----------------+
| kanji | char(50) | | | | |
| kana | char(50) | | | | |
| meaning | char(50) | | | | |
| ID | int(10) unsigned | | PRI | NULL | auto_increment |
+---------+------------------+------+-----+---------+----------------+
4 rows in set (0.17 sec)

and this code:

-=-=-=-=-=-=-=-=-
import MySQLdb

cn = MySQLdb.connect(user="wulfraed",
passwd="tr3th4mk4r",
db="test",
host="localhost")
cr = cn.cursor()

res = cr.execute("insert into edict(kanji, kana, meaning) values (%s,
%s, %s)",
("a", "b", "c"))

print res

##cr.close()
##
##cr = cn.cursor()

res = cr.execute("select * from edict")

print res

for rw in cr.fetchall():
print rw

cr.close()
cn.close()
-=-=-=-=-=-=-=-

gives the following (it keeps adding a row each time I run)

G:\My Documents>python t.py
1
5
(''a'', ''b'', ''c'', 1L)
(''a'', ''b'', ''c'', 2L)
(''a'', ''b'', ''c'', 3L)
(''a'', ''b'', ''c'', 4L)
(''a'', ''b'', ''c'', 5L)

G:\My Documents>
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <



这篇关于似乎无法将行插入MySQL表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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