300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Python pypyodbc读取拆分mdb文件

Python pypyodbc读取拆分mdb文件

时间:2020-05-27 01:26:20

相关推荐

Python pypyodbc读取拆分mdb文件

需求:将一个mdb文件拆分成两个差不多平均的mdb文件。def split_mdb_Before_encryption(path,file,split_count):file1=path+file.split('.mdb')[0]+'_1.mdb'file2=path+file.split('.mdb')[0]+'_2.mdb'file=path+file# file = r"E:\Daily-Work\11_Save_File\all.mdb"# file1 = r"E:\Daily-Work\11_Save_File\all_1.mdb"# file2 = r"E:\Daily-Work\11_Save_File\all_2.mdb"shutil.copyfile(file,file1)shutil.copyfile(file,file2)connStr = 'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ='+file+';PWD=007'conn = pypyodbc.win_connect_mdb(connStr) # 链接数据库cur = conn.cursor() # 创建游标# for table_info in cur.tables(tableType='TABLE'):# print(table_info[2])all_tables=['initevent','NETIDS_AttackType','NETIDS_Type_Security','top10_popular_event','validrule']columns=[]sql_initevent = 'SELECT * FROM initevent'cur.execute(sql_initevent)all_initevent_data = cur.fetchall() # 取 ActualValues_T 所有数据total_rows = len(all_initevent_data)total_cols = len(all_initevent_data[0])print("****************Begin to process\"表:initevent\"****************")# print("\"表initevent:%s\"总行数 = %d" %('initevent', total_rows))# print("\"表initevent:%s\"总列数 = %d" %('initevent', total_cols))print("*****表initevent一共有",len(all_initevent_data),"行数据****")# print(all_initevent_data)# for line in all_initevent_data:#print(type(line))#sql_insert="INSERT INTO initevent "+line#cur1.execute(sql_insert)sql_NETIDS_AttackType = 'SELECT * FROM NETIDS_AttackType'cur.execute(sql_NETIDS_AttackType)all_NETIDS_AttackType_data = cur.fetchall() # 取 ActualValues_T 所有数据print("*****表NETIDS_AttackType一共有",len(all_NETIDS_AttackType_data),"行数据****")sql_NETIDS_Type_Security = 'SELECT * FROM NETIDS_Type_Security'cur.execute(sql_NETIDS_Type_Security)all_NETIDS_Type_Security_data = cur.fetchall() # 取 ActualValues_T 所有数据print("*****表NETIDS_Type_Security一共有",len(all_NETIDS_Type_Security_data),"行数据****")sql_top10_popular_event = 'SELECT * FROM top10_popular_event'cur.execute(sql_top10_popular_event)all_top10_popular_event_data = cur.fetchall() # 取 ActualValues_T 所有数据print("*****表top10_popular_event一共有",len(all_top10_popular_event_data),"行数据****")sql_validrule = 'SELECT * FROM validrule'cur.execute(sql_validrule)all_validrule_data = cur.fetchall() # 取 ActualValues_T 所有数据print("*****表validrule一共有",len(all_validrule_data),"行数据****")connStr1 = 'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ='+file1+';PWD=007'conn1 = pypyodbc.win_connect_mdb(connStr1) # 链接数据库cur1 = conn1.cursor() # 创建游标sql_del="delete from initevent where seq not in (select top "+str(split_count)+" seq from initevent)"cur1.execute(sql_del)mit()sql_select="select count(*) from initevent"count=cur1.execute(sql_select).fetchall()[0]print(file1+'行数',count)connStr2 = 'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ='+file2+';PWD=007'conn2 = pypyodbc.win_connect_mdb(connStr2) # 链接数据库cur2 = conn2.cursor() # 创建游标sql_del="delete from initevent where seq in (select top "+str(split_count)+" seq from initevent)"cur2.execute(sql_del)mit()sql_select="select count(*) from initevent"count=cur2.execute(sql_select).fetchall()[0]print(file2+'行数',count)conn2.close() # 关闭数据库conn1.close() # 关闭数据库conn.close() # 关闭数据库

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。