前提・実現したいこと
エラーを直したい!
発生している問題・エラーメッセージ
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 'between 2000-01-01 00:00:00 and 2000-01-01 00:30:00) and ### = 1 and ##' at line 1") During handling of the above exception, another exception occurred: DatabaseError: Execution failed on sql : (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)
該当のソースコード
Python3
1import numpy as np 2import pandas as pd 3import pandas.io.sql as psql 4import matplotlib.pyplot as plt 5import seaborn as sns 6import sqlalchemy as sa 7import datetime 8import pickle 9import os 10import const 11from pandas.io import sql 12from sklearn.externals import joblib 13from more_itertools import chunked 14from more_itertools import collapse 15from sqlalchemy import create_engine 16from sklearn.ensemble import RandomForestRegressor 17from sklearn.model_selection import train_test_split 18from sklearn.metrics import r2_score 19from sklearn.metrics import mean_squared_error 20date_format = '%Y-%m-%d %H:%M:%S' 21from sshtunnel import SSHTunnelForwarder 22import MySQLdb 23(中略) 24df = pd.DataFrame() 25for index, row in df_hoge.iterrows(): 26 if index > 5: 27 break 28 29 str_start_time = row['start'].strftime(date_format) 30 str_end_time = row['end'].strftime(date_format) 31 print(str_start_time) 32 print(str_end_time) 33 34 str_sql = 'select ### as hogehoge, ### as hogehoge2, ### as hogehoge3, ### as hogehoge4 from ### where ### (between ' + str_start_time +' and '+ str_end_time +') and ### = 1 and ### = 0 and ### > 0 order by ###'; 35 df_tmp = psql.read_sql(str_sql,cnn) 36
試したこと
str_sqlの+str_start_time+と+str_end_time+でエラーが起こってるのは分かったのですが対処法がわかりません.
また,パース文字で上記二つを置き換えても駄目だったので,上の方法で試しています.
補足情報(FW/ツールのバージョンなど)
str_start_timeとstr_end_timeがsql文で指示が出せればどんな解決方法でもいいです.
よろしくお願いいたします.
回答1件
あなたの回答
tips
プレビュー