Compare commits
10 Commits
8556073062
...
2f30c56eb1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2f30c56eb1 | ||
|
|
174d616825 | ||
|
|
74619db6bb | ||
|
|
d98c85ec01 | ||
|
|
8edca7204f | ||
| 349ef270b7 | |||
| 3f934a4d84 | |||
|
|
d982868db9 | ||
| f3d8d685c2 | |||
| dafae21aa3 |
73
Davky/10 Import textovy davek.py
Normal file
73
Davky/10 Import textovy davek.py
Normal file
@@ -0,0 +1,73 @@
|
||||
import mysql.connector
|
||||
import os
|
||||
|
||||
db_config = {
|
||||
'host': '127.0.0.1',
|
||||
'port': 3307,
|
||||
'user': 'root',
|
||||
'password': 'Vlado9674+',
|
||||
'database': 'ordinace'
|
||||
}
|
||||
|
||||
ROOT_DIR = r'm:\Medicus\DavkyExport\09305000'
|
||||
|
||||
|
||||
def hromadny_raw_import():
|
||||
try:
|
||||
conn = mysql.connector.connect(**db_config)
|
||||
cursor = conn.cursor()
|
||||
|
||||
for root, dirs, files in os.walk(ROOT_DIR):
|
||||
for filename in files:
|
||||
if filename.upper().startswith('KDAVKA'):
|
||||
filepath = os.path.join(root, filename)
|
||||
|
||||
with open(filepath, 'r', encoding='cp1250') as f:
|
||||
first_line = f.readline().strip('\n')
|
||||
|
||||
if not first_line.startswith('DP'): continue
|
||||
|
||||
# Unikátní klíč (prvních 30 znaků)
|
||||
fingerprint = first_line[:30].strip()
|
||||
|
||||
# Kontrola duplicity
|
||||
cursor.execute("SELECT id_davky FROM davky WHERE identifikator = %s", (fingerprint,))
|
||||
if cursor.fetchone():
|
||||
continue
|
||||
|
||||
print(f"Importuji: {filename} -> {fingerprint}")
|
||||
|
||||
# Vložení dávky
|
||||
obdobi = first_line[20:26].strip()
|
||||
cursor.execute("INSERT INTO davky (identifikator, obdobi) VALUES (%s, %s)", (fingerprint, obdobi))
|
||||
id_davky = cursor.lastrowid
|
||||
|
||||
# Import všech řádků souboru
|
||||
with open(filepath, 'r', encoding='cp1250') as f:
|
||||
id_posledni_a = None
|
||||
for idx, line in enumerate(f, 1):
|
||||
raw = line.strip('\n')
|
||||
if not raw: continue
|
||||
typ = raw[0]
|
||||
|
||||
# Uložení věty
|
||||
cursor.execute(
|
||||
"INSERT INTO vety (id_davky, typ_vety, obsah_radku, poradi_v_souboru) VALUES (%s, %s, %s, %s)",
|
||||
(id_davky, typ, raw, idx)
|
||||
)
|
||||
current_id_vety = cursor.lastrowid
|
||||
|
||||
# Hierarchie pod 'A'
|
||||
if typ == 'A':
|
||||
id_posledni_a = current_id_vety
|
||||
|
||||
if id_posledni_a:
|
||||
cursor.execute("UPDATE vety SET id_dokladu_vazba = %s WHERE id_vety = %s",
|
||||
(id_posledni_a, current_id_vety))
|
||||
conn.commit()
|
||||
print("Raw import dokončen.")
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
hromadny_raw_import()
|
||||
Reference in New Issue
Block a user