Create table error in MYSQL database using Python

Sarthak Kashyap :

I wanted to create a database of my data which have been stored in files .For this I used file handling to read data from the stored files and input into the table of my database. But it shows error given below.

Is it the correct way to switch data storage option from files to database.

The error occured is given below -:

Traceback (most recent call last):
  File "C:/Users/sarth/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratches/scratch_2.py", line 61, in <module>
    indatabase()

  File "C:/Users/sarth/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratches/scratch_2.py", line 54, in indatabase
    cur.execute ("SELECT * FROM PETROL")

  File "C:\Users\sarth\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\cursor.py", line 566, in execute
    self._handle_result(self._connection.cmd_query(stmt))

  File "C:\Users\sarth\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\connection.py", line 537, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))

  File "C:\Users\sarth\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\connection.py", line 436, in _handle_result
    raise errors.get_exception(packet)

mysql.connector.errors.ProgrammingError: 1146 (42S02): Table 'pump.petrol' doesn't exist
Failed to create table in MySQL: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AMOUNT FLOAT ,QUANTITY FLOAT ,Year VARCHAR(10) ,Date DATE' at line 1

This is the code which reads data from files and then inputs in the database.

import pickle
def indatabase() :
    # FOR PETROL
    import mysql.connector


    try :
        conn = mysql.connector.connect (host='localhost' ,database='PUMP' ,user='asdad' ,
        password='11234567')

        cur = conn.cursor ( )
        cur.execute ("DROP TABLE IF EXISTS PETROL")
        cur.execute ("CREATE TABLE PETROL ( id INT AUTO_INCREMENT PRIMARY KEY ,
        AMOUNT FLOAT ,QUANTITY FLOAT ,Year VARCHAR(10) ,Date DATE")
        fp1 = open ("D:/Python/petrol/pdate/pdate.txt" , "rb+")
        while True:
            try :
                pdate = pickle.load (fp1)
                cur.execute ("INSERT INTO PETROL Date VALUES(?)" , (pdate))
            except EOFError :
                fp1.close()
        fp3 = open ("D:/Python/petrol/pyear/pyear.txt" , "rb+")
        while True:
            try :
                pyear = pickle.load (fp3)
                cur.execute ("INSET INTO PETROL Year VALUES(?)" , (pyear))
            except EOFError :
                fp3.close()
        fp4=open ("D:/Python/petrol/petrolamt/petrolamt.txt" , "rb+")
        while True:
            try :
                pamt = pickle.load (fp4)
                cur.execute ("INSERT INTO PETROL Amount VALUES(?)" , (pamt))
            except EOFError :
                fp4.close()

        fp5 = open ("D:/Python/petrol/petrolqty/petrolqty.txt" , "rb+")
        while True:
            try :
                pqty = pickle.load (fp5)
                cur.execute ("INSERT INTO PETROL Quantity VALUES(?)" , (pqty))
            except EOFError :
                fp5.close()
        conn.commit ( )

    except mysql.connector.Error as error :
        print ("Failed to create table in MySQL: {}".format (error))
    finally :
        if (conn.is_connected ( )) :
            cur.execute ("SELECT * FROM PETROL")
            for i in cur :
                print (i)
            cur.close ( )
            conn.close ( )
            print ("MySQL connection is closed")

indatabase()

Chris Curvey :

on this line

CREATE TABLE PETROL...

you are missing the closing parenthesis after Date DATE

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related