Python: How can I input a SQL query as a String without errors?
up vote
0
down vote
favorite
I am creating a program that manipulates SQL queries. At the moment it's very simple and all I want to do is define a variable with the SQL statement inside it as a string.
E.G.
str = "SELECT DISTINCT
pv.project_id,
gps.period_name,
gps.period_year,
rbse.resource_source_id,
rbse.alias as resource_name,
DECODE(pjo_plan_version_utils.get_time_phased_code(pv.plan_version_id), 'G',
pld.quantity, pjo_plan_version_utils.spread_amount('L', pld.start_date, pld.end_date, gps.start_date, gps.end_date, pld.quantity)
) as hours,
(131.4-pld.quantity)/131.4 unallocated_percentage
FROM
pjf_rbs_elements rbse,
gl_period_statuses gps,
pjo_plan_line_details pld,
pjo_planning_elements pe,
pjo_plan_versions_vl pv,
pjo_plan_types_vl pt
WHERE
1=1
AND pe.rbs_element_id = rbse.rbs_element_id
AND pld.planning_element_id = pe.planning_element_id
AND pv.plan_version_id = pe.plan_version_id
AND pv.current_plan_status_flag = 'Y'
AND ((gps.start_date <= pld.end_date)
AND (gps.end_date >= pld.start_date))
AND gps.adjustment_period_flag = 'N'
AND gps.application_id = 10037
AND pv.plan_type_id = pt.plan_type_id
AND pt.plan_type_code = 'PROJECT_PLAN'"
I am getting invalid syntax errors so I am clearly not defining it correctly. What is the best way to do this? For insight, the script will analyse str to identify tables and columns and return anything that matches a list.
python
add a comment |
up vote
0
down vote
favorite
I am creating a program that manipulates SQL queries. At the moment it's very simple and all I want to do is define a variable with the SQL statement inside it as a string.
E.G.
str = "SELECT DISTINCT
pv.project_id,
gps.period_name,
gps.period_year,
rbse.resource_source_id,
rbse.alias as resource_name,
DECODE(pjo_plan_version_utils.get_time_phased_code(pv.plan_version_id), 'G',
pld.quantity, pjo_plan_version_utils.spread_amount('L', pld.start_date, pld.end_date, gps.start_date, gps.end_date, pld.quantity)
) as hours,
(131.4-pld.quantity)/131.4 unallocated_percentage
FROM
pjf_rbs_elements rbse,
gl_period_statuses gps,
pjo_plan_line_details pld,
pjo_planning_elements pe,
pjo_plan_versions_vl pv,
pjo_plan_types_vl pt
WHERE
1=1
AND pe.rbs_element_id = rbse.rbs_element_id
AND pld.planning_element_id = pe.planning_element_id
AND pv.plan_version_id = pe.plan_version_id
AND pv.current_plan_status_flag = 'Y'
AND ((gps.start_date <= pld.end_date)
AND (gps.end_date >= pld.start_date))
AND gps.adjustment_period_flag = 'N'
AND gps.application_id = 10037
AND pv.plan_type_id = pt.plan_type_id
AND pt.plan_type_code = 'PROJECT_PLAN'"
I am getting invalid syntax errors so I am clearly not defining it correctly. What is the best way to do this? For insight, the script will analyse str to identify tables and columns and return anything that matches a list.
python
replace each double quote with 3 single quotes, also change the parameter namestr
to maybequery_str
, asstr
is already used by python.
– teng
Nov 20 at 0:04
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am creating a program that manipulates SQL queries. At the moment it's very simple and all I want to do is define a variable with the SQL statement inside it as a string.
E.G.
str = "SELECT DISTINCT
pv.project_id,
gps.period_name,
gps.period_year,
rbse.resource_source_id,
rbse.alias as resource_name,
DECODE(pjo_plan_version_utils.get_time_phased_code(pv.plan_version_id), 'G',
pld.quantity, pjo_plan_version_utils.spread_amount('L', pld.start_date, pld.end_date, gps.start_date, gps.end_date, pld.quantity)
) as hours,
(131.4-pld.quantity)/131.4 unallocated_percentage
FROM
pjf_rbs_elements rbse,
gl_period_statuses gps,
pjo_plan_line_details pld,
pjo_planning_elements pe,
pjo_plan_versions_vl pv,
pjo_plan_types_vl pt
WHERE
1=1
AND pe.rbs_element_id = rbse.rbs_element_id
AND pld.planning_element_id = pe.planning_element_id
AND pv.plan_version_id = pe.plan_version_id
AND pv.current_plan_status_flag = 'Y'
AND ((gps.start_date <= pld.end_date)
AND (gps.end_date >= pld.start_date))
AND gps.adjustment_period_flag = 'N'
AND gps.application_id = 10037
AND pv.plan_type_id = pt.plan_type_id
AND pt.plan_type_code = 'PROJECT_PLAN'"
I am getting invalid syntax errors so I am clearly not defining it correctly. What is the best way to do this? For insight, the script will analyse str to identify tables and columns and return anything that matches a list.
python
I am creating a program that manipulates SQL queries. At the moment it's very simple and all I want to do is define a variable with the SQL statement inside it as a string.
E.G.
str = "SELECT DISTINCT
pv.project_id,
gps.period_name,
gps.period_year,
rbse.resource_source_id,
rbse.alias as resource_name,
DECODE(pjo_plan_version_utils.get_time_phased_code(pv.plan_version_id), 'G',
pld.quantity, pjo_plan_version_utils.spread_amount('L', pld.start_date, pld.end_date, gps.start_date, gps.end_date, pld.quantity)
) as hours,
(131.4-pld.quantity)/131.4 unallocated_percentage
FROM
pjf_rbs_elements rbse,
gl_period_statuses gps,
pjo_plan_line_details pld,
pjo_planning_elements pe,
pjo_plan_versions_vl pv,
pjo_plan_types_vl pt
WHERE
1=1
AND pe.rbs_element_id = rbse.rbs_element_id
AND pld.planning_element_id = pe.planning_element_id
AND pv.plan_version_id = pe.plan_version_id
AND pv.current_plan_status_flag = 'Y'
AND ((gps.start_date <= pld.end_date)
AND (gps.end_date >= pld.start_date))
AND gps.adjustment_period_flag = 'N'
AND gps.application_id = 10037
AND pv.plan_type_id = pt.plan_type_id
AND pt.plan_type_code = 'PROJECT_PLAN'"
I am getting invalid syntax errors so I am clearly not defining it correctly. What is the best way to do this? For insight, the script will analyse str to identify tables and columns and return anything that matches a list.
python
python
asked Nov 19 at 23:58
A.Leggett
265
265
replace each double quote with 3 single quotes, also change the parameter namestr
to maybequery_str
, asstr
is already used by python.
– teng
Nov 20 at 0:04
add a comment |
replace each double quote with 3 single quotes, also change the parameter namestr
to maybequery_str
, asstr
is already used by python.
– teng
Nov 20 at 0:04
replace each double quote with 3 single quotes, also change the parameter name
str
to maybe query_str
, as str
is already used by python.– teng
Nov 20 at 0:04
replace each double quote with 3 single quotes, also change the parameter name
str
to maybe query_str
, as str
is already used by python.– teng
Nov 20 at 0:04
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
The string in multiline so you need to use triple quotes or else you get an EOL. Also you shouldn't name a variable str
because it is already a function.
string = """SELECT DISTINCT
pv.project_id,
gps.period_name,
gps.period_year,
rbse.resource_source_id,
rbse.alias as resource_name,
DECODE(pjo_plan_version_utils.get_time_phased_code(pv.plan_version_id), 'G',
pld.quantity, pjo_plan_version_utils.spread_amount('L', pld.start_date, pld.end_date, gps.start_date, gps.end_date, pld.quantity)
) as hours,
(131.4-pld.quantity)/131.4 unallocated_percentage
FROM
pjf_rbs_elements rbse,
gl_period_statuses gps,
pjo_plan_line_details pld,
pjo_planning_elements pe,
pjo_plan_versions_vl pv,
pjo_plan_types_vl pt
WHERE
1=1
AND pe.rbs_element_id = rbse.rbs_element_id
AND pld.planning_element_id = pe.planning_element_id
AND pv.plan_version_id = pe.plan_version_id
AND pv.current_plan_status_flag = 'Y'
AND ((gps.start_date <= pld.end_date)
AND (gps.end_date >= pld.start_date))
AND gps.adjustment_period_flag = 'N'
AND gps.application_id = 10037
AND pv.plan_type_id = pt.plan_type_id
AND pt.plan_type_code = 'PROJECT_PLAN'"""
add a comment |
up vote
1
down vote
Your string concatenation is incorrect.
Try something like this:
str = "line1"
"line2"
"line3"
#output : no spaces or new lines
line1line2line3
or (but you can not indent by doing below way, if you indent, the indentation becomes part of concatenation)
str = """line1
line2
line3"""
#output : new lines
line1
line2
line3
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384346%2fpython-how-can-i-input-a-sql-query-as-a-string-without-errors%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The string in multiline so you need to use triple quotes or else you get an EOL. Also you shouldn't name a variable str
because it is already a function.
string = """SELECT DISTINCT
pv.project_id,
gps.period_name,
gps.period_year,
rbse.resource_source_id,
rbse.alias as resource_name,
DECODE(pjo_plan_version_utils.get_time_phased_code(pv.plan_version_id), 'G',
pld.quantity, pjo_plan_version_utils.spread_amount('L', pld.start_date, pld.end_date, gps.start_date, gps.end_date, pld.quantity)
) as hours,
(131.4-pld.quantity)/131.4 unallocated_percentage
FROM
pjf_rbs_elements rbse,
gl_period_statuses gps,
pjo_plan_line_details pld,
pjo_planning_elements pe,
pjo_plan_versions_vl pv,
pjo_plan_types_vl pt
WHERE
1=1
AND pe.rbs_element_id = rbse.rbs_element_id
AND pld.planning_element_id = pe.planning_element_id
AND pv.plan_version_id = pe.plan_version_id
AND pv.current_plan_status_flag = 'Y'
AND ((gps.start_date <= pld.end_date)
AND (gps.end_date >= pld.start_date))
AND gps.adjustment_period_flag = 'N'
AND gps.application_id = 10037
AND pv.plan_type_id = pt.plan_type_id
AND pt.plan_type_code = 'PROJECT_PLAN'"""
add a comment |
up vote
1
down vote
accepted
The string in multiline so you need to use triple quotes or else you get an EOL. Also you shouldn't name a variable str
because it is already a function.
string = """SELECT DISTINCT
pv.project_id,
gps.period_name,
gps.period_year,
rbse.resource_source_id,
rbse.alias as resource_name,
DECODE(pjo_plan_version_utils.get_time_phased_code(pv.plan_version_id), 'G',
pld.quantity, pjo_plan_version_utils.spread_amount('L', pld.start_date, pld.end_date, gps.start_date, gps.end_date, pld.quantity)
) as hours,
(131.4-pld.quantity)/131.4 unallocated_percentage
FROM
pjf_rbs_elements rbse,
gl_period_statuses gps,
pjo_plan_line_details pld,
pjo_planning_elements pe,
pjo_plan_versions_vl pv,
pjo_plan_types_vl pt
WHERE
1=1
AND pe.rbs_element_id = rbse.rbs_element_id
AND pld.planning_element_id = pe.planning_element_id
AND pv.plan_version_id = pe.plan_version_id
AND pv.current_plan_status_flag = 'Y'
AND ((gps.start_date <= pld.end_date)
AND (gps.end_date >= pld.start_date))
AND gps.adjustment_period_flag = 'N'
AND gps.application_id = 10037
AND pv.plan_type_id = pt.plan_type_id
AND pt.plan_type_code = 'PROJECT_PLAN'"""
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The string in multiline so you need to use triple quotes or else you get an EOL. Also you shouldn't name a variable str
because it is already a function.
string = """SELECT DISTINCT
pv.project_id,
gps.period_name,
gps.period_year,
rbse.resource_source_id,
rbse.alias as resource_name,
DECODE(pjo_plan_version_utils.get_time_phased_code(pv.plan_version_id), 'G',
pld.quantity, pjo_plan_version_utils.spread_amount('L', pld.start_date, pld.end_date, gps.start_date, gps.end_date, pld.quantity)
) as hours,
(131.4-pld.quantity)/131.4 unallocated_percentage
FROM
pjf_rbs_elements rbse,
gl_period_statuses gps,
pjo_plan_line_details pld,
pjo_planning_elements pe,
pjo_plan_versions_vl pv,
pjo_plan_types_vl pt
WHERE
1=1
AND pe.rbs_element_id = rbse.rbs_element_id
AND pld.planning_element_id = pe.planning_element_id
AND pv.plan_version_id = pe.plan_version_id
AND pv.current_plan_status_flag = 'Y'
AND ((gps.start_date <= pld.end_date)
AND (gps.end_date >= pld.start_date))
AND gps.adjustment_period_flag = 'N'
AND gps.application_id = 10037
AND pv.plan_type_id = pt.plan_type_id
AND pt.plan_type_code = 'PROJECT_PLAN'"""
The string in multiline so you need to use triple quotes or else you get an EOL. Also you shouldn't name a variable str
because it is already a function.
string = """SELECT DISTINCT
pv.project_id,
gps.period_name,
gps.period_year,
rbse.resource_source_id,
rbse.alias as resource_name,
DECODE(pjo_plan_version_utils.get_time_phased_code(pv.plan_version_id), 'G',
pld.quantity, pjo_plan_version_utils.spread_amount('L', pld.start_date, pld.end_date, gps.start_date, gps.end_date, pld.quantity)
) as hours,
(131.4-pld.quantity)/131.4 unallocated_percentage
FROM
pjf_rbs_elements rbse,
gl_period_statuses gps,
pjo_plan_line_details pld,
pjo_planning_elements pe,
pjo_plan_versions_vl pv,
pjo_plan_types_vl pt
WHERE
1=1
AND pe.rbs_element_id = rbse.rbs_element_id
AND pld.planning_element_id = pe.planning_element_id
AND pv.plan_version_id = pe.plan_version_id
AND pv.current_plan_status_flag = 'Y'
AND ((gps.start_date <= pld.end_date)
AND (gps.end_date >= pld.start_date))
AND gps.adjustment_period_flag = 'N'
AND gps.application_id = 10037
AND pv.plan_type_id = pt.plan_type_id
AND pt.plan_type_code = 'PROJECT_PLAN'"""
answered Nov 20 at 0:05
Joyal Mathew
302112
302112
add a comment |
add a comment |
up vote
1
down vote
Your string concatenation is incorrect.
Try something like this:
str = "line1"
"line2"
"line3"
#output : no spaces or new lines
line1line2line3
or (but you can not indent by doing below way, if you indent, the indentation becomes part of concatenation)
str = """line1
line2
line3"""
#output : new lines
line1
line2
line3
add a comment |
up vote
1
down vote
Your string concatenation is incorrect.
Try something like this:
str = "line1"
"line2"
"line3"
#output : no spaces or new lines
line1line2line3
or (but you can not indent by doing below way, if you indent, the indentation becomes part of concatenation)
str = """line1
line2
line3"""
#output : new lines
line1
line2
line3
add a comment |
up vote
1
down vote
up vote
1
down vote
Your string concatenation is incorrect.
Try something like this:
str = "line1"
"line2"
"line3"
#output : no spaces or new lines
line1line2line3
or (but you can not indent by doing below way, if you indent, the indentation becomes part of concatenation)
str = """line1
line2
line3"""
#output : new lines
line1
line2
line3
Your string concatenation is incorrect.
Try something like this:
str = "line1"
"line2"
"line3"
#output : no spaces or new lines
line1line2line3
or (but you can not indent by doing below way, if you indent, the indentation becomes part of concatenation)
str = """line1
line2
line3"""
#output : new lines
line1
line2
line3
edited Nov 20 at 0:18
answered Nov 20 at 0:04
Naidu
4,9043817
4,9043817
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384346%2fpython-how-can-i-input-a-sql-query-as-a-string-without-errors%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
replace each double quote with 3 single quotes, also change the parameter name
str
to maybequery_str
, asstr
is already used by python.– teng
Nov 20 at 0:04