Database statements via python but database left intact

Νίκος Αλεξόπουλος nikos.gr33k at gmail.com
Sat Oct 5 15:27:49 EDT 2013


Actually the whole code is this:

# 
=================================================================================================================
# DATABASE INSERTS -
# 
=================================================================================================================
if cookieID != 1977 and re.search( 
r'(msn|gator|amazon|yandex|reverse|who|cloudflare|fetch|barracuda|spider|google|crawl|pingdom)', 
host ) is None:

	try:
		# locate the ID of the page's URL
		cur.execute('''SELECT ID FROM counters WHERE url = %s''', page )
		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)''', page )
			cID = cur.lastrowid		#get the primary key value of the new added 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 Cookie
		cur.execute('''SELECT * FROM visitors WHERE counterID = %s and 
cookieID = %s''', (cID, cookieID) )
		data = cur.fetchone()		#cookieID is unique
			
		if not data:
			# first time visitor on this page, create new record
			cur.execute('''INSERT INTO visitors (counterID, cookieID, host, city, 
useros, browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s, 
%s)''', (cID, cookieID, host, city, useros, browser, ref, lastvisit) )
		else:
			# found the page, save its primary key for later use
			vID = data[0]
			# UPDATE record using retrieved vID
			cur.execute('''UPDATE visitors SET host = %s, city = %s, useros = %s, 
browser = %s, ref= %s, hits = hits + 1, lastvisit = %s
						   WHERE counterID = %s and cookieID = %s''', (host, city, useros, 
browser, ref, lastvisit, vID, cookieID) )
		
	except pymysql.ProgrammingError as e:
		print( repr(e) )
		sys.exit(0)

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

If at some point an error is made does that mean that no 
update/insertion happens?
PEhats that is why iam seeing no entries at all into my database tables?



More information about the Python-list mailing list