'mysql.connector.errors.ProgrammingError: 1146 (42S02): Table 'd1.tblname' doesn't exist

Right now i am doing a project in which I have to create SQL database using mysql.connector for python on RPi. The purpose of this project is to create SQL Database from the text files present in a directory of raspberry pi. My motive is to create tables in database as of the names of text files and then inserting data from text files to their corrosponding tables.

I have written a python script which sccessfuly connects to my database and creates my required table but I have problems which I try to load my data into created tables.

My code looks like

#!/usr/bin/python
hostname = 'localHost'
username = 'userName'
password = 'password'
database = 'databseName'
portname = 'portNumber'

from mysql.connector.constants import ClientFlag
import os,csv,string,sys
import mysql.connector
print('\nConnecting SQL database using host = '+hostname+' ,username = '+username+' ,database = '+database+' ,port = '+portname+'.')
print('\n')


myConnection = mysql.connector.connect(user=username, passwd=password,host=hostname,port=portname,db=database,client_flags=[ClientFlag.LOCAL_FILES])
myCursor =myConnection.cursor()

rootDir = '/mnt/Wdrive/pc70/SK/E07'

for dirName, subdirList, fileList in os.walk(rootDir):
        for fname in fileList:
                tblname = os.path.basename(fname).split('.')[0]
                #print('%s' %(tblname))
                print(tblname)
                sql= 'CREATE TABLE %s (Datum varchar(12), 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)) % tblname
                myCursor.execute(sql)
                myConnection.commit()
                finalpath= os.path.join(rootDir,fname)
                print(finalpath)
                loadData= "LOAD DATA LOCAL INFILE 'finalpath' INTO TABLE tblname COLUMNS TERMINATED BY '\t' "
                #loadData= "LOAD DATA LOCAL INFILE join INTO TABLE tblname COLUMNS TERMINATED BY '\t' IGNORE 11 lines"
                myCursor.execute(loadData)
                myConnection.commit()

When I run the above program I get the following error

   Connecting SQL database using host = **** ,username = **** ,database = **** ,port = ****.


CM_lad_19Dez2018_14Uhr38
Traceback (most recent call last):
  File "25createtable.py", line 39, in <module>
    myCursor.execute(loadData)
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/cursor.py", line 491, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 683, in cmd_query
    statement))
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 601, in _handle_result
    raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1146 (42S02): Table 'd1.tblname' doesn't exist

My program is creating tables as per my requirement but it's not loading data into it and exits the code with the above mentioned error.

I need to tweak loadData= "LOAD DATA LOCAL INFILE 'finalpath' INTO TABLE **tblname** COLUMNS TERMINATED BY '\t' " in a way that it reads the corrosponding table in my database As far as I know the error lies with tblname variable. Any ideas?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source