1.使用","来消除print输出的默认换行符,如:

def query(cur):

	row = cur.fetchone()

	set_len = len(row)

	while row:

  for i in range(0,set_len): #Note:range is from 0 to set_len-1

  	print row[i],      #Note:use ',' to reduce the endl by print

  print ""

  row = cur.fetchone()


2.在引用另一个模块,并且要使用另外一个模块中的成员的时候,类、方法等,必须前面加上模块名,

如在my_class模块中有一个my_class类,现在引用进来并且使用my_class类来创建一个对象的时候:

cluster = my_class.my_class() #remember to add the module name

Python导入模块的方法有两种:import module 和 from module import,区别是前者所有导入的东西使用时需加上模块名的


限定,而后者不要。但是一般情况下还是使用第一种方法,这样可以明确某个方法属于哪一个模块,并且可以避免不同模块中


的名字冲突


3.可以在一个函数中抛出异常,然后在使用这个函数的地方捕捉这个异常,如:

def func(cursor,sql):

	try:

  cursor.execute(sql)

  return cursor

	except pyodbc.Error,e:

  #print e.args[0]

  raise e


sql = "select * from xxx"

try:

	func(cursor,sql)

	print "unexpectly comes here!"

except pyodbc.Error,e:

	print e.args[0]

	print e.args[1]

finally:

	cursor.close()

	conn.close()