mysql - INSERT WHERE NOT EXISTS clause using python variables -


i attempting write sql query insert rows table when script run. have been able working whilst duplicating data, unable set insert can determine whether or not record unique.

i have tried following these threads:

most efficient way sql 'insert if not exists'

python mysqldb / mysql insert ignore & checking if ignored

currently, can system add records, due frequency script ran @ database being filled same records. current code:

for post in r.get_subreddit("listentothis").get_new(limit=5): if 'youtube.com' in post.url or 'youtu.be' in post.url:     print(str(post.title))     print(str(post.url)) sql = """insert listentothis2(title,url) values ('%s', '%s')  not exists (select 1 listentothis2 url = '%s') """ % (post.title, post.url, post.url)     try:         cursor.execute(sql)         db.commit()     except:         db.rollback() 

i have tried:

sql = """insert ignore listentothis2(title,url) values ('%s', '%s') """ % (post.title, post.url) 

however, adds 3 recent records whether or not commited database already. appreciated!


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -