473,395 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 2002
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

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

Similar topics

2
by: aum | last post by:
Hi, I'm looking for an object-relational layer, which can wrap a MySQL database into highly pythonic objects. There seem to be dozens of packages which provide some of this functionality, but...
1
by: Yong Wang | last post by:
> Hi: > We have a database system. In the database, we have an > attribute called date0, which contains date data (format: > i. e. 2004-08-12). I wrote a script to fetch the data > in a...
10
by: callmebill | last post by:
I'm getting my feet wet with making Python talk to MySQL via ODBC. I started on Windows, and it went smoothly enough due to the ODBC stuff that apparently is native to Python at least on windows...
4
by: pmcgover | last post by:
I enjoyed Paul Barry's September article in Linux Journal entitled, "Web Reporting with MySQL, CSS and Perl". It provides a simple, elegant way to use HTML to display database content without any...
14
by: Ben | last post by:
I don't know whether anyone can help, but I have an odd problem. I have a PSP (Spyce) script that makes many calls to populate a database. They all work without any problem except for one...
3
by: cuties | last post by:
Hi all.... i'm very new to this programming language. i'm required to fulfill this task in the company i'm doing my practical. i hope i can get guide for my problem... Here is the script i...
5
by: yoyoz | last post by:
Help!!!! i am newbie to php, i was trying to establish the connection to another machine (solaris) so that i can retrieve data from the database stored inside there using my own PC. for your...
2
by: Iain Adams | last post by:
Hi, I am new to python. I have been having trouble using the MysqlDB. I get an error pointing from the line cursor.execute("UPDATE article SET title = %s, text = %s WHERE id = %u",...
0
by: Edwin.Madari | last post by:
-----Original Message----- statement prepared first and executed many times with exectemany - db API http://www.python.org/dev/peps/pep-0249/ inline statemets can be exeucuted only. hope that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.