Here are the latest versions of My (as in mine) SQL helper functions. Please feel free to rename them if you use them.
The SELECT helper: - def MySQLSelect(table, arglist=(), argdict={}, **kwargs):
-
"""Build an SQL SELECT command from the arguments:
-
Return a single string which can be 'execute'd.
-
arglist is a list of strings that are column names to get.
-
argdict and kwargs are two way to evaluate 'colName'=value
-
for the WHERE clause"""
-
# Allow NULL columns in the result set # pyodbc makes rows mutable so, use it!
-
a = ', '.join((arg, 'NULL')[arg is None] for arg in arglist)
-
args = argdict.copy()
-
args.update(kwargs)
-
for key, value in args.items():
-
args[key] = (str(value), repr(value))[isinstance(value, str)]
-
b = ''
-
if args:
-
b = 'WHERE %s' %' AND '.join(key + '=' + value
-
for key, value in args.items())
-
-
return ' '.join(['SELECT', (a or '*'), 'FROM', table, b])
The INSERT and UPDATE helpers now convert None to NULL: -
-
def MySQLInsert(table, argdict={}, **kwargs):
-
"""Build an SQL INSERT command from the arguments:
-
Return a single string which can be 'execute'd.
-
argdict is a dictionary of 'column_name':value items.
-
**kwargs is the same but passed in as column_name=value"""
-
args = argdict.copy() # don't modify caller dictionary!
-
args.update(kwargs)
-
keys = args.keys() # an ordered list #
-
argslist = []
-
for key in keys:
-
a = args[key] # argslist will match the order from above #
-
argslist.append(((str(a), repr(a))[isinstance(a, str)], "NULL")[a is None])
-
# wrap comma separated values in parens
-
a = '(%s)' %', '.join(field for field in keys)
-
b = '(%s)' %', '.join(argslist)
-
return ' '.join(['INSERT', table, a, 'VALUES', b])
-
-
-
def MySQLUpdate(table, valuedict, argdict={}, **kwargs):
-
"""Build an SQL SELECT command from the arguments:
-
Return a single string which can be 'execute'd.
-
valuedict is a dictionary of column_names:value to update.
-
argdict and kwargs are two way to evaluate 'colName'=value
-
for the WHERE clause."""
-
vargs = valuedict.copy()
-
for key, value in vargs.items():
-
vargs[key] = ((str(value), repr(value))[type(value) == str], "NULL")[value is None]
-
a = 'SET %s' % ', '.join(key + '=' + value
-
for key, value in vargs.items())
-
args = argdict.copy()
-
args.update(kwargs)
-
for key, value in args.items():
-
args[key] = (str(value), repr(value))[type(value) == str]
-
b = ''
-
if args:
-
b = 'WHERE %s' % ' AND '.join(key + '=' + value
-
for key, value in args.items())
-
-
return ' '.join(['UPDATE', table, a, b])
0 4782 Sign in to post your reply or Sign up for a free account.
Similar topics
by: Robert Ferrell |
last post by:
I have a style question. I have a class with a method, m1, which
needs a helper function, hf. I can put hf inside m1, or I can make it
another method of the class. The only place hf should ever...
|
by: Rahul Anand |
last post by:
Getting SQL Exception when trying to implement Connection
based Trasaction using SQL Helper class.
I am using the follwing function to execute my stored
procs:
-=-=-=-
ExecuteScalar(ByVal...
|
by: D. Shane Fowlkes |
last post by:
Hello All.
I keep asking for help with this on the www.asp.net forums and nobody seems
to be able to help. What I'm trying to accomplish is very simple. I simply
want to create a Hyperlink...
|
by: D. Shane Fowlkes |
last post by:
Hey guys. I have a Repeater and a Template. One of the dataitems calls a
helper function. The dataitem sends the record ID to the function and the
function runs a complex query and returns a...
|
by: Joe Johnston |
last post by:
I need a Browser Helper object written in VB.NET Please
point me at a good example.
Joe
MCPx3
~ Hoping this MSDN ng three day turnaround is true.
Additional info:
What is a BHO? In its...
|
by: Tran Hong Quang |
last post by:
Hello,
What is helper function concept? I am new to C.
Thanks
Tran Hong Quang
|
by: shaun roe |
last post by:
When should a function be a private member, and when simply a standalone
function in the .cpp file?
I'm in the middle of writing a class which bridges between two packages,
and so I need some...
|
by: mailforpr |
last post by:
Suppose you have a couple of helper classes that are used by 2 client
classes only. How can I hide these helper classes from other
programmers? Do you think this solution is a good idea?:
class...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |