So, I'm trying to assign some SQL to a variable which will later be executed in the code. I also want to include a varible inside the SQL. I am only allowed to code over 80 characters and then I have been told to use a black slash, now this causes a problem. Hopefully this better explains it:

Current code-

sql_query = """"SELECT A1.value Value, COUNT(A1.value) "Frequency",
A1.category Category'
FROM popularity A1
WHERE Category = "books"
GROUP BY A1.value"""

I need to insert a varible here:

WHERE Category = category

Now that I think about it I might just assign each line to a variable but it's not aesthetically pleasing.

I have a solution which I'm half-heartedly happy with:

sql_query = 'SELECT A1.value Value, COUNT(A1.value)' + '"Frequency"'
sql_query = sql_query + ' , A1.category Category'
sql_query = sql_query + ' FROM popularity A1'
sql_query = sql_query + ' WHERE Category = ' + test + ' GROUP BY A1.value'

New solution:

sql_query = 'SELECT A1.value Value, COUNT(A1.value)' + '"Frequency"' \
    + ', A1.category Category' + ' FROM popularity A1' + ' WHERE Category = ' \
    + test + ' GROUP BY A1.value'

^ That one looks better, I think it might be the best I can get. Though, I'll still take suggestions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.