Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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; 'much!': 0.05; 'retrieved': 0.05; 'schema': 0.05; 'that?': 0.05; '%s"': 0.07; 'column': 0.07; 'data:': 0.07; 'except:': 0.07; 'host,': 0.07; 'null,': 0.07; 'try:': 0.07; '%s",': 0.09; '(1,': 0.09; 'hostname': 0.09; 'keyed': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'index': 0.13; '"error': 0.16; '%d:': 0.16; '%s,': 0.16; '(%s)",': 0.16; '(%s,': 0.16; 'also:': 0.16; 'data...': 0.16; 'hits': 0.16; 'host)': 0.16; 'increment': 0.16; 'presume': 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; 'cc:2**0': 0.23; 'for?': 0.23; 'insert': 0.23; 'references': 0.23; 'cc:no real name:2**0': 0.24; 'idea': 0.24; 'host': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'values': 0.26; 'record': 0.28; 'key,': 0.29; 'unique,': 0.29; 'probably': 0.29; 'worked': 0.30; 'primary': 0.30; 'print': 0.32; 'page.': 0.33; 'skip:s 30': 0.33; "aren't": 0.33; 'like:': 0.33; 'null': 0.33; 'received:google.com': 0.34; 'needed': 0.35; 'auto': 0.35; 'sequence': 0.35; 'doing': 0.35; 'table': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'next': 0.35; 'really': 0.36; 'except': 0.36; 'but': 0.36; 'should': 0.36; 'thank': 0.36; 'does': 0.37; 'being': 0.37; 'why': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'page': 0.38; 'where': 0.40; 'your': 0.60; 'most': 0.61; "you've": 0.61; 'save': 0.61; 'provide': 0.62; 'times': 0.63; 'show': 0.63; 'visit': 0.64; 'making': 0.64; 'date,': 0.65; 'pin': 0.65; 'webpage': 0.65; '8bit%:100': 0.70; '8bit%:92': 0.70; 'saving': 0.72; 'foreign': 0.72; '(url)': 0.84; '2013': 0.84; 'cid': 0.84; 'vid': 0.84; 'dennis': 0.91; 'visits': 0.91 X-Received: by 10.49.1.70 with SMTP id 6mr1270334qek.27.1359128634017; Fri, 25 Jan 2013 07:43:54 -0800 (PST) Newsgroups: comp.lang.python Date: Fri, 25 Jan 2013 07:43:53 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=94.68.70.179; posting-account=DYJQ-woAAACEPH85Au2BhUVfFTfSfVa4 References: <88306c73-dfa2-44e1-ab0c-d90dba05be1c@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 94.68.70.179 MIME-Version: 1.0 Subject: Re: mysql solution From: Ferrous Cranus To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: , Message-ID: Lines: 248 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1359128642 news.xs4all.nl 6855 [2001:888:2000:d::a6]:48889 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37676 =CE=A4=CE=B7 =CE=A0=CE=AD=CE=BC=CF=80=CF=84=CE=B7, 24 =CE=99=CE=B1=CE=BD=CE= =BF=CF=85=CE=B1=CF=81=CE=AF=CE=BF=CF=85 2013 10:43:59 =CE=BC.=CE=BC. UTC+2,= =CE=BF =CF=87=CF=81=CE=AE=CF=83=CF=84=CE=B7=CF=82 Dennis Lee Bieber =CE=AD= =CE=B3=CF=81=CE=B1=CF=88=CE=B5: > On Thu, 24 Jan 2013 03:04:46 -0800 (PST), Ferrous Cranus >=20 > declaimed the following in >=20 > gmane.comp.python.general: >=20 >=20 >=20 > > # insert new page record in table counters or update it if already exis= ts >=20 > > try: >=20 > > cursor.execute( '''INSERT INTO counters(page, hits) VALUES(%s, %s)=20 >=20 > > ON DUPLICATE KEY UPDATE hits =3D hits + 1''', (htmlpage, 1) ) >=20 > > except MySQLdb.Error, e: >=20 > > print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] ) >=20 > > =09 >=20 > > # update existing visitor record if same pin and same host found >=20 > > try: >=20 > > cursor.execute( '''UPDATE visitors SET hits =3D hits + 1, useros =3D = %s, browser =3D %s, date =3D %s WHERE pin =3D %s AND host =3D %s''', (usero= s, browser, date, pin, host)) >=20 > > except MySQLdb.Error, e: >=20 > > print ( "Error %d: %s" % (e.args[0], e.args[1]) ) >=20 > > >=20 > > # insert new visitor record if above update did not affect a row >=20 > > if cursor.rowcount =3D=3D 0: >=20 > > cursor.execute( '''INSERT INTO visitors(hits, host, useros, browser, = date) VALUES(%s, %s, %s, %s, %s)''', (1, host, useros, browser, date) ) >=20 > >=20 >=20 >=20 >=20 > Seeing the database schema would help. At present I have no idea >=20 > what is defined as a key, what may be a foreign key, etc. >=20 >=20 >=20 > For example: you show a "counters" table in which you are saving >=20 > "hits" per page (I presume the URL is being saved). But the very next >=20 > thing you are doing is something with a hit count in a "visitors" table >=20 > which appears to be keyed by the combination of "host" and "pin" -- but >=20 > you've failed to provide "pin" on the INSERT. >=20 >=20 >=20 > Furthermore, your "visitors" table is only saving the most recent >=20 > "useros" and "browser" data... Is that what you really want -- or do you >=20 > want to log ALL users that visit the page. >=20 >=20 >=20 > Making presumptions, I'd probably have something like: >=20 >=20 >=20 > SCHEMA: >=20 >=20 >=20 > create table counters >=20 > ( >=20 > ID integer not null auto_increment primary key, >=20 > URL varchar(255) not null, >=20 > hits integer not null default 1, >=20 > unique index (URL) >=20 > ); >=20 >=20 >=20 > create table visitors >=20 > ( >=20 > ID integer not null auto_increment primary key, >=20 > counterID integer not null, >=20 > host varchar(255) not null, >=20 > userOS varchar(255) not null, >=20 > browser varchar(255) not null, >=20 > hits integer not null default 1, >=20 > lastVisit datetime not null, >=20 > foreign key (counterID) references counters (ID), >=20 > unique index (counterID, host) >=20 > ); >=20 >=20 >=20 > -=3D-=3D-=3D- >=20 >=20 >=20 > con =3D db.connection() >=20 >=20 >=20 > cur =3D con.cursor() >=20 >=20 >=20 > try: >=20 > #find the needed counter for the page URL >=20 > cur.execute("select ID from counters where URL =3D %s", (htmlpage, ) ) >=20 > data =3D cur.fetchone() #URL is unique, so should only be one >=20 > if not data: >=20 > #first time for page; primary key is automatic, hit is defaulted >=20 > cur.execute("insert into counters (URL) values (%s)", >=20 > (htmlpage,) ) >=20 > cID =3D cur.lastrowid #get the primary key value of the new record >=20 > else: >=20 > #found the page, save primary key and use it to issue hit update >=20 > cID =3D data[0] >=20 > cur.execute("update counters set hits =3D hits + 1 where ID =3D %s", >=20 > (cID,) ) >=20 >=20 >=20 > #find the visitor record for the (saved) cID and current host >=20 > cur.execute("""select ID from visitors=20 >=20 > where counterID =3D %s >=20 > and host =3D %s""", >=20 > (cID, host) ) >=20 > data =3D cur.fetchone() #cID&host are unique >=20 > if not data: >=20 > #first time for this host on this page, create new record >=20 > cur.execute("""insert into visitors=20 >=20 > (counterID, host, userOS, browser, lastVisit) >=20 > values (%s, %s, %s, %s, %s)""", >=20 > (cID, host, useros, browser, date) ) >=20 > #primary key and hits are defaulted, don't care about key >=20 > else: >=20 > #found the page, save its primary key for later use >=20 > vID =3D data[0] >=20 > #update record using retrieved vID >=20 > cur.execute("""update visitors set >=20 > userOS =3D %s, >=20 > browser =3D %s, >=20 > lastVisit =3D %s, >=20 > hits =3D hits + 1 >=20 > where ID =3D %s""", >=20 > (useros, browser, date, vID) ) >=20 >=20 >=20 > con.commit() #if we made it here, the transaction is complete >=20 >=20 >=20 > except: #blind excepts aren't "good", but you get the idea >=20 > #ANY exception needs to rollback the above sequence >=20 > con.rollback() #something failed, rollback the entire transaction >=20 > print "ERROR DURING hit counter update sequence" >=20 It worked like a charm! Thank you very much! what do you mean by that? " Furthermore, your "visitors" table is only saving the most recent=20 "useros" and "browser" data... Is that what you really want -- or do you=20 want to log ALL users that visit the page. " If the same hostname visits my webpage multiple times i only update the us= erOS, bwoswer, date information.=20 What do you mean? And also: why does the table 'visitors' ahs to have an auto increment colum= n ID what for?