I want to extract the status of a database. How can I do this using a Python script?
I tried to write a simple Python script, but it did not produce the desired output.
My requirement is to extract this "trunc log on chkpt, mixed log and data".
cmd = 'sp_helpdb pubs2'
res = subprocess.getoutput(cmd)
print(cmd)
name db_size owner dbid created status
------ -------- ------ ----- ------------- -------------------------------
pubs2 20.0 MB sa 4 Apr 13, 2005 trunc log on chkpt, mixed log
and data
(1 row affected)
pubs2
device_fragments size usage created free kbytes
------------------ ------- ------------- ------------------- -------
master 10.0 MB data and log Apr 13 2005 10:29AM 2304
pubs_2_dev 10.0 MB data and log Apr 13 2005 10:33AM 9888
device segment
------------- ----------------------------------------------------
master default
master logsegment
master system
pubs_2_dev default
pubs_2_dev logsegment
pubs_2_dev system
pubs_2_dev titleseg1
pubs_2_dev titleseg2
pubs_2_dev titleseg3
pubs_2_dev titleseg4
pubs_2_dev titleseg5
sp_helpdbwith no arguments as this will only generate a single result set (unlike your example which generates 3 result sets) which can then be parsed for the row where the first column is the name of the desired database ... 2) keep in mind the desired status info occurs on a single line (unlike the dual lines shown in your sample output) so you should be able to just parse/pick the last column/field on the line ...isqlcommand line tool I'd set a visible column delimiter (eg,|) and then parse for the next to last field, but I'm assuming python has a lib for parsing SQL result sets which should make it easier to either a) parse out the last column or b) parse out the 'status' column