Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing CSV into MySQL
#1
I have a CSV file that contains 10234 records. Here are what the first 20 rows look like.
Each row has 19 column. The delimiter='\t'

Quote:Gary Ehrenfeld
# 558686118
ID Time Record Type Historic Glucose (mg/dL) Scan Glucose (mg/dL) Non-numeric Rapid-Acting Insulin Rapid-Acting Insulin (units) Non-numeric Food Carbohydrates (grams) Non-numeric Long-Acting Insulin Long-Acting Insulin (units) Notes Strip Glucose (mg/dL) Ketone (mmol/L) N/A N/A N/A Previous Time Updated Time
132 2018/07/28 01:41 0 141
133 2018/07/28 01:56 0 133
134 2018/07/28 02:11 0 126
135 2018/07/28 02:27 1 123
137 2018/07/28 02:27 0 126
138 2018/07/28 02:42 0 119
139 2018/07/28 02:57 0 96
140 2018/07/28 03:12 0 79
141 2018/07/28 03:27 0 71
142 2018/07/28 03:42 0 69
143 2018/07/28 03:57 0 72
144 2018/07/28 04:14 1 79
147 2018/07/28 05:16 6 2018/07/28 05:16 2018/07/28 05:16
148 2018/07/28 04:12 0 83
149 2018/07/28 04:27 0 86
150 2018/07/28 04:42 0 85
151 2018/07/28 04:57 0 86

The MySQL table.
Quote: id int
r_date datetime
rtype int
hist int
scan int

I want to import only the first 5 rows from each column into the database.

My python code.

import mysql.connector
from mysql.connector import Error
import csv
from tkinter import *

try:
    connection = mysql.connector.connect(host='localhost',
                                         database='meter',
                                         user='root',
                                         password='root')
    if connection.is_connected():
        db_Info = connection.get_server_info()
        print("Connected to MySQL database... MySQL Server version on ", db_Info)
        cursor = connection.cursor()
        cursor.execute("select database();")
        record = cursor.fetchone()
        print("Your connected to - ", record)
except Error as e:
    print("Error while connecting to MySQL", e)
finally:
    # closing database connection.
    if (connection.is_connected()):
        cursor.close()
        print("MySQL connection is closed")

cursor = connection.cursor()

f = open('summary.csv', 'r')
dr = csv.reader(f, delimiter='\t')
for row in dr:
        cursor.execute('INSERT INTO readings (ID, r_date, rtype, hist, scan) VALUES (?, ?, ?, ?, ?)',
                       row)
connection.commit()
The error I get.

Quote:"Not all parameters were used in the SQL statement")
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement

Huh Huh

Thanks for any help.
Gary
Reply
#2
My best guess, without playing around with it, is that
        cursor.execute('INSERT INTO readings (ID, r_date, rtype, hist, scan) VALUES (?, ?, ?, ?, ?)',
                       row)
ought to be
        cursor.execute('INSERT INTO readings (ID, r_date, rtype, hist, scan) VALUES (?, ?, ?, ?, ?)',
                       row[:5])
Reply
#3
I will give it a try.

Thank you

Tried the row[:5]. Got the same error.

I just insert all the fields and it works okay now.

Not the best solution but it works.
Gary
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql and mysql.connector error lostintime 2 679 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,044 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