英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论