mysql solution

Ferrous Cranus nikos.gr33k at gmail.com
Thu Jan 24 06:04:46 EST 2013


# 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) )

======================================================

I;am now convinced the hash solution isn't reversible and also isn't unique.
I'am trying the database oriented solution.

pin column = 5-digit integer Primary Key.

When i'am inserting a new record to table counters, a sequenced number is crated as pin. Thats works ok.


But when i try to Update or Insert into the visitors table the 'pin' comunn is needed to to identify the rrecord for which iam trying to update like here:

================================
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))
================================

how is the mysql statement is going to find the 'pin' to update the specific record.
And also in here:

================================
if cursor.rowcount == 0:
		cursor.execute( '''INSERT INTO visitors(pin, hits, host, useros, browser, date) VALUES(%s, %s, %s, %s, %s, %s)''', (pin, 1, host, useros, browser, date) )
================================

'pin' column's value is also need to make insert





More information about the Python-list mailing list