그래서 나는 배열 데이터를 파이썬으로 가지고 있습니다. 음, 오히려 목록 목록이 있습니다. 이 배열을 MySQL 데이터베이스의 단일 셀에 저장하려고합니다. JSON을 사용하여 데이터를 직렬화하려고 시도했지만 JSON의 작동 방식을 이해하지 못했을 수 있습니다.Python 목록 (JSON 또는 기타)을 MySQL 데이터베이스에 삽입하십시오.
그래서 내 데이터베이스에 연결 한 후 (내가
cur = con.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS 963168MBV17A(Id INT AUTO_INCREMENT PRIMARY KEY, Rev INT, Part VARCHAR(15), SN INT(7), Date DATE, Time TIME, Iterations INT(3), Upstream LONGBLOB, Downstream LONGBLOB, ResultList LONGTEXT, Result CHAR(1), Report LONGBLOB)")
내가 upstream_data 및 downstream_data라는 목록 내 목록을 가지고 할 업스트림에 대한 LONGTEXT 및 LONGBLOB 데이터 유형을 시도하고 다운 스트림했습니다
export_upstream = json.dumps(upstream_data)
export_downstream = json.dumps(downstream_data)
그런 다음 SQL 명령을 실행합니다.
cur = con.cursor()
sql_input = "INSERT INTO 963168MBV17A(Rev, Part, SN, Iterations, Date, Time, Upstream, Downstream, ResultList, Result, Report) VALUES('503', '100-120970-0031', '1594539', '%s', '%s', '%s', '%s', '%s', 0, P, 0" %(export_date, export_time, export_numtests, export_upstream, export_downstream)
cur.execute(sql_input)
export_upstream = json.dumps(json.dumps(upstream_data))
export_downstream = json.dumps(json.dumps(downstream_data))
을하지만 상관없이 나는 오류와 끝까지 : Mordi (http://stackoverflow.com/questions/4251124/inserting-json-into-mysql-using-python)에 의해 R, 나는 시도조차
또한Traceback (most recent call last):
File "P:\Projects\testing database\scrap\test.py", line 83, in <module>
cur.execute(sql_input)
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1")
, 나는
print "about to execute(%s)" % sql_input
내가 JSON 개체가 목록 (사방에 작은 따옴표의 많은 긴 문자열로 나타나는 것을보고,에 외부에 수행 할 때 문자열을 나타냄). json.dumps (json.dumps (upstream_data))를 수행하면 안쪽 인용 부호가 큰 따옴표가되고 \ 앞에 문자가옵니다. 그래도 그래도 같은 오류가 발생합니다.
아이디어가 있으십니까? 그렇지 않다면 파이썬 배열 /리스트 데이터를 단일 MySQL 셀에 저장하는 더 좋은 방법이 있을까요?
출력 here
가 마법처럼 일했다, 감사 – sirgogo