白银论坛,猎奇绳艺,南雄太君
import pyodbc cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=xxx;DATABASE=xxx;UID=xxx;PWD=xxx') cursor = cnxn.cursor() cursor.execute("SELECT id FROM datatable") row = cursor.fetchone()
execute执行的时候,
有很多SQL语句用单行来写并不是很方便,所以你也可以使用三引号的字符串来写:
cursor.execute(""" select user_id, user_name from users where last_logon < '2001-01-01' and bill_overdue = 'y' """)
有中文的时候,可以:
if row: print ' '.join(row)
如果要抽取每一个内容,可以定位:
for rows in row: print row.id
假如你使用的是三引号,那么你也可以这样使用:
deleted = cursor.execute(""" delete from products where id <> 'pyodbc' """).rowcount
有些数据库(比如SQL Server)在计数时并没有产生列名,这种情况下,你想访问数据就必须使用下标。当然你也可以使用“as”关键字来取个列名
row = cursor.execute("select count(*) as user_count from users").fetchone() print '%s users' % row.user_count
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
新手学习Python2和Python3中print不同的用法
Python基于os.environ从windows获取环境变量
网友评论