如何去掉“(),”从 Python、PyODBC、SQL 返回?

发布于 2024-09-16 23:35:34 字数 1351 浏览 4 评论 0原文

我获取了一堆需要使用 Python 和 PyODBC 更新的 SQL ID:

import pyodbc
cnxn = pyodbc.connect('DSN=YesOne;PWD=xxx')
cursor = cnxn.cursor()
cursor.execute("SELECT ID FROM One.dbo.Two WHERE STATUS = 'OnGoing' AND ID = ''")

我有第二位更新 ID 的代码:

cursor.execute("Update One.dbo.Two SET STATUS = 'Completed', Modified = 'Today' WHERE ID = '1051'")

问题是当我查看之前在 Python 中获取的 ID 时,我得到:

row = cursor.fetchall()
f row:
    print row

[(1016, ), (1017, ), (1019, ), (1020, ), (1021, ), (1025, ), (1026, ), (1027,),(1029,),(1048,),(1049, )]

或者

if row:
    print row[3]

(1020,)

我只需要这个数字,这样我就可以运行脚本的第二部分:

WHERE ID = '1051'"

部分。我尝试过:

count = len(row)
while count > 0:
    newrow = row[count-1]
    print 'SELECT ID FROM One.dbo.Two WHERE ID = ' + str(newrow)
    count = count-1

它给了我:

从 One.dbo.Two 中选择 ID,其中 ID = (1049, )
从 One.dbo.Two 中选择 ID,其中 ID = (1048, )
等等...

我尝试过:

str(row[1]).lstrip('(),')

并且我得到了:

'1017,)'

如何从 ID 中删除字符以便重新使用该 ID?

谢谢,

阿德里安

I grabbed a bunch of SQL IDs I need to update with Python and PyODBC:

import pyodbc
cnxn = pyodbc.connect('DSN=YesOne;PWD=xxx')
cursor = cnxn.cursor()
cursor.execute("SELECT ID FROM One.dbo.Two WHERE STATUS = 'OnGoing' AND ID = ''")

I have a second bit of code that updates the IDs:

cursor.execute("Update One.dbo.Two SET STATUS = 'Completed', Modified = 'Today' WHERE ID = '1051'")

The problem is when I view the IDs grabbed in Python earlier I get either:

row = cursor.fetchall()
f row:
    print row

[(1016, ), (1017, ), (1019, ), (1020,
), (1021, ), (1025, ), (1026, ),
(1027, ), (1029, ), (1048, ), (1049,
)]

or

if row:
    print row[3]

(1020, )

I need just the number so I can run my second part of the script for the:

WHERE ID = '1051'"

part. I tried with:

count = len(row)
while count > 0:
    newrow = row[count-1]
    print 'SELECT ID FROM One.dbo.Two WHERE ID = ' + str(newrow)
    count = count-1

and it gave me:

SELECT ID FROM One.dbo.Two WHERE ID = (1049, )
SELECT ID FROM One.dbo.Two WHERE ID = (1048, )
etc...

And I tried:

str(row[1]).lstrip('(),')

and I got:

'1017, )'

How do I strip the characters from the ID so I can reuse the ID?

Thanks,

Adrian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亢潮 2024-09-23 23:35:34

首先:

rows = [x[0] for x in cursor.fetchall()]

然后放弃你可怕的 while 循环:

for row in rows:
  print 'SELECT ID FROM One.dbo.Two WHERE ID = %s' % row

First of all:

rows = [x[0] for x in cursor.fetchall()]

Then abandon your terrible while loop:

for row in rows:
  print 'SELECT ID FROM One.dbo.Two WHERE ID = %s' % row
合约呢 2024-09-23 23:35:34

我相信问题是您正在从列表中访问元组,因此需要指定列表中的位置和元组中的位置:

row = cursor.fetchall()
f row:
    print row
    print row[3]
    print row[3][0]

输出:

[(1016, ), (1017, ), (1019, ), (1020, ), (1021, )]
(1020, )
1020

I believe the issue is that you are accessing a tuple from within a list, so need to specify location in list and location in tuple:

row = cursor.fetchall()
f row:
    print row
    print row[3]
    print row[3][0]

outputs:

[(1016, ), (1017, ), (1019, ), (1020, ), (1021, )]
(1020, )
1020
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文