新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

怎么用Python将一个DB的表结构同步给另一个DB

这篇文章主要介绍“怎么用Python将一个DB的表结构同步给另一个DB”,在日常操作中,相信很多人在怎么用Python将一个DB的表结构同步给另一个DB问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用Python将一个DB的表结构同步给另一个DB”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

创新互联建站服务项目包括红古网站建设、红古网站制作、红古网页制作以及红古网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,红古网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到红古省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

具体的实现代码如下所示:
# coding:utf-8import pyMySQL
dbDict = {"test1":"l-beta.test1"}dbUser = "test"dbPassword = "123456"
class DBUtils():    def __init__(self):        self.conn = pymysql.connect(dbDict['test1'], dbUser, dbPassword)        self.cursor = self.conn.cursor()
   def dbSelect(self, sql):        print("------------------------------------")        print(sql)        resultList = []        self.cursor.execute(sql)        result = self.cursor.fetchall()        columns = self.cursor.description        for val in result:            tempDict = {}            for cloNum in range(len(columns)):                tempDict[str(columns[cloNum][0])] = val[cloNum]            resultList.append(tempDict)        print("---------------------打印查询结果----------------------")        print(resultList)        self.dbClose()        return resultList
   def dbExcute(self, sql):        print(sql)        self.cursor.execute(sql)        self.dbClose()
   def dbClose(self):        self.conn.commit()        self.cursor.close()        self.conn.close()

if __name__ == "__main__":    test = DBUtils()    result = test.dbSelect("select table_name from information_schema.tables where table_schema='testdb1'")    for dict1 in result:        test = DBUtils()        create_table_sql = "create table testdb.%s as select * from testdb1.%s" % (dict1['table_name'],dict1['table_name'])        print(create_table_sql)        test.dbExcute(create_table_sql)        

到此,关于“怎么用Python将一个DB的表结构同步给另一个DB”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


新闻标题:怎么用Python将一个DB的表结构同步给另一个DB
本文链接:http://www.wjwzjz.com/article/pdegcg.html
在线咨询
服务热线
服务热线:028-86922220
TOP