ALTER TABLE table_name ADD UNIQUE (column_list); Lockroutertable="lock table table1 write" cur1.execute(Lockroutertable) SELECT="select id from table where ip=%s" cur1.execute(SELECT,router) result=cur1.fetchone() if result==None: INSERT="insert table1 value ('','','')" else: ID=result[0]
2)SELECT X
3) INSERT
4)解锁。
写操作时可以放到事务里面,innodb的MVCC可以保证并发事务。
如果要保证值唯一的话,可以在该字段上建立唯一索引,但是切记写操作时别使用replace,而应该使用insert。
Lockroutertable="lock table table1 write"
cur1.execute(Lockroutertable)
SELECT="select id from table where ip=%s"
cur1.execute(SELECT,router)
result=cur1.fetchone()
if result==None:
INSERT="insert table1 value ('','','')"
else:
ID=result[0]