473,809 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help fixing a Query using string formating

12 New Member
just for info I am using BOA constructor and python 2.3

I am writting a query which works if i break it up as follows (and it also works if I run it in SQL view of access)
Expand|Select|Wrap|Line Numbers
  1. stn_name = whatever
  2. xf_name =  whatever2
  3.  
  4. Query_MDB_grid1 ="""SELECT EMS_StationName, EMS_XFName,           EMS_XFMRName, FromBusName, FromKV, ToBusName, ToKV, CKT,firstBusnumber, secondbusnumber  
  5.                                 FROM MarketDBTransformers 
  6.                                 WHERE EMS_StationName like ' """
  7.  
  8. Query_MDB_grid1 = Query_MDB_grid1 + stn_name + "'" + " and EMS_XFName like '"  
  9.  
  10. Query_MDB_grid1 = Query_MDB_grid1 + xf_name + "'"
  11.  
But if I try to combine the entire query as follows it does not see stn_name and
xf_name as variable but as text within quotes ' " + xf_name+" '
Expand|Select|Wrap|Line Numbers
  1. Query_MDB_grid1 ="""SELECT EMS_StationName, EMS_XFName, EMS_XFMRName, FromBusName, FromKV, ToBusName, ToKV, CKT,firstBusnumber, secondbusnumber  
  2.                                 FROM MarketDBTransformers 
  3.                                 WHERE EMS_StationName like '"+stn_name+"' and EMS_XFName like '"+xf_name+"' """
  4.  
I really appreciate the help
Oct 17 '07 #1
6 1476
bartonc
6,596 Recognized Expert Expert
And I will appreciate your using CODE tags. Instructions on their use is on the right hand side, every time you post. Thanks.

(I have to add these manually when you neglect using them.
There is a site rule requiring their use; see our Posting Guidelines) Thank you.
Oct 17 '07 #2
bartonc
6,596 Recognized Expert Expert
String formating is the answer. String formats use the % operator to show which variable (and its type - in this case %s means string type) goes into the string when it is "formated":
Expand|Select|Wrap|Line Numbers
  1. stn_name = 'Nevada'
  2. xf_name =  'ME'
  3.  
  4. Query_MDB_grid1 ="""SELECT EMS_StationName, EMS_XFName,           EMS_XFMRName, FromBusName, FromKV, ToBusName, ToKV, CKT,firstBusnumber, secondbusnumber  
  5.                                 FROM MarketDBTransformers 
  6.                                 WHERE EMS_StationName like ' """
  7.  
  8. Query_MDB_grid1 = Query_MDB_grid1 + stn_name + "'" + " and EMS_XFName like '"  
  9.  
  10. Query_MDB_grid1 = Query_MDB_grid1 + xf_name + "'"
  11. print Query_MDB_grid1
  12.  
  13.  
  14. Query_MDB_grid1 ="""SELECT EMS_StationName, EMS_XFName, EMS_XFMRName, FromBusName, FromKV, ToBusName, ToKV, CKT,firstBusnumber, secondbusnumber  
  15.                                 FROM MarketDBTransformers 
  16.                                 WHERE EMS_StationName like '%s' and EMS_XFName like '%s' """ %(stn_name, xf_name)
  17. print Query_MDB_grid1
Oct 17 '07 #3
bartonc
6,596 Recognized Expert Expert
I've published a set of SQL helper functions in the Articles section which make query formation a lot more readable and much easier to change. For example:
Expand|Select|Wrap|Line Numbers
  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.     a = ', '.join(arg for arg in arglist)
  8.     args = argdict.copy()
  9.     args.update(kwargs)
  10.     for key, value in args.items():
  11.         args[key] = (str(value), repr(value))[isinstance(value, str)]
  12.     b = ''
  13.     if args:
  14.         b = 'WHERE %s' % ' AND '.join(key + '=' + value
  15.                                       for key, value in args.items())
  16.  
  17.     return ' '.join(['SELECT', (a or '*'), 'FROM', table, b])
  18.  
  19. columns = ('EMS_StationName', 'EMS_XFName', 'EMS_XFMRName', 'FromBusName',
  20.            'FromKV', 'ToBusName', 'ToKV', 'CKT', 'firstBusnumber',
  21.            'secondbusnumber')
  22. table = 'MarketDBTransformers'
  23.  
  24. where = """WHERE EMS_StationName like '%s' and EMS_XFName like '%s' """ %(stn_name, xf_name)
  25.  
  26. print MySQLSelect(table, columns) + where
