0

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
3
  • I can't tell from your question what commands you typed v. what you received as output. I suggest you reformat your question. Commented Jun 17 at 15:17
  • I don't work with python so I can't comment on python-specific coding suggestions; but from a Sybase ASE perspective a few suggestions to make parsing easier ... 1) run sp_helpdb with 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 ... Commented Jun 19 at 16:06
  • 3) if I were using the isql command 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 Commented Jun 19 at 16:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.