Wednesday 15 January 2014

python - How to get the last row id on update? -



python - How to get the last row id on update? -

i beginner in mysql , may fault somewhere, , not able understand how can resolved. construction of table:-

create table `nearest_product_type` ( `id` integer auto_increment not null primary key, `created` datetime not null, `modified` datetime not null, `name` varchar(15) not null unique ) ;

and code trying:-

base = mysqldb.connect (host="localhost", user = "root", passwd = "sheeshmohsin", db="points") basecursor = base.cursor() queryone = """insert nearest_product_type (name,created,modified) values (%s,%s,%s) on duplicate key update name=name """ category = "indica" valueone = (category,datetime.datetime.now(),datetime.datetime.now()) basecursor.execute(queryone, valueone) product_id = basecursor.lastrowid basecursor.close() base.commit() base.close() print product_id

on running python script, first time when category not unique, works fine, on running 1 time again same category first time, lastly row id returns 0. need id of lastly row updated.

and when checked rows in table, auto-increment working, suppose if run script 4 times, in first time when category unique id 1 , suppose unique category comes in 4th time, id assigned row 4, should 2, because sec row. how can solve this?

the on duplicate key update part here not work key auto-increment column, never duplicates.

it clause causing unexpected counts, particularly given unique setting on name.

you can seek using select max(id) nearest_product_type lastly id added.

python mysql sql mysql-python

No comments:

Post a Comment