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


Messages In This Thread
Importing CSV into MySQL - by gehrenfeld - Dec-24-2018, 03:43 PM
RE: Importing CSV into MySQL - by micseydel - Dec-26-2018, 11:08 PM
RE: Importing CSV into MySQL - by gehrenfeld - Dec-27-2018, 11:39 AM

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