简体   繁体   English

使用 Python 在 MYSQL 数据库中创建表错误

[英]Create table error in MYSQL database using Python

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()

on this line在这条线上

CREATE TABLE PETROL...

you are missing the closing parenthesis after Date DATE您在Date DATE之后缺少右括号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM