Python Forum
Mysql and mysql.connector error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mysql and mysql.connector error
#1
Hello ,
i trying to read a text file that has 9 items to be inputted to the MySQL database.
The one issue i have (other than this error) the file can have more input data. please see end for test data
Any help i would be grateful. self taught on python Wall




cursor = db.cursor()

# Open the text file and read its contents
with open("sam.txt") as file:
    lines = file.readlines()

# Loop through the lines of the file and insert them into the database
for line in lines:
    # Split the line into its columns, assuming they are separated by commas
    values = line.strip().split(",")
    # Define the SQL query to insert the values into the table
sql = ("INSERT INTO master1 (inf0,time1,strong,lats,lon,loc1,loc2,test1,test2,test3,test4) values (%s,%s,%s,%s,%s,%s,%s,%s,%s)")
    # Execute the query with the values as parameters

DATAIS = ('1','2','3','4','5','6','7','8','9','10','11')


cursor.execute(sql, values)
    # Commit the changes to the database
db.commit()

# Close the database connection
db.close()
print(cursor.rowcount, "records inserted.")
Error:
Not all parameters were used in the SQL statement

test data

Information, 2021.09.17 000000 UTC , 6.6(Mwp), -33.300 -178.200, south of the Kermadec Islands An information statemenNot all parameters were used in the SQL statementt indicates that an, earthquake has occurred, but does not pose a tsunami threat, or that a tsunami warning, advisory, or watch has been issued for another section of the ocean
Reply
#2
There are 11 things here:
(inf0,time1,strong,lats,lon,loc1,loc2,test1,test2,test3,test4)
There are 9 things here:
(%s,%s,%s,%s,%s,%s,%s,%s,%s)
]
There are nine things here:
Information, 2021.09.17 000000 UTC , 6.6(Mwp), -33.300 -178.200, south of the Kermadec Islands An information statemenNot all parameters were used in the SQL statementt indicates that an, earthquake has occurred, but does not pose a tsunami threat, or that a tsunami warning, advisory, or watch has been issued for another section of the ocean
They all have to agree. The last one is questionable. Should "but does not pose a tsunami threat" really be a column in you table?

I think these should be your table columns.
type, time, strong, lat_long, description

You need to limit the split to split the line into 5 parts. You should strip off leading and trailing whitespace.
values = [x.strip() for x in line.strip().split(",", maxsplit=5)
From your test data:
Output:
type = Information time = 2021.09.17 000000 UTC strong = 6.6(Mwp) long_lat = -33.300 -178.200 description = south of the Kermadec Islands An information statemenNot all parameters were used in the SQL statementt indicates that an, earthquake has occurred, but does not pose a tsunami threat, or that a tsunami warning, advisory, or watch has been issued for another section of the ocean
If not all lines have the same information, you will need a more intelligent parser.
Reply
#3
thanks for your help



(Oct-02-2023, 11:54 PM)deanhystad Wrote: There are 11 things here:
(inf0,time1,strong,lats,lon,loc1,loc2,test1,test2,test3,test4)
There are 9 things here:
(%s,%s,%s,%s,%s,%s,%s,%s,%s)
]
There are nine things here:
Information, 2021.09.17 000000 UTC , 6.6(Mwp), -33.300 -178.200, south of the Kermadec Islands An information statemenNot all parameters were used in the SQL statementt indicates that an, earthquake has occurred, but does not pose a tsunami threat, or that a tsunami warning, advisory, or watch has been issued for another section of the ocean
They all have to agree. The last one is questionable. Should "but does not pose a tsunami threat" really be a column in you table?

I think these should be your table columns.
type, time, strong, lat_long, description

You need to limit the split to split the line into 5 parts. You should strip off leading and trailing whitespace.
values = [x.strip() for x in line.strip().split(",", maxsplit=5)
From your test data:
Output:
type = Information time = 2021.09.17 000000 UTC strong = 6.6(Mwp) long_lat = -33.300 -178.200 description = south of the Kermadec Islands An information statemenNot all parameters were used in the SQL statementt indicates that an, earthquake has occurred, but does not pose a tsunami threat, or that a tsunami warning, advisory, or watch has been issued for another section of the ocean
If not all lines have the same information, you will need a more intelligent parser.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 305 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Unable to connectDjango to mysql server sureshpython 0 194 Apr-09-2024, 07:30 PM
Last Post: sureshpython
  Virtual Env changing mysql connection string in python Fredesetes 0 383 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 833 Mar-21-2023, 10:06 PM
Last Post: deanhystad
  Replacing values ​​in Mysql with a condition stsxbel 0 642 Mar-05-2023, 08:20 PM
Last Post: stsxbel
  Mysql Workbench table not updated CatBall 2 1,121 Feb-13-2023, 05:37 PM
Last Post: CatBall
Photo How to select NULL and blank values from MySQL table into csv python300 9 2,456 Dec-27-2022, 09:43 PM
Last Post: deanhystad
  mysql id auto increment not working tantony 10 2,451 Oct-18-2022, 11:43 PM
Last Post: Pedroski55
  Mysql Syntax error in pymysql ilknurg 4 2,378 May-18-2022, 06:50 AM
Last Post: ibreeden
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,136 Feb-09-2022, 09:55 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020