refactor db code in tc.py