# -*- coding: utf-8 -*-
r"""B87: xuất bản CHỤP NHẤT QUÁN của CSDLA.db + CSDLB.db (API sqlite3.backup) + verify, để gửi chủ nhân."""
import os, sqlite3, time

A = r'C:\Claw 2\Nghien cuu\Du-an-A-Quy-trinh-son\CSDLA.db'
B = r'C:\Claw 2\Nghien cuu\Du-an-B-He-mau-Stain-Glaze\CSDLB.db'
OUT = r'C:\Claw 2\_gui_CSDL_1210'
os.makedirs(OUT, exist_ok=True)

for src_p, ten in ((A, 'CSDLA.db'), (B, 'CSDLB.db')):
    dich = os.path.join(OUT, ten)
    src = sqlite3.connect(src_p)
    dst = sqlite3.connect(dich)
    with dst:
        src.backup(dst)
    dst.close(); src.close()
    print('=== %s' % ten)
    print('   gốc : %s (%d B, sửa %s)' % (src_p, os.path.getsize(src_p),
          time.strftime('%d/%m/%Y %H:%M', time.localtime(os.path.getmtime(src_p)))))
    print('   chụp: %s (%d B)' % (dich, os.path.getsize(dich)))
    c = sqlite3.connect('file:%s?mode=ro' % dich.replace('\\', '/'), uri=True)
    print('   integrity_check:', c.execute('pragma integrity_check').fetchone()[0])
    t = [r[0] for r in c.execute("select name from sqlite_master where type='table' order by name")]
    for x in t:
        if x == 'sqlite_sequence':
            continue
        n = c.execute('select count(*) from "%s"' % x).fetchone()[0]
        print('      %-22s %5d dòng' % (x, n))
    c.close()
    print()
print('  thư mục gửi:', OUT)
