我试图使用python脚本在MYSQL数据库中存储一些数据,但是我收到了以下错误。
mysql.connector.errors.ProgrammingError: Failed processing format-parameters;
Python 'list' cannot be converted to a MySQL type
实际上我从netCDF文件中提取变量并尝试将它们存储在MYSQL db中。我的代码是
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;"""
这些是我在mMYSQL数据库中的字段/列名称。我试图将netCDF4数据插入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).tolist(), (data2).tolist(), data3.tolist(), data4.tolist(), data5.tolist()])
data_array.append(data)
print type(data)
for v in data_array:
cursor.executemany(smeData, (v,))
当我打印netCDF变量数据时,例如时间变量,它看起来像 此
nc.variables['time'][:] # netCDF variable
所以data1看起来像这样
[1302614127 1302614137 1302614147 ............., 1302614627 1302614647 1302614657]
和data2的微秒看起来像是
[0 0 0..................., 0 0 0]
并且data_array看起来像这样,因为它包含五个不同的列表。
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])
如果我尝试执行cursor.executemany(smeData,(v,))命令,它会给我这个错误
Python 'list' cannot be converted to a MySQL type
我的MYSQL插入语法是
"INSERT INTO `rob-tabl` (`time.val`,`time.microsec`,`temp.degrees`,
`humid.calc`,`pressure.calc`) VALUES (%s,%s,%s,%s,%s)"
我在MYSQL中创建了5个列,我不得不将netCDF中的数据存储到db中 如果有人帮助我或提供一些提示,我该如何处理这样的错误。这会很棒。谢谢
答案 0 :(得分:0)
虽然我从未按照我所看到的那样使用它:
smeData = "INSERT INTO `rob-tabl` (`time.val`,`time.microsec`,`temp.degrees`, `humid.calc`,`pressure.calc`) VALUES (%s,%s,%s,%s,%s)"
cursor.executemany(smeData, data_array)
没有for循环。