I am trying to store some data in MYSQL database using python script, but i got the following error.
mysql.connector.errors.ProgrammingError: Failed processing format-parameters;
Python 'ndarray' cannot be converted to a MySQL type
Actually I am extracting variables from netCDF file and trying to store them in MYSQL db. my code is
import sys
import collections
import os
import netCDF4
import calendar
from netCDF4 import Dataset
import mysql.connector
from mysql.connector import errorcode
table = 'rob-tabl'
con = mysql.connector.connect(user='rob', password='xxxx',
database=roby)
cursor = con.cursor()
smeData = """
CREATE TABLE rob-tabl (
`id` bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`time.val` double,
`time.microsec` double,
`temp.degrees` double,
`humid.calc` double,
`pressure.calc` double;"""
these are my fields/ columns names in mMYSQL database. I am trying to insert netCDF4 data into MYSQL
smeData = "INSERT INTO `" + table + "` "
.
.
.
.
.
.
.
.
data_array = []
for item in totfiles.items(): # loop on different netCDF files in a directory , but at the moment I had only one file
nc = Dataset('filename', 'r')
data1 = nc.variables['time'][:]
data2 = nc.variables['microsec'][:]
data3 = nc.variables['temperature'][:]
data4 = nc.variables['humidity'][:]
data5 = nc.variables['pressure'][:]
data = data1 + data2 + data3 + data4 + data5
data_array.append(data)
print 'data_array: ', data_array
cursor.execute(smeData, data_array)
or if i try to combine all variable like this
data_array = []
for item in totfiles.items():
nc = Dataset('filename', 'r')
data1 = nc.variables['time'][:]
data2 = nc.variables['microsec'][:]
data3 = nc.variables['temperature'][:]
data4 = nc.variables['humidity'][:]
data5 = nc.variables['pressure'][:]
data = ([(data1).tolist(), (data2).tolist(), data3.tolist(), data4.tolist(), data5.tolist()])
data_array.append(data)
print type(data)
for v in data:
cursor.executemany(smeData, (v,))
when I print netCDF variables data, e.g time variable, it looks like this
nc.variables['time'][:] # netCDF variable
i got this
[1302614127 1302614137 1302614147 ..., 1302614627 1302614647 1302614657]
and microseconds looks like
[0 0 0 ..., 0 0 0]
and the data_array looks like
data_array= [[1302614127 1302614137 1302614147 ..., 1302614627 1302614647
1302614657], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [21, 22,34,34....,67,55], [12.2, 12.54, 12.55....,45.54,45.48], [0,0,0...,0,0,00]]
but if I print
for v in data:
print v
then i got 1st list only within list and not the other lists which I guess is my main issue.
[1302614127 1302614137 1302614147 ..., 1302614627 1302614647 1302614657]
and if i try to do cursor.executemany(smeData, (v,)) command , it gives me this error
mysql.connector.errors.ProgrammingError: Not all parameters were used in
the SQL statement
my MYSQL insert syntax is my MYSQL syntax is
"INSERT INTO `rob-tabl` (`time.val`,`time.microsec`,`temp.degrees`,
`humid.calc`,`pressure.calc`) VALUES (%s,%s,%s,%s,%s)"
In my case it is numpy.float32. I created 5 columns in MYSQL and I had to store data from netCDF into db.
I am new to programming and I am learning. if someone help me or give some hint how can I deal with such a error. I shall be very thankful.
thanx a lot.