Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.04; 'subject:help': 0.08; 'cursor': 0.09; 'except:': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows': 0.09; 'try:': 0.09; 'runs': 0.10; 'language.': 0.14; 'changes': 0.15; 'disconnect': 0.16; 'iteration': 0.16; 'missing?': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'seconds.': 0.16; 'language': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'commit': 0.19; 'skip:f 30': 0.19; 'command': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'query': 0.26; 'code:': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'code': 0.31; 'getting': 0.31; 'exceptions': 0.31; 'seemingly': 0.31; 'strip': 0.31; 'subject:some': 0.31; 'file': 0.32; 'skip:m 30': 0.32; 'text': 0.33; 'open': 0.33; 'skip:# 10': 0.33; 'connection': 0.35; 'except': 0.35; 'prepare': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; 'method': 0.36; 'possible': 0.36; 'should': 0.36; 'minimum': 0.38; 'server': 0.38; 'to:addr:python- list': 0.38; 'anything': 0.39; 'expect': 0.39; 'delete': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'matter': 0.61; "you're": 0.61; 'back': 0.62; "you'll": 0.62; "you've": 0.63; 'our': 0.64; 'connecting': 0.64; 'charset:windows-1252': 0.65; 'smith': 0.68; 'records': 0.73; 'bare': 0.84; 'idiom': 0.84; 'apparent': 0.91; 'mistake': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Newbie needing some help Date: Fri, 08 Aug 2014 22:54:27 +0100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-78-146-1-50.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.0 In-Reply-To: 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: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1407534891 news.xs4all.nl 2901 [2001:888:2000:d::a6]:52131 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 5137 X-Received-Body-CRC: 688916665 Xref: csiph.com comp.lang.python:75908 On 08/08/2014 20:07, Matt Smith wrote: > I am trying to write a program that will loop through a text file and > delete rows in a mysql database. > > It seemingly runs but I don't see anything getting deleted in the db. > Is there anything apparent that I am missing? > > This is the code: > #!/usr/bin/python > import mysql.connector > # > f=open('/home/smithm/email-list.txt', 'r') The modern idiom is:- with open(...) as f: but that doesn't matter here. > for line in f: > # > # Open database connection > db = mysql.connector.connect(user="xx", password="xx", > host="localhost", database="xx") > > # prepare a cursor object using cursor() method > cursor = db.cursor() Are you aware that you're connecting to your db and setting up the cursor in every iteration of the loop? > > # Prepare SQL query to DELETE required records > sql = "DELETE FROM tblc_users WHERE user_email=%s, % (line)" > try: > # Execute the SQL command > cursor.execute(sql) > # Commit your changes in the database > db.commit() > except: This bare except is masking every possible thing that can go wrong. Strip it out and I expect you'll find your mistake in seconds. Once you've corrected your code add back in the bare minimum number of exceptions that you really should catch. > # Rollback in case there is any error > db.rollback() > > # disconnect from server > db.close() > > -- > Matthew Smith > -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence