0

How can I convert the account value into a string that my SQL query accepts.

import pandas as pd
import os
from xlwings import Workbook, Range
from sqlalchemy import create_engine

account = Range('G2').options(numbers=int).value #account number #value is 3001001000

sql = 'SELECT top 30 oraclenum,pcat,ConLenses, sum(dollars) as dollars FROM ' \
          'ConData WHERE oraclenum = {} group by oraclenum,pcat, ConLenses'.format(account)

How can I include the value between ' '. Kinda like WHERE oraclenum = '3001001000' in my SQL query. It throws an error if its not formatted as a string.

1

1 Answer 1

2

You could put escaped quotes around your format expression (\'{}\'), though the best way is to swap quotes for double quotes and put simple quotes around your format string

sql = "SELECT top 30 oraclenum,pcat,ConLenses, sum(dollars) as dollars FROM " \
          "ConData WHERE oraclenum = '{}' group by oraclenum,pcat, ConLenses".format(account)
Sign up to request clarification or add additional context in comments.

2 Comments

You could also use """ (triple quotes) for multi-line strings. See stackoverflow.com/questions/10660435/…
you're right, but in that case, it's just 2 strings on a single line, concatenated and using backslash to avoid creating a too long line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.