如果表存在但将其加载为空,将数据加载到sql表中

时间:2019-04-25 23:08:26

标签: mysql database sqlite mariadb pymysql

我正在编写一个连接到SQL数据库的python脚本。它根据文件夹路径创建数据库,并根据相应数据库中这些文件夹中存在的文件创建表。

我的以下代码可以很好地完成所有工作,但我想对其进行优化,以便仅在表为空时才将数据加载到表中。

以下代码的问题是,每当我运行它时,它都会检查该表是否不存在,会创建该表,如果该表已经存在,它将继续运行,但是在将数据加载到表中时,每次运行脚本时,它将数据从文件1加载到表中。

我想对它进行调整,使其仅在数据已经不存在的情况下才将数据加载到表中。如果存在数据,则代码继续。

我尝试执行如果不存在则创建表之类的操作,但未成功。

hostname = 'hostname'
username = 'usrname'
password = '12345'
database = 'd1'
portname = '12345'
from mysql.connector.constants import ClientFlag
import pathlib
import sys
import os
import mysql.connector
import subprocess
from subprocess import *
import time
from termcolor import colored


print(colored('\nConnecting SQL database using host = '+hostname+' , username = '+username+' , port = ' +portname+ ' , database = ' +database+'.','cyan',attrs=['reverse','blink']))
print('\n')
myConnection = mysql.connector.connect(user=username, passwd=password,host=hostname,port=portname,database=database,client_flags=[ClientFlag.LOCAL_FILES])
myCursor =myConnection.cursor()

rootDir35 = '/mnt/Wdrive/pc35/SK/E13'
filenames35 = os.listdir(rootDir35)
root35 = pathlib.Path(rootDir35)
non_empty_dirs35 = {str(p35.parent) for p35 in root35.rglob('*') if p35.is_file()}
#35
try:
        print(colored('**** Starting Performing SQL-Queries for pc35 **** \n','green',attrs=['reverse','blink'] ))
        for f35 in non_empty_dirs35:
                dB35 = f35.replace('/','_') or f35.replace('-','_')
                for dirName35, subdirList35, fileList35 in os.walk(rootDir35):
                        if dirName35 == f35:
                                p1135= 'Current Working Directory is:   %s' %f35+ ' '
                                print(colored(p1135,'cyan'))
                                createDB35='CREATE DATABASE IF NOT EXISTS %s' %dB35
                                myCursor.execute(createDB35)
                                p135='Database of pc35 Created :        %s' %dB35
                                print(colored(p135,'cyan'))
                                useDB35='use %s' %dB35
                                myCursor.execute(useDB35)
                                myConnection.commit()
                                p235= 'Database in use :               %s' %dB35
                                print(colored(p235,'cyan'))
                                print(' ')
                                for fname35 in fileList35:
                                        completePath35 ='%s' %dirName35+'/%s'%fname35
                                        tblname35 = os.path.basename(fname35).split('.')[0]
                                        if '-' not in tblname35:
                                                if '.' not in tblname35:
                                                       sql35= 'CREATE TABLE if not exists %s (Datum varchar(50), Uhrzeit varchar(13), UpsACT_V varchar(6), UpsPRE_V varchar(6), IpsACT_A varchar(6), IpsPRE_A varchar(6), PpsACT_W varchar(6), PpsPRE_W varchar(10), UelACT_V varchar(6), UelPRE_V varchar(6), IelACT_A varchar(8), IelPRE_A varchar(8), PelACT_W varchar(8), PelPRE_W varchar(8), Qlad_Ah varchar(10), Qlast_Ah varchar(10))' %(tblname35)
                                                        myCursor.execute(sql35)
                                                        myConnection.commit()
                                                        test35 = 'The Table                %s              '              %tblname35+               'in database             %s             '%dB35+'is created'
                                                        print(colored(test35,'yellow'))
                                                        loadData35= "LOAD DATA LOCAL INFILE  '%s' "  %completePath35 + "INTO TABLE %s" %tblname35
                                                        myCursor.execute(loadData35)
                                                        myConnection.commit()
                                                        p335='Data loaded from file      %s              ' %fname35
                                                        p435='               into table %s              ' %tblname35
                                                        p535 = p335 + p435
                                                        print(colored(p535,'green'))
                                                        print(' ')
        print(colored('**** SQL-Queries for pc35 successfully executed **** \n','green',attrs=['reverse','blink']))
except:
        print(' ')
        print(colored('**** SQL queries for pc35 doesnot executed. Please refer to the report or user manual for more details ****','red',attrs=['reverse','blink']))
        print(' ')

我想要的是如果表中不存在则加载数据。 您认为如何,有可能或者我应该怎么做才能实现这一目标?

0 个答案:

没有答案