Jump to content

Pythona nd MySQL

Go to solution Solved by Mr_KoKa,

You need to connection.commit() after fetchall

So I am currently creating a script that will pull data from websites and post them to discord / twitter when it finds a new item.

 

I first it checks if the data is in the database and if not it adds it, this keeps looping until I close the script.

 

The problem is if I delete a few items from the database on the next loop the script still thinks they are in the database and I don't know why. If I stop and start the script it adds them back.

 

This function gets passed a list of lists

 

[['Product name', 'product link'], ['Product name', 'product link'],['Product name', 'product link']]

def saveData(d):
	added = 0
	for item in d:
		for i in item:
			with connection.cursor() as cursor:
				link = i[1]
				sql = "SELECT * FROM `stock` where `Link`=%s"
				cursor.execute(sql, (link,))
				result = cursor.fetchall()
				try:
					print('[!] item {} already in database'.format(result[0]['Link']))
				except:
					try:
						sql = "INSERT INTO `stock` (`Name`, `Link`, `Sizes`) VALUES (%s, %s, %s)"
						cursor.execute(sql, (i[0], i[1], i[2]))
						connection.commit()
						added += 1
						print('[+] New item: {0} added to database'.format(i[0]))
						if enableTwitter:
							tweet(i)
						if enableDiscord:
							sendDiscord(i)
					except Exception as e:
						print(e)
	print('{0} New items added to database'.format(added))

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/828750-pythona-nd-mysql/
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×