python 스크립트를 사용하여 MYSQL 데이터베이스에 일부 데이터를 저장하려고하는데 다음 오류가 발생합니다.Python 'ndarray'를 MySQL 유형으로 변환 할 수 없습니다.
mysql.connector.errors.ProgrammingError: Failed processing format-parameters;
Python 'ndarray' 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 데이터베이스의 내 필드/열 이름입니다. 나는 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)
을 netCDF4 데이터를 삽입하려고 또는 내가 netCDF의 변수 데이터를 인쇄 할 때이
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,))
처럼 모든 변수를 결합하려고하면, 예를 들어, 시간 변수, 그것은 이
처럼 보인다nc.variables['time'][:] # netCDF variable
내가이
[1302614127 1302614137 1302614147 ..., 1302614627 1302614647 1302614657]
있어
및 마이크로은
[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]]
처럼 보이지만 내가
for v in data:
print v
를 인쇄하면 다음 난 단지 목록 내 첫번째 목록을 가지고 아닌 다른리스트 나는 내 주요 이슈라고 생각한다. 내가 cursor.executemany (smeData, (V)) 명령을 수행하려고하면, 그것은 나에게이 오류
내 MYSQL 구문을 삽입은
mysql.connector.errors.ProgrammingError: Not all parameters were used in
the SQL statement
를 제공하고
[1302614127 1302614137 1302614147 ..., 1302614627 1302614647 1302614657]
는 내 MYSQL 구문은
입니다"INSERT INTO `rob-tabl` (`time.val`,`time.microsec`,`temp.degrees`,
`humid.calc`,`pressure.calc`) VALUES (%s,%s,%s,%s,%s)"
제 경우에는 numpy.float32입니다. MYSQL에 5 개의 열을 만들었고 netCDF의 데이터를 db에 저장해야했습니다.
저는 프로그래밍에 익숙하지 않고 배우고 있습니다. 누군가가 나를 도와 주거나 그런 실수를 어떻게 처리 할 수 있는지 알려주십시오. 나는 매우 감사 할 것이다. 많이 고맙습니다.
는 종종 사람들 (예를 들어'pickle','np.save'로) 문자열로 배열을 인코딩하고 데이터베이스에 그것을 저장 'BLOB'로 하지만 숫자 필드 열로 배열을 저장하려고하는 것 같습니다. 1d 배열을 단순한리스트로 변환하기 위해서'arr.tolist()'를 해봤습니까? – hpaulj
MYSQL 필드 정의의 샘플을 제공하십시오. – hpaulj
@hpaulj 도움을 주셔서 대단히 감사합니다. MYSQL 필드 샘플을 추가하고 곧 다시 올 것입니다. 네, 데이터베이스에 5 개의 필드를 쓰려고합니다. 더 자세한 내용으로 내 질문을 업데이트 할 것입니다. – rob