如何去掉“(),”从 Python、PyODBC、SQL 返回?
我获取了一堆需要使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先:
然后放弃你可怕的 while 循环:
First of all:
Then abandon your terrible while loop:
我相信问题是您正在从列表中访问元组,因此需要指定列表中的位置和元组中的位置:
输出:
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:
outputs: