Mysql таблица имеет student
имеет 2 таблицы students
и teach
Код ниже
def tbl_col_names(table):
db = {}
for i in table:
cursor.execute("select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME= '%s'" % (i))
tabledescription = cursor.fetchall()
print ('tableDesc1', tabledescription)
tableDescription1 = [i[0] for i in tabledescription]
print (tableDescription1)
db[i] = tableDescription1
with open('data.json','w') as f:
db1 = json.dumps(db)
test = (f"data ='[{db1}]'")
f.write(test)
return db
cursor.execute("SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema='databasename'")
ecords = cursor.fetchall()
print (records)
member = [i[0] for i in records]
print ('member',member)
allcolumnvalues = tbl_col_names(member)
print ('all',allcolumnvalues )
данные. json вывод ниже
data = '[{"students": ["student_no"], "teach": ["last_name", "course_no"]}]'
Как получить тип данных вместе с именем столбца
Ниже приведен запрос mysql для получения имени столбца вместе с типом данных
select COLUMN_NAME,DATA_TYPE from INFORMATION_SCHEMA.COLUMNS where table_name='student';
cursor.execute("select COLUMN_NAME,DATA_TYPE from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME= '%s'" % (i))
My ожидаемый выход data.json
ниже
data = '[{"students": {"student_no":"int"}, "teach": {"last_name":"varchar", "course_no":"int"}}]'