Guest User

Untitled

a guest
Nov 17th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. import fdb
  2. import json
  3. import sys
  4. import decimal
  5. import datetime
  6.  
  7. def js(val):
  8. if type(val) == int:
  9. return val
  10. if type(val) == str:
  11. return val
  12. if val is None:
  13. return val
  14. if type(val) == decimal.Decimal:
  15. return str(val)
  16. if type(val) == datetime.datetime:
  17. return val.isoformat()
  18. raise Exception(type(val))
  19.  
  20. con = fdb.connect(dsn='sdr.fdb', user='sysdba', password='masterkey')
  21.  
  22. cur = con.cursor()
  23. cur.execute("SELECT a.RDB$RELATION_NAME FROM RDB$RELATIONS a WHERE RDB$SYSTEM_FLAG=0")
  24.  
  25. tables = [row[0].strip() for row in cur.fetchall()]
  26.  
  27. db={}
  28.  
  29. for table in tables:
  30. db[table] = {}
  31. cur.execute("select rdb$field_name from rdb$relation_fields where rdb$relation_name='{}' order by rdb$field_position".format(table))
  32.  
  33. db[table]['cols']=[head[0].strip() for head in cur.fetchall()]
  34.  
  35. cur.execute("select * from {}".format(table))
  36.  
  37. db[table]['rows']=[[js(field) for field in row] for row in cur.fetchall()]
  38.  
  39. json.dump(db, sys.stdout)
Add Comment
Please, Sign In to add comment