469,592 Members | 1,807 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

newbie python mysql query

Hi, I am new to both python and mysql and have a problem.
Expand|Select|Wrap|Line Numbers
  1. c.execute('load data infile %s into table Thursday fields terminated by ",";',csv_file)
  2.  
For some reason (and I've tried a lot !!) I am unable to figure out how to use a variable where "Thursday" is, this line works as is and has no problem with the csv_file variable. Any help would be much appreciated.
Aug 10 '07 #1
6 1775
bartonc
6,596 Expert 4TB
Hi, I am new to both python and mysql and have a problem.
Expand|Select|Wrap|Line Numbers
  1. c.execute('load data infile %s into table Thursday fields terminated by ",";',csv_file)
  2.  
For some reason (and I've tried a lot !!) I am unable to figure out how to use a variable where "Thursday" is, this line works as is and has no problem with the csv_file variable. Any help would be much appreciated.
Expand|Select|Wrap|Line Numbers
  1. >>> strVar = 'Thursday'
  2. >>> print "today is %s" %strVar
  3. today is Thursday
  4. >>> 
Hope that's what you are looking for.
Aug 10 '07 #2
Expand|Select|Wrap|Line Numbers
  1. >>> strVar = 'Thursday'
  2. >>> print "today is %s" %strVar
  3. today is Thursday
  4. >>> 
Hope that's what you are looking for.
Thank you for the quick reply.

I need it to work in the line:

c.execute('load data infile %s into table Thursday fields terminated by ",";',csv_file)

but no matter what I try eg

c.execute('load data infile %s into table %s fields terminated by ",";',csv_file,%day)

it doesn't work
Aug 10 '07 #3
bartonc
6,596 Expert 4TB
Thank you for the quick reply.

I need it to work in the line:

c.execute('load data infile %s into table Thursday fields terminated by ",";',csv_file)

but no matter what I try eg

c.execute('load data infile %s into table %s fields terminated by ",";',csv_file,%day)

it doesn't work
The number of formats ('%') need to match the number of arguments. For multiple arguments the line would look like:
Expand|Select|Wrap|Line Numbers
  1. "Here is some %s with a number which equals %d" %('text', 5)
It would be very helpful if you post more that just one line.

And note the use of CODE tags - instructions are on the right hand side while you are replying.

Thanks.
Aug 10 '07 #4
The number of formats ('%') need to match the number of arguments. For multiple arguments the line would look like:
Expand|Select|Wrap|Line Numbers
  1. "Here is some %s with a number which equals %d" %('text', 5)
It would be very helpful if you post more that just one line.

And note the use of CODE tags - instructions are on the right hand side while you are replying.

Thanks.
here are some of the variables and the function

Expand|Select|Wrap|Line Numbers
  1.  
  2. # find what days today and tomorrow are
  3. now=datetime.datetime.now()
  4. add_one_day=datetime.timedelta(days=1)
  5. today=datetime.datetime.now().strftime("%A")
  6. tomorrow=(now+add_one_day).strftime("%A")
  7.  
  8. # loads csv files from above into db
  9.  
  10. def sql_load_data():
  11.     try:
  12.         db = MySQLdb.connect(host=host, user=user, passwd=passwd ,db=db_name)
  13.         c = db.cursor()
  14.     except:
  15.         logger.critical("unable to access database")
  16.         sys.exit()
  17.     for csv_file in csv_files_r:
  18.         csv_file=csv_file.strip("\n")
  19.         try:
  20.              c.execute('load data infile %s into table today fields terminated by ",";',csv_file)
  21.         except TypeError:
  22.              logger.warning("got type error")
  23.              continue
  24.         except csv.Error:
  25.              logger.warning("bad csv record, skipping ....")
  26.              continue
  27.     c.close()
  28.     db.commit()
  29.     db.close()
  30.  
  31.  
I need to pass in the variable "today" as the table name eg Thursday
because I need it to load the data into the table named after the current day.
Aug 10 '07 #5
bartonc
6,596 Expert 4TB
here are some of the variables and the function

Expand|Select|Wrap|Line Numbers
  1.  
  2. # find what days today and tomorrow are
  3. now=datetime.datetime.now()
  4. add_one_day=datetime.timedelta(days=1)
  5. today=datetime.datetime.now().strftime("%A")
  6. tomorrow=(now+add_one_day).strftime("%A")
  7.  
  8. # loads csv files from above into db
  9.  
  10. def sql_load_data():
  11.     try:
  12.         db = MySQLdb.connect(host=host, user=user, passwd=passwd ,db=db_name)
  13.         c = db.cursor()
  14.     except:
  15.         logger.critical("unable to access database")
  16.         sys.exit()
  17.     for csv_file in csv_files_r:
  18.         csv_file=csv_file.strip("\n")
  19.         try:
  20.              c.execute('load data infile %s into table today fields terminated by ",";',csv_file)
  21.         except TypeError:
  22.              logger.warning("got type error")
  23.              continue
  24.         except csv.Error:
  25.              logger.warning("bad csv record, skipping ....")
  26.              continue
  27.     c.close()
  28.     db.commit()
  29.     db.close()
  30.  
  31.  
I need to pass in the variable "today" as the table name eg Thursday
because I need it to load the data into the table named after the current day.
Expand|Select|Wrap|Line Numbers
  1. #
  2.              c.execute('load data infile %s into table today fields terminated by ",";' %today, csv_file)  # the var goes just after the closing quote
Sorry that I didn't see that in isolation. Context makes a big difference.

Check out the Articles section for some cool GUI examples using the same tools that you are using.
Aug 10 '07 #6
Expand|Select|Wrap|Line Numbers
  1. #
  2.              c.execute('load data infile %s into table today fields terminated by ",";' %today, csv_file)  # the var goes just after the closing quote
Sorry that I didn't see that in isolation. Context makes a big difference.

Check out the Articles section for some cool GUI examples using the same tools that you are using.
Thanks for your help. I managed to fudge it by.

Expand|Select|Wrap|Line Numbers
  1. c.execute('load data infile %s into table ' + today + ' fields terminated by ",";',(csv_file))
  2.  
:-)
Aug 10 '07 #7

Post your reply

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

Similar topics

2 posts views Thread by aum | last post: by
1 post views Thread by Yong Wang | last post: by
10 posts views Thread by callmebill | last post: by
4 posts views Thread by pmcgover | last post: by
5 posts views Thread by yoyoz | last post: by
2 posts views Thread by Iain Adams | last post: by
reply views Thread by Edwin.Madari | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.