Tracking any change in an table on SQL Server With Python - python

How are you today?
I'm a newbie in Python. I'm working with SQL server 2014 and Python 3.7. So, my issue is: When any change occurs in a table on DB, I want to receive a message (or event, or something like that) on my server (Web API - if you like this name).
I don't know how to do that with Python.
I have an practice (an exp. maybe). I worked with C# and SQL Server, and in this case, I used "SQL Dependency" method in C# to solve that. It's really good!
Have something like that in Python? Many thank for any idea, please!
Thank you so much.

I do not know many things about SQL. But I guess there are tools for SQL to detect those changes. And then you could create an everlasting loop thread using multithreading package to capture that change. (Remember to use time.sleep() to block your thread so that It wouldn't occupy the CPU for too long.) Once you capture the change, you could call the function that you want to use. (Actually, you could design a simple event engine to do that). I am a newbie in Computer Science and I hope my answer is correct and helpful. :)

Related

Automate a manual task using Python

I have a question and hope someone can direct me in the right direction; Basically every week I have to run a query (SSMS) to get a table containing some information (date, clientnumber, clientID, orderid etc) and then I copy all the information and that table and past it in a folder as a CSV file. it takes me about 15 min to do all this but I am just thinking can I automate this, if yes how can I do that and also can I schedule it so it can run by itself every week. I believe we live in a technological era and this should be done without human input; so I hope I can find someone here willing to show me how to do it using Python.
Many thanks for considering my request.
This should be pretty simple to automate:
Use some database adapter which can work with your database, for MSSQL the one delivered by pyodbc will be fine,
Within the script, connect to the database, perform the query, parse an output,
Save parsed output to a .csv file (you can use csv Python module),
Run the script as the periodic task using cron/schtask if you work on Linux/Windows respectively.
Please note that your question is too broad, and shows no research effort.
You will find that Python can do the tasks you desire.
There are many different ways to interact with SQL servers, depending on your implementation. I suggest you learn Python+SQL using the built-in sqlite3 library. You will want to save your query as a string, and pass it into an SQL connection manager of your choice; this depends on your server setup, there are many different SQL packages for Python.
You can use pandas for parsing the data, and saving it to a ~.csv file (literally called to_csv).
Python does have many libraries for scheduling tasks, but I suggest you hold off for a while. Develop your code in a way that it can be run manually, which will still be much faster/easier than without Python. Once you know your code works, you can easily implement a scheduler. The downside is that your program will always need to be running, and you will need to keep checking to see if it is running. Personally, I would keep it restricted to manually running the script; you could compile to an ~.exe and bind to a hotkey if you need the accessibility.

Automating DOSbox application

I have a very old DOS application which I would like to automate. Like there are keypresses and such which if automated will help a lot as I might have to run the program over a hundred times manually.
My question seems to be very similar to this one but the solutions offered there are not very useful for me, plus it is over nine years old
Automating old DOS application using Python
Only big difference between this question and mine is that I have no option other than DOSbox for doing this. This application is set up on a lot of computers, and all the people using the application know how to use DOSBox. Migrating to Virtualbox would be a pain and very time-consuming.
I was thinking maybe if I could mechanize this somehow in python using xautomaton or uinput, but I haven't been able to figure out exactly how. The application will be running on Ubuntu primarily.
To give an idea of the application, I am attaching a screenshot:
The solution does not necessarily need to be in python. Any other language would work. Any help is appreciated.
I figured this out. Although this does not use python, to do this, I just captured the windowid of DOSbox and sent all the key presses there using xdotool. Here is an example:
wid=$(xdotool search --class DOSbox)
xdotool key --window $wid m t 5 Return Return i
Which will type "mt5", then press enter twice and then type "i"
The series of keypresses can be stored in a string or a file and called iteratively each time this has to be run. If there is a better method to do this, please feel free to answer.

communication between raspberryPi, gsm module and database

i'am just learning about raspberryPi. how can i communicate raspberryPi with database, so that can raspberryPi send message/sms using gsm module?? and how the Raspberry Pi read databases mysql from different tables that could be sent in a single sentence using gsm?
I hope any suggestion given to me from friends in here
Thanks
Nice first question adew!
To improve the next future question you make please read: https://stackoverflow.com/help/how-to-ask
I will take the time to try answer your question, because I suspect that there is more people out there with the same ideas as you.
To start with
GSM module communication:
This question has many different answers and the answer depends on what hardware you will use. There is no standard in this area.
To give you some options;
https://www.sparkfun.com/products/9533
https://www.adafruit.com/products/2636
Sparkfun will provide you with a detailed datashet & Adafruit even has a detailed tutorial. The drawback with the Adafruit tutorial is that it assumes that you are using a arduino (Not a big problem)
Both of the above modules are capable of UART communication, something you easily can access with a RaspberryPI and
http://pyserial.readthedocs.io/en/latest/pyserial_api.html
https://github.com/WiringPi/WiringPi-Python
Personally I have used WiringPI before and it is easy to start with and has a bunch of active maintainers on IRC.
To wrap up the GSM-part. Consult the manual for that particular device you are going to use. That is the only way you will know what to send and what you will receive from the module.
Database querys:
Are you using a existing database or are you going to create your own from scratch?
Even that part of the question is to broad.
If you are going to make a new database just for this kind of application I might suggest using
https://www.sqlalchemy.org/
It will give you a nice little ORM tool. It will make it easy for you to insert and fetch data that probably matches how you want to work with the data.
If the database is a existing MySQL database
https://dev.mysql.com/doc/connector-python/en/connector-python-examples.html
Well again, consult the manual.
To wrap this up.
This was a very lengthy post, that gave you almost nothing about almost everything. This is why people down-votes a questions like the one you made. Most people dont have the time to give this kind of answers.
I suspect that the next question you make will be related to any of the above topics.
Try to be precise
Give a piece of code that exposes the problem you have
One problem at a time
Happy hacking & have a look at https://raspberrypi.stackexchange.com/ !
//David Bern

Rewrite a procedural script in clean OOP Python style

I've successfully written a Powershell script which:
query AD to obtain the list of computer
query every computer through WMI to obtain software/hardware information
insert the collected data into a MySQL database.
The script works fine but I don't like how it's implemented. It's procedural and there is a lot of code duplication, causing a mess everytime I need to change something.
Now, what I would like to ask you is: what is the cleanest way to implement it in python using OOP? I've thought something similar to this (pseudocode):
Class ADquery
function get_computers( UO ): return a list of computers in the specified UO
Class Computer
constructor( computername )
function query(): connect to the computer and pull the info through WMI
function print(): print the collected info to the console (debug)
property RAM
property CPU
...
Questions:
In order to save the collected data into a Database, do I have to create another object (e.g. Database) and pass the Computer object to him or add a member function to the Computer class (e.g. save_db() ) ?
If I go for the second option, that wouldn't cause a massive number of MySQL connections when I'm dealing with multiple objects?
Thanks a lot and sorry for my bad English
That architecture looks reasonable to me.
You could do either, I'm not sure it really makes a huge difference with a small application like this.
Potentially. Depending on how it's implemented you could get a lot of connections going on. If you're doing a reasonable number of inserts I'd stick them in a list and insert all at once, if that's possible with your code.
You could also grab an Object Oriented Design book from the internet or your local bookstore, e.g. Rumbaugh et al.. I would also recommend reading up on Design Patterns, e.g. the book by Gamma et. al.. I'm currently doing that, and it is really helpful to look at standard patterns of how to solve a particular problem to shape your thought process about object oriented programming.
ps Your English isn't bad at all (note that I'm also not a native speaker ;)).

Rookie Python-questions

Is it possible to make python run on your homepage? I know, this is a really stupid question but please don't pick on me for my stupidity :)
If it is possible, how? Do you have to upload/install the executing part of Python to you website using FTP? or...?
Edit: Just found out my provider does not support python and that shell access is completely restricted. Problem solved :)
Everything depends on the hosting provider you use for your homepage -- do they offer Python among their services, and, if so, what version, and how do you write server-side scripts to use it (is it CGI-only, or...?) -- if not, or the version / deployment options disappoint, what do they allow in terms of giving you shell access and running long-time processes?
It's impossible for us to judge any of these aspects, because every single one of them depends on your hosting provider, and absolutely none of them depends on Python itself!-)
Yes, you can. I don't know exactly how but I know it is possible. Mabye look into this website:
https://trinket.io/
This website lets you do this. I sent them a message to see how they do it so I will update this to let you know after they respond.
Python is a scripting language, though it is used gracefully for building back end web applications.

Categories

Resources