标签:数据库 return sele 插入 firebird print update main you
功能:使用于相同的表从一个数据库转存到另一数据库;
方式:直连fdb并加载django,引用django的model完成;
原因:1、select * from *** 返回的数有很多None,直接inset into *** values ()会报错;
2、设计字段包含关键字,select和insert,update都需要加”“,容易忽略;
3、有几个字段是blob的,消息体超大,直接插入会报错,使用django的model不但简单而且避免了一些因粗心犯的错误,代码也更易读。
# !/usr/bin/env python # -*- coding: utf-8 -*- import fdb import os import django def excute_sql(sql): con = fdb.connect(dsn=‘ip:path/***.FDB‘, user=‘your user name‘, password=‘your pwd‘) try: cur = con.cursor() cur.execute(sql) return quert_list except Exception as e: print(‘execute sql %s error, msg is %s‘ % (sql, e.message)) print (‘execute sql %s error, msg is %s‘ % (sql, e.message)) raise Exception(‘execute sql %s error, msg is %s‘ % (sql, e.message)) finally: con.close() def query(model): column_list = [‘\"%s\"‘ % f.column for f in model._meta.fields] field_list = [f.name for f in model._meta.fields] query_field = ‘,‘.join(column_list) sql = ‘select %s from %s‘ % (query_field, model._meta.db_table) data_list = excute_sql(sql) result = [] for data in data_list: data_map = collections.OrderedDict() for index in xrange(0, len(data)): if data[index] is not None: data_map[field_list[index]] = data[index] result.append(data_map) return result def insert(model): data_list = query_from_vnfm(model) model.objects.all().delete() for data in data_list: NfInstModel.objects.create(**nf_data) def init(): os.environ.setdefault("DJANGO_SETTINGS_MODULE", "***.settings") django.setup() if __name__ == ‘__main__‘: init() from *** import *** insert(***)
标签:数据库 return sele 插入 firebird print update main you
原文地址:https://www.cnblogs.com/small-office/p/9430509.html