By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,035 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

newbie python mysql query

P: 4
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
Share this Question
Share on Google+
6 Replies


bartonc
Expert 5K+
P: 6,596
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

P: 4
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
Expert 5K+
P: 6,596
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

P: 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
Expert 5K+
P: 6,596
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

P: 4
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.