dbtojson.py 666 B

1234567891011121314151617181920
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. # import pymysql
  4. import mysql.connector
  5. import json
  6. db = mysql.connector.connect(host='222.73.129.15',
  7. user='root',
  8. passwd='kepaiteng!QAZ',
  9. db='eq',
  10. port=31306)
  11. # tname="buydetail"
  12. tname=input("请输入表名:")
  13. cursor = db.cursor(dictionary=True)
  14. err =cursor.execute("select * from %s limit 1"%(tname))
  15. result =cursor.fetchall()
  16. print(f"json: {json.dumps(result, indent=4, sort_keys=True, default=str)}")
  17. # write_file = open("%s.json"%(tname), "w", encoding="utf-8").write(sqlstr)
  18. print(err)
  19. db.close()