Jump to content

Send data from MySQL db to Python Code

HCStrike

I am looking to create a discord bot for my server in Python using the Discord.py library. The bot should be able to assign user roles depending on their user type which is set in a MySQL db. (Just some background to the story)

 

I have looked into triggers to trigger the script when a MySQL db is updated, for example when they put their discord username and ID into a field and update their profile. I am a bit stuck on how I could send the discord username and ID along with their user type to the python script on the trigger. Any ideas how I could do this?

 

Thanks

i7-6700k @ 4ghz

GTX1070

16GB Ram

Asus Maximus VIII Hero

500GB SSD

1TB HDD

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, geo3 said:

Do you know how I'd be able to "convert" certain sections of a row within a MySQL table into an argument for the python script?

 

i7-6700k @ 4ghz

GTX1070

16GB Ram

Asus Maximus VIII Hero

500GB SSD

1TB HDD

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, HCStrike said:

Do you know how I'd be able to "convert" certain sections of a row within a MySQL table into an argument for the python script?

 

Arguments are passed as strings. If you need a different data type convert it inside python.

 

 

A 3d option would be to have python directly query the database. Like this http://mysql-python.sourceforge.net/MySQLdb.html

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, geo3 said:

Arguments are passed as strings. If you need a different data type convert it inside python.

 

 

A 3d option would be to have python directly query the database. Like this http://mysql-python.sourceforge.net/MySQLdb.html

Sorry I wasn't very clear, I was meaning how could I go about getting the data out of the MySQL db on an UPDATE trigger and get it into an argument for the python script?

i7-6700k @ 4ghz

GTX1070

16GB Ram

Asus Maximus VIII Hero

500GB SSD

1TB HDD

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, geo3 said:

Arguments are passed as strings.

This is not true in python you can pass almost anything as an argument, strings, numbers, classes, functions all work.

 

7 hours ago, HCStrike said:

Sorry I wasn't very clear, I was meaning how could I go about getting the data out of the MySQL db on an UPDATE trigger and get it into an argument for the python script?

https://stackoverflow.com/questions/23382499/run-python-script-on-database-event

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

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, vorticalbox said:

This is not true in python you can pass almost anything as an argument, strings, numbers, classes, functions all work.

 

Nope. They're strings. 

 

Run this with any amount of arguments. 

import sys
for i in sys.argv:
    print type(i)

It will only out put a bunch of <type 'str'>

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, geo3 said:

Nope. They're strings. 

 

Run this with any amount of arguments. 


import sys
for i in sys.argv:
    print type(i)

It will only out put a bunch of <type 'str'>

My bad I though you meant an argument to a function.

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

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, vorticalbox said:

In this example 

DELIMITER @@
 
CREATE TRIGGER Test_Trigger 
AFTER INSERT ON MyTable 
FOR EACH ROW 
BEGIN
 DECLARE cmd CHAR(255);
 DECLARE result int(10);
 SET cmd=CONCAT('sudo /home/sarbac/hello_world ','Sarbajit');
 SET result = sys_exec(cmd);
END;
@@
DELIMITER ;

Would 'Sarbajit' be an argument? So I could put say the 'Discord ID' and 'User Type' field as 2 arguments and they would be passed as the actual users information?

i7-6700k @ 4ghz

GTX1070

16GB Ram

Asus Maximus VIII Hero

500GB SSD

1TB HDD

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, HCStrike said:

Would 'Sarbajit' be an argument? 

That's correct.

 

Although if mysql allows data fields to be passed this way. It may just be a static string. If not they you could use what I said in my earlier post and have python query the database. The you trigger just needs to initiate the script and not worry about passing it data.

Link to comment
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

×