Oct 17 '07 #4
bartonc
6,596 Recognized Expert Expert
I've published a set of SQL helper functions in the Articles section which make query formation a lot more readable and much easier to change.
Looking at that, though, I see that they need updating to the current versions which support the conversion of None to NULL in inserts and updates.
Oct 17 '07 #5
ApoorvaDesai
12 New Member
Sorry for not using the code tags....will keep it in mind next time....thanks for the replies
Oct 18 '07 #6
bartonc
6,596 Recognized Expert Expert
Looking at that, though, I see that they need updating to the current versions which support the conversion of None to NULL in inserts and updates.
I have updated the SQL helpers in this thread. And my SQL Connection Manager has been updated here.
Oct 18 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1668
by: Dan Gidman | last post by:
Okay all I have a problem. I have two list of adresses and phone numbers. I do not have control over the contents of the lists The only unique field between the two is the phone number. I need to be able to inner join the two lists on phone number. This would normally be straigt forward but the problem is that they are formated different and one of them does't even have a control on the formating. *Phone numbers are US phone...
5
1925
by: Alicia | last post by:
Hello everyone based on the data, I created a union query which produces this. SELECT ,,, 0 As ClosedCount FROM UNION SELECT ,, 0 AS OpenedCount, FROM ORDER BY , ;
12
3440
by: strict9 | last post by:
Hello all, I'm writing several queries which need to do various string formating, including changing a phone number from (123) 456-7890. After some problem with data mismatches, I finally got it to work only to see that it takes 30-60 seconds to run the query, instead of the usual .5 seconds when I use a query without a function. Here is the code for the function. I call it using
1
1221
by: stevenrec | last post by:
I'm back. Do not use queries too often and have trouble formating a field: misc: if((!)='2';"Normal)"; if((!)='8';"Extra") Basically want it to check the number in the <order_typefield and if it is 2, type Normal and if it is 8 type Extra. Thought I could get it to work with IIF, but have not had any luck yet.
17
2041
by: so many sites so little time | last post by:
all right so the script is pretty simple it goes it retrives what the id of the post is and it lets you edit it well no it doesnt. now if you go to www.kirewire.com/pp2/index/php you will see a number 1 that is the value of collumn home_id which is set to auto increment ect ect but this script which gets the id of home_id of a row and lets you edit it does not work for somereason...
8
4995
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to string and print it. but i want to use the String.Format() method if possible to do it.
9
2048
by: JAF | last post by:
I need help with the following format: 1. Paragragh goes here and text wraps or indents several spaces on second and subsequent lines. 2. Paragragh goes here and text wraps or indents several spaces on second and subsequent lines. More lines continue here. 3. Paragragh goes here and text wraps or indents
1
2827
by: Randy Volkart | last post by:
I'm trying to fix a glitch in a complex access database, and have a fairly complex problem... unless there's some obscure easy fix I don't know being fairly new with Access. Basically, the area I'm trying to fix includes a form which takes entered data, concatenates it into a VB string to form an SQL query, then launches a report with information from the query. Several tables are linked in the query, but the key ones for this problem...
2
1751
by: sitko | last post by:
Hi, I'm in the process of converting a VB.net program into a C program so it can run on a unix like machine. I've been moving along at a nice pace, but this conversion has stumped me. I need a function to take two arguments:(a double number which may or may not be an integer, and an integer which will be the number of digits to store after the decimal place) it needs to turn this into a string which is always 10 characters long. if the...
0
10640
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10387
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7662
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.