def get_hit_count(page_name): page_name = str(page_name) stmt = """INSERT INTO cookbook.hitcount (path, hits) VALUES('%s', LAST_INSERT_ID(1)) ON DUPLICATE KEY UPDATE hits = LAST_INSERT_ID(hits+1) """ %page_name print("""<p>%s</p>""" % stmt) conn = cookbook.connect() #conn.autocommit = True cursor = conn.cursor() cursor.execute("""INSERT INTO cookbook.hitcount (path, hits) VALUES(%s, LAST_INSERT_ID(1)) ON DUPLICATE KEY UPDATE hits = LAST_INSERT_ID(hits+1)""", (page_name,)) conn.commit() cursor.close() #conn.close() #conn = cookbook.connect() cursor = conn.cursor() cursor.execute("""SELECT hits from cookbook.hitcount WHERE path='%s'""" %page_name) cursor.execute("""SELECT LAST_INSERT_ID()""") result = cursor.fetchone() cursor.close() conn.close() return result
(seq INT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (seq)) ''') cursor.close() conn.commit() except mysql.connector.Error as e: print("Cannot create seqdiag table") print("Error: %s" % e) def gen_seq_val(cursor): try: cursor.execute("INSERT INTO seqdiag (seq) VALUES(NULL)") except mysql.connector.Error as e: print("Cannot create AUTO_INCREMENT value") print("Error: %s" % e) conn = cookbook.connect() init_table(conn) cursor = conn.cursor() gen_seq_val(cursor) seq = cursor.lastrowid print("seq: %d" % seq) cursor.execute("SET @x = LAST_INSERT_ID(48)") seq = cursor.lastrowid print("seq after SET via lastrowid: %d" % seq) cursor.execute("SELECT LAST_INSERT_ID()") row = cursor.fetchone() seq = row[0] print("seq after SET via LAST_INSERT_ID(): %d" % seq)
#!/usr/bin/python # booksales.py: show how to use LAST_INSERT_ID(expr) import mysql.connector import cookbook conn = cookbook.connect() try: #@ _UPDATE_COUNTER_ cursor = conn.cursor() cursor.execute(''' INSERT INTO booksales (title,copies) VALUES('The Greater Trumps',LAST_INSERT_ID(1)) ON DUPLICATE KEY UPDATE copies = LAST_INSERT_ID(copies+1) ''') count = cursor.lastrowid cursor.close() conn.commit() #@ _UPDATE_COUNTER_ print("count: %d" % count) except mysql.connector.Error as e: print("Oops, the statement failed") print("Error: %s" % e) conn.close()
import cgi import cookbook from cookbook_webutils import * title = "Query Output Display - Lists" # Print content type header, blank line that separates # headers from page body, and first part of page print("Content-Type: text/html") print("") print("<html>") print("<head><title>%s</title></head>" % title) print("<body>") conn = cookbook.connect() # Display some lists "manually", printing tags as items are fetched print("<p>Ordered list:</p>") #@ _ORDERED_LIST_INTERTWINED_ stmt = "SELECT item FROM ingredient ORDER BY id" cursor = conn.cursor() cursor.execute(stmt) print("<ol>") for (item,) in cursor: if item is None: # handle possibility of NULL item item = "" print("<li>%s</li>" % cgi.escape(item, 1)) print("</ol>") cursor.close()
#!/usr/local/bin/python import os, sys import cookbook sys.path.append('./lib') import cookbook_utils import cookbook_webutils db = cookbook.connect() # Mysql_tut.connect_to_db(db1Host, db1UName, db1Pwd, db1Name) acc_info = cookbook_utils.get_enumorset_info(db, 'cookbook', 'cow_order', 'accessories') if acc_info['default'] is None: acc_def = "" else: acc_def = acc_info['default'].split(',') # Print header, blank line, and initial part of page print('''Content-Type: text/html <html> <head><title>HTML forms driven by Python</title></head> <body> <p>Trying different forms:</p> ''')