Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'case.': 0.05; 'converts': 0.07; 'see:': 0.07; 'cursor': 0.09; "they've": 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'index': 0.13; '(the': 0.15; 'file,': 0.15; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'inserting': 0.16; 'lowercase': 0.16; 'message- id:@cskk.homeip.net': 0.16; 'received:202.125.174': 0.16; 'received:202.125.174.133': 0.16; 'received:boardofstudies.nsw.edu.au': 0.16; 'received:cskk.homeip.net': 0.16; 'received:edu.au': 0.16; 'received:harvey.boardofstudies.nsw.edu.au': 0.16; 'received:homeip.net': 0.16; 'received:nsw.edu.au': 0.16; 'row': 0.16; 'simpson': 0.16; 'subject:compare': 0.16; 'wrote:': 0.17; 'url:dev': 0.17; '(or': 0.18; 'lets': 0.22; 'url:02': 0.22; 'cheers,': 0.23; 'cc:2**0': 0.23; 'work.': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'values': 0.26; 'first,': 0.27; 'plain': 0.27; 'run': 0.28; 'case,': 0.29; 'error': 0.30; 'point': 0.31; 'file': 0.32; 'entry': 0.33; 'open': 0.35; 'table': 0.35; 'there': 0.35; 'but': 0.36; '(i.e.': 0.36; 'compare': 0.36; 'received:au': 0.36; 'skip:m 40': 0.36; 'test': 0.36; 'should': 0.36; 'charset:us-ascii': 0.36; 'data': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'some': 0.38; 'url:en': 0.38; 'instead': 0.39; 'where': 0.40; 'your': 0.60; 'content- disposition:inline': 0.60; 'skip:u 10': 0.60; "you've": 0.61; 'lower': 0.61; 'url:5': 0.61; 'situation': 0.62; 'solve': 0.62; 'more': 0.63; 'content,': 0.65; 'life': 0.66; 'url:0': 0.67; 'upper': 0.75; '_if_': 0.84; 'write:': 0.91 Date: Wed, 12 Dec 2012 09:43:00 +1100 From: Cameron Simpson To: Anatoli Hristov Subject: Re: MySQLdb compare lower MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) References: Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355265784 news.xs4all.nl 6879 [2001:888:2000:d::a6]:55822 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34659 On 11Dec2012 22:01, Anatoli Hristov wrote: | Excuse me for the noob question, but is there a way to compare a field | in mysql as lower() somehow? | | I have a situation where I compare the SKU in my DB and there are some | SKU that are with lowercase and some with uppercase, how can I solve | this in your opinion ? | | def Update_SQL(price, sku): | | db = MySQLdb.connect("localhost","getit","opencart", | use_unicode=True, charset="utf8") | cursor = db.cursor() | sql = "UPDATE product SET price=%s WHERE sku=%s" | cursor.execute(sql, (price, sku)) | db.commit() | db.close() Let the db do the work. Untested example: sql = "UPDATE product SET price=%s WHERE LOWER(sku)=LOWER(%s)" See: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html However I would point out that this form of the SQL requires a scan of the whole db table per update. The plain test against "sku" instead of "LOWER(sku)" lets the db use an index on "sku" to access the relevant row directly; MUCH more efficient (if you've got an index on "sku", of course). You can only run with the first, efficient, form if the sku values in the db are normalised. For example, all upper case or all lower case. Supposing that your sku has been normalised to all lower case (i.e. the data entry phase converts skus to lowercase when inserting data into the db), then you can write: sql = "UPDATE product SET price=%s WHERE sku=LOWER(%s)" which can use the index on "sku" - efficient. Here's you're normalising the test value (the %s part) to match the db content, which should alreay be lowercase. _If_ you know SKUs can always be normalised to lower case (or upper case, your call provided it is consistent), you can normalise the values in the db if they've been put in unnormalised. And then get on with your life as above. Cheers, -- Cameron Simpson Hal, open the file Hal, open the damn file, Hal open the, please Hal - Haiku Error Messages http://www.salonmagazine.com/21st/chal/1998/02/10chal2.html