SQL帮助器函数更新

以下是My(和我的一样)SQL助手函数的最新版本。如果您使用它们,请随意重命名它们。SELECT助手:

选择 | 换行 | 行号
  1. def MySQLSelect(table, arglist=(), argdict={}, **kwargs):
  2.     """Build an SQL SELECT command from the arguments:
  3.     Return a single string which can be 'execute'd.
  4.     arglist is a list of strings that are column names to get.
  5.     argdict and kwargs are two way to evaluate 'colName'=value
  6.     for the WHERE clause"""
  7.     # Allow NULL columns in the result set # pyodbc makes rows mutable so, use it!
  8.     a = ', '.join((arg, 'NULL')[arg is None] for arg in arglist)
  9.     args = argdict.copy()
  10.     args.update(kwargs)
  11.     for key, value in args.items():
  12.         args[key] = (str(value), repr(value))[isinstance(value, str)]
  13.     b = ''
  14.     if args:
  15.         b = 'WHERE %s' %' AND '.join(key + '=' + value
  16.                                      for key, value in args.items())
  17.  
  18.     return ' '.join(['SELECT', (a or '*'), 'FROM', table, b])

INSERT和UPDATE助手现在将None转换为NULL:

选择 | 换行 | 行号
  1.  
  2. def MySQLInsert(table, argdict={}, **kwargs):
  3.     """Build an SQL INSERT command from the arguments:
  4.     Return a single string which can be 'execute'd.
  5.     argdict is a dictionary of 'column_name':value items.
  6.     **kwargs is the same but passed in as column_name=value"""
  7.     args = argdict.copy()   # don't modify caller dictionary!
  8.     args.update(kwargs)
  9.     keys = args.keys() # an ordered list #
  10.     argslist = []
  11.     for key in keys:
  12.         a = args[key]  # argslist will match the order from above #
  13.         argslist.append(((str(a), repr(a))[isinstance(a, str)], "NULL")[a is None])
  14.     # wrap comma separated values in parens
  15.     a = '(%s)' %', '.join(field for field in keys)
  16.     b = '(%s)' %', '.join(argslist)
  17.     return ' '.join(['INSERT', table, a, 'VALUES', b])
  18.  
  19.  
  20. def MySQLUpdate(table, valuedict, argdict={}, **kwargs):
  21.     """Build an SQL SELECT command from the arguments:
  22.     Return a single string which can be 'execute'd.
  23.     valuedict is a dictionary of column_names:value to update.
  24.     argdict and kwargs are two way to evaluate 'colName'=value
  25.     for the WHERE clause."""
  26.     vargs = valuedict.copy()
  27.     for key, value in vargs.items():
  28.         vargs[key] = ((str(value), repr(value))[type(value) == str], "NULL")[value is None]
  29.     a = 'SET %s' % ', '.join(key + '=' + value
  30.                                   for key, value in vargs.items())
  31.     args = argdict.copy()
  32.     args.update(kwargs)
  33.     for key, value in args.items():
  34.         args[key] = (str(value), repr(value))[type(value) == str]
  35.     b = ''
  36.     if args:
  37.         b = 'WHERE %s' % ' AND '.join(key + '=' + value
  38.                                       for key, value in args.items())
  39.  
  40.     return ' '.join(['UPDATE', table, a, b])

标签: python

添加新评论