Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'example:': 0.03; 'exception': 0.03; '#if': 0.05; 'error:': 0.05; 'retrieved': 0.05; 'schema': 0.05; '%s"': 0.07; 'data:': 0.07; 'except:': 0.07; 'host,': 0.07; 'null,': 0.07; 'try:': 0.07; '%s",': 0.09; '(1,': 0.09; 'keyed': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'index': 0.13; '"error': 0.16; '%d:': 0.16; '%s,': 0.16; '(%s)",': 0.16; '(%s,': 0.16; 'data...': 0.16; 'hits': 0.16; 'host)': 0.16; 'presume': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'row': 0.16; 'later': 0.16; 'duplicate': 0.17; 'exists': 0.17; 'integer': 0.17; 'thu,': 0.17; 'jan': 0.18; 'appears': 0.18; 'combination': 0.22; 'help.': 0.22; 'defined': 0.22; "i'd": 0.22; 'insert': 0.23; 'references': 0.23; 'idea': 0.24; 'host': 0.24; 'values': 0.26; 'header:X-Complaints- To:1': 0.28; 'record': 0.28; 'key,': 0.29; 'unique,': 0.29; 'probably': 0.29; 'primary': 0.30; 'print': 0.32; 'page.': 0.33; 'skip:s 30': 0.33; "aren't": 0.33; 'like:': 0.33; 'null': 0.33; 'url:home': 0.33; 'to:addr:python-list': 0.33; 'needed': 0.35; 'sequence': 0.35; 'doing': 0.35; 'table': 0.35; 'something': 0.35; 'next': 0.35; 'received:org': 0.36; 'really': 0.36; 'except': 0.36; 'but': 0.36; 'should': 0.36; 'charset:us-ascii': 0.36; 'being': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'page': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'most': 0.61; "you've": 0.61; 'save': 0.61; 'provide': 0.62; 'show': 0.63; 'visit': 0.64; 'making': 0.64; 'date,': 0.65; 'pin': 0.65; 'saving': 0.72; 'foreign': 0.72; '(url)': 0.84; '2013': 0.84; 'cid': 0.84; 'vid': 0.84; 'dennis': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: mysql solution Date: Thu, 24 Jan 2013 15:43:59 -0500 Organization: > Bestiaria Support Staff < References: <88306c73-dfa2-44e1-ab0c-d90dba05be1c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-76-249-17-3.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 3.3/32.846 X-No-Archive: YES 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: 119 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1359060248 news.xs4all.nl 6977 [2001:888:2000:d::a6]:35492 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37629 On Thu, 24 Jan 2013 03:04:46 -0800 (PST), Ferrous Cranus declaimed the following in gmane.comp.python.general: > # insert new page record in table counters or update it if already exists > try: > cursor.execute( '''INSERT INTO counters(page, hits) VALUES(%s, %s) > ON DUPLICATE KEY UPDATE hits = hits + 1''', (htmlpage, 1) ) > except MySQLdb.Error, e: > print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] ) > > # update existing visitor record if same pin and same host found > try: > cursor.execute( '''UPDATE visitors SET hits = hits + 1, useros = %s, browser = %s, date = %s WHERE pin = %s AND host = %s''', (useros, browser, date, pin, host)) > except MySQLdb.Error, e: > print ( "Error %d: %s" % (e.args[0], e.args[1]) ) > > # insert new visitor record if above update did not affect a row > if cursor.rowcount == 0: > cursor.execute( '''INSERT INTO visitors(hits, host, useros, browser, date) VALUES(%s, %s, %s, %s, %s)''', (1, host, useros, browser, date) ) > Seeing the database schema would help. At present I have no idea what is defined as a key, what may be a foreign key, etc. For example: you show a "counters" table in which you are saving "hits" per page (I presume the URL is being saved). But the very next thing you are doing is something with a hit count in a "visitors" table which appears to be keyed by the combination of "host" and "pin" -- but you've failed to provide "pin" on the INSERT. Furthermore, your "visitors" table is only saving the most recent "useros" and "browser" data... Is that what you really want -- or do you want to log ALL users that visit the page. Making presumptions, I'd probably have something like: SCHEMA: create table counters ( ID integer not null auto_increment primary key, URL varchar(255) not null, hits integer not null default 1, unique index (URL) ); create table visitors ( ID integer not null auto_increment primary key, counterID integer not null, host varchar(255) not null, userOS varchar(255) not null, browser varchar(255) not null, hits integer not null default 1, lastVisit datetime not null, foreign key (counterID) references counters (ID), unique index (counterID, host) ); -=-=-=- con = db.connection() cur = con.cursor() try: #find the needed counter for the page URL cur.execute("select ID from counters where URL = %s", (htmlpage, ) ) data = cur.fetchone() #URL is unique, so should only be one if not data: #first time for page; primary key is automatic, hit is defaulted cur.execute("insert into counters (URL) values (%s)", (htmlpage,) ) cID = cur.lastrowid #get the primary key value of the new record else: #found the page, save primary key and use it to issue hit update cID = data[0] cur.execute("update counters set hits = hits + 1 where ID = %s", (cID,) ) #find the visitor record for the (saved) cID and current host cur.execute("""select ID from visitors where counterID = %s and host = %s""", (cID, host) ) data = cur.fetchone() #cID&host are unique if not data: #first time for this host on this page, create new record cur.execute("""insert into visitors (counterID, host, userOS, browser, lastVisit) values (%s, %s, %s, %s, %s)""", (cID, host, useros, browser, date) ) #primary key and hits are defaulted, don't care about key else: #found the page, save its primary key for later use vID = data[0] #update record using retrieved vID cur.execute("""update visitors set userOS = %s, browser = %s, lastVisit = %s, hits = hits + 1 where ID = %s""", (useros, browser, date, vID) ) con.commit() #if we made it here, the transaction is complete except: #blind excepts aren't "good", but you get the idea #ANY exception needs to rollback the above sequence con.rollback() #something failed, rollback the entire transaction print "ERROR DURING hit counter update sequence" -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/