将数据传递给一个使用pyodbc的变量。

huangapple go评论65阅读模式
英文:

Pass data to a varaible with pyodbc

问题

我连接到了一个数据库,里面有一些数据。

在这里,我打印出了ID为2的人的姓名

cursor.execute('Select Name from Customers Where Id = 2')
for row in cursor:
    print(row)

这个人的名字是"Alex",但在代码中被打印成了这样

('Alex', )

我想将这个名字"Alex"传递给一个变量,然后当我打印它时,我希望它看起来像这样

print(name)

答案

Alex

我该如何做呢?

英文:

I connected to a database and there are some datas in it.

In here I print the name of the person whose id is 2

cursor.execute('Select Name from Customers Where Id = 2')
for row in cursor:
    print(row)

The name of the person is "Alex" it this code is being printed like this

('Alex', )

I want to pass this name "Alex" to a varaible and then when i print it I want it to be look like

print(name)

answer

Alex

How can I do it?

答案1

得分: 3

print(row[0])

这是一个元组,所以从中提取第一个元素。

英文:
print(row[0])

It is a tuple, so extract so first element from it.

huangapple
  • 本文由 发表于 2023年1月8日 23:10:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定