pymssql ValueError:list.remove(x):x不在列表中

时间:2018-10-09 11:00:58

标签: python sql-server pymssql

我对下面的代码有疑问:

import pandas as pd
import tensorflow
from matplotlib import pyplot
from sklearn.preprocessing import MinMaxScaler
from keras.models import model_from_json
import pymssql

加载json并创建模型

json_file = open('model_Messe_Dense.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights("model_Messe_Dense.h5")

导入值

import pickle

y_scaler = pickle.load( open( "y_scaler.p", "rb" ))
x_scaler = pickle.load( open( "x_scaler.p","rb"))

连接到服务器并更新值

while True:
    try:
        conn = pymssql.connect(
            server='SHS_Messe', 
            user='sa', 
            password='sa',
            database='ChillWARE_Transfer'
        )

        stmt = "SELECT screw_speed,\
                        ID,\
                        Cylinder_Temperatur_Zone_1,\
                        Cylinder_Temperatur_Zone_2,\
                        Cylinder_Temperatur_Zone_3,\
                        Cylinder_Temperatur_Zone_4,\
                        Cylinder_Temperatur_Zone_5,\
                        Cylinder_Temperatur_Zone_6,\
                        mass_pressure,\
                        Update_Done\
                        FROM to_ChillWARE where ID= (SELECT MAX(ID) FROM 
to_ChillWARE)"
    # Excute Query here
        df = pd.read_sql(stmt,conn)

    except pymssql.Error as e:
        print (e)
        break


    feature_col_names = ['screw_speed','Cylinder_Temperatur_Zone_1','Cylinder_Temperatur_Zone_2','Cylinder_Temperatur_Zone_3',\
                     'Cylinder_Temperatur_Zone_4','Cylinder_Temperatur_Zone_5','Cylinder_Temperatur_Zone_6']
    predicted_class_names = ['mass_pressure']
    Update = ['Update_Done']

    x = df[feature_col_names].values
    Update = df[Update].values
    x_scaled = x_scaler.transform(x)
    x_test = x_scaled

    predicted = loaded_model.predict(x_test)
    predicted = y_scaler.inverse_transform(predicted)

    predicted=predicted.reshape(-1)
    predicted.shape
    predicted=predicted * 51

    value=str(predicted)

    value=value.replace('[','')
    value=value.replace(']','')

    Update = str(Update)
    Update=Update.replace('[','')
    Update=Update.replace(']','')

    if Update == "False":
        cursor = conn.cursor()

        query = "UPDATE to_ChillWARE SET [mass_pressure] ="
        query = query + value + ",[Update_Done] = 1"
        query = query + " where ID= (SELECT MAX(ID) FROM to_ChillWARE)"

        cursor.execute(query)
        conn.commit()

我想检查我是否与mssql服务器建立连接,并且Update == False是否要更新值。

在我的电脑上,一切正常。我通过python和exe(pyinstaller)执行了代码。但是,如果我要将其转移到另一台PC,则会收到错误消息:

Traceback (most recent call last):
File "Test.py", line 29, in <module>
File "src\pymssql.pyx", line 636, in pymssql.connect
File "src\_mssql.pyx", line 1957, in _mssql.connect
File "src\_mssql.pyx", line 675, in _mssql.MSSQLConnection.__init__
ValueError: list.remove(x): x not in list

我认为pymssql函数有问题。 我在这里发现了相同的错误,但我不明白解决方案:

https://github.com/sqlmapproject/sqlmap/issues/3035

如果有人可以帮助,那就太好了。

谢谢大家

1 个答案:

答案 0 :(得分:0)

根据您提供的链接中的评论,它看起来像是连接错误。

您是否已从正在尝试使用机器的代码中检查您是否可以访问提供了服务器名和那些凭据的数据库服务器?

使用以下注释中的解决方案进行编辑:

您可以通过在while循环外定义“ conn = pymssql.connect ...”来重用连接,并始终使用该变量,因此您不必在每次迭代时都创建连接。