Skip to content

Instantly share code, notes, and snippets.

@tsukanov-as
Created April 12, 2020 09:43
Show Gist options
  • Save tsukanov-as/b18e41f25635f23db80f0870a4cf80b3 to your computer and use it in GitHub Desktop.
Save tsukanov-as/b18e41f25635f23db80f0870a4cf80b3 to your computer and use it in GitHub Desktop.
Копирование живых таблиц базы 1С из сломаной базы в бэкап
"""
Сначала надо поставить библиотеку командой pip install pyodbc
"""
import pyodbc
server = 'localhost'
database = 'db_damaged'
username = 'sa'
password = 'пароль'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password, autocommit=True)
cursor = cnxn.cursor()
#region Перенос таблиц
"""
рядом должен лежать файл list.txt со списком скульных таблиц
"""
with open('list.txt', 'r') as f:
for line in f.readlines():
# print(line[:-1])
table = line[:-1]
try:
cursor.execute(f"SELECT * FROM [db_damaged].[dbo].[{table}];") # если тут бахнет, то следующие две строки будут пропущены
cursor.execute(f"DROP TABLE [db_oldbackup].[dbo].[{table}];")
cursor.execute(f"SELECT * INTO [db_oldbackup].[dbo].[{table}] FROM [db_damaged].[dbo].[{table}];")
except Exception as x:
print(table)
#endregion
#region Проверка количества строк
# cursor.execute("SELECT * FROM [db_oldbackup].[dbo].[_Document268];")
# rows = cursor.fetchall()
# print(len(rows))
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment