cassandra.protocol.SyntaxException: <Error from server: code=2000 [Syntax error in CQL query] message="line 1:137 no viable alternative at input

Viewed 480

I am trying to insert a zip file to blob storage of cassandra db. type workflowid str and savepath blob while creating a table column

try:

bin_data = open('4e9b-9c08-e3cafafd871b-20211129034556.zip', 'rb').read()
print(type(bin_data))
#hex_data = codecs.encode(bin_data, "hex_codec")
#print(type(hex_data))
auth_provider = PlainTextAuthProvider(username = username , password = password)
cluster = Cluster(contact_points = [ip], port=1234, auth_provider = auth_provider)
session = cluster.connect("keyspace")
session.row_factory = dict_factory
resultpath = bin_data
workflowid = "d6fe8ea4-fffd"
query = "insert into tablename(resultpath, workflowid) VALUES({resultpath}, '{workflowid}');".format(resultpath, workflowid)
rslt = session.execute(query, timeout=None)
df = pd.DataFrame(rslt)
print(df)

except Exception as E:
    print("Error: ", str(E))
1 Answers

It looks like some of the data in the BLOB isn't being properly escaped. Try using a prepared statement, instead:

query = "insert into tablename(resultpath, workflowid) VALUES(?,?);"
pStatement = session.prepare(query)
rslt = session.execute(pStatement, [resultpath, workflowid])
Related