Store SQL query as a variable with a CGI Python script
I'm a newbie to python and cgi.
How do I store an SQL query as a python variable in a cgi script?
Currently, my script is:
#!/usr/bin/python3
#enable debugging - BEGIN
import cgi
import cgitb
cgitb.enable()
#enable debugging - END
#HTTP Headers - BEGIN
#set encoding to UTF-8
print("Content-Type: text/html;charset=utf-8")
print()
#HTTP Headers - END
#HTML Content - BEGIN
print("<h1>Form Test</h1>")
data = cgi.FieldStorage()
username = data['username'].value
print("<p><b>Hello, " + username + "</b></p>")
import sqlite3
conn = sqlite3.connect('/home/user/BankingApp.db')
c = conn.cursor()
#Display SUM of Balances:
print ("<h1>Bank's Total Balance</h1><br />")
for row in c.execute('SELECT SUM(Balance) FROM Accounts')
print ("Bank's Balance: $")
print (row)
It produces the output:
Bank's Balance: $ (10000.0,)
I want to print out the SUM without the parenthesis so that it reads like:
Bank's Balance: $ 10000.0
I figure if I can store the SUM query into a variable, I can format the output however I want.
Thank you.
python python-3.x cgi
add a comment |
I'm a newbie to python and cgi.
How do I store an SQL query as a python variable in a cgi script?
Currently, my script is:
#!/usr/bin/python3
#enable debugging - BEGIN
import cgi
import cgitb
cgitb.enable()
#enable debugging - END
#HTTP Headers - BEGIN
#set encoding to UTF-8
print("Content-Type: text/html;charset=utf-8")
print()
#HTTP Headers - END
#HTML Content - BEGIN
print("<h1>Form Test</h1>")
data = cgi.FieldStorage()
username = data['username'].value
print("<p><b>Hello, " + username + "</b></p>")
import sqlite3
conn = sqlite3.connect('/home/user/BankingApp.db')
c = conn.cursor()
#Display SUM of Balances:
print ("<h1>Bank's Total Balance</h1><br />")
for row in c.execute('SELECT SUM(Balance) FROM Accounts')
print ("Bank's Balance: $")
print (row)
It produces the output:
Bank's Balance: $ (10000.0,)
I want to print out the SUM without the parenthesis so that it reads like:
Bank's Balance: $ 10000.0
I figure if I can store the SUM query into a variable, I can format the output however I want.
Thank you.
python python-3.x cgi
add a comment |
I'm a newbie to python and cgi.
How do I store an SQL query as a python variable in a cgi script?
Currently, my script is:
#!/usr/bin/python3
#enable debugging - BEGIN
import cgi
import cgitb
cgitb.enable()
#enable debugging - END
#HTTP Headers - BEGIN
#set encoding to UTF-8
print("Content-Type: text/html;charset=utf-8")
print()
#HTTP Headers - END
#HTML Content - BEGIN
print("<h1>Form Test</h1>")
data = cgi.FieldStorage()
username = data['username'].value
print("<p><b>Hello, " + username + "</b></p>")
import sqlite3
conn = sqlite3.connect('/home/user/BankingApp.db')
c = conn.cursor()
#Display SUM of Balances:
print ("<h1>Bank's Total Balance</h1><br />")
for row in c.execute('SELECT SUM(Balance) FROM Accounts')
print ("Bank's Balance: $")
print (row)
It produces the output:
Bank's Balance: $ (10000.0,)
I want to print out the SUM without the parenthesis so that it reads like:
Bank's Balance: $ 10000.0
I figure if I can store the SUM query into a variable, I can format the output however I want.
Thank you.
python python-3.x cgi
I'm a newbie to python and cgi.
How do I store an SQL query as a python variable in a cgi script?
Currently, my script is:
#!/usr/bin/python3
#enable debugging - BEGIN
import cgi
import cgitb
cgitb.enable()
#enable debugging - END
#HTTP Headers - BEGIN
#set encoding to UTF-8
print("Content-Type: text/html;charset=utf-8")
print()
#HTTP Headers - END
#HTML Content - BEGIN
print("<h1>Form Test</h1>")
data = cgi.FieldStorage()
username = data['username'].value
print("<p><b>Hello, " + username + "</b></p>")
import sqlite3
conn = sqlite3.connect('/home/user/BankingApp.db')
c = conn.cursor()
#Display SUM of Balances:
print ("<h1>Bank's Total Balance</h1><br />")
for row in c.execute('SELECT SUM(Balance) FROM Accounts')
print ("Bank's Balance: $")
print (row)
It produces the output:
Bank's Balance: $ (10000.0,)
I want to print out the SUM without the parenthesis so that it reads like:
Bank's Balance: $ 10000.0
I figure if I can store the SUM query into a variable, I can format the output however I want.
Thank you.
python python-3.x cgi
python python-3.x cgi
edited Nov 25 '18 at 5:31
jackolantirn
asked Nov 25 '18 at 1:17
jackolantirnjackolantirn
113
113
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You could use a simple regular expression to remove the parenthesis and comma.
In [7]: import re
In [8]: x = '(10000.0,) '
In [9]: re.sub(r'[(|)|,]', '', x)
Out[9]: '10000.0 '
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',
autoActivateHeartbeat: false,
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%2f53463877%2fstore-sql-query-as-a-variable-with-a-cgi-python-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could use a simple regular expression to remove the parenthesis and comma.
In [7]: import re
In [8]: x = '(10000.0,) '
In [9]: re.sub(r'[(|)|,]', '', x)
Out[9]: '10000.0 '
add a comment |
You could use a simple regular expression to remove the parenthesis and comma.
In [7]: import re
In [8]: x = '(10000.0,) '
In [9]: re.sub(r'[(|)|,]', '', x)
Out[9]: '10000.0 '
add a comment |
You could use a simple regular expression to remove the parenthesis and comma.
In [7]: import re
In [8]: x = '(10000.0,) '
In [9]: re.sub(r'[(|)|,]', '', x)
Out[9]: '10000.0 '
You could use a simple regular expression to remove the parenthesis and comma.
In [7]: import re
In [8]: x = '(10000.0,) '
In [9]: re.sub(r'[(|)|,]', '', x)
Out[9]: '10000.0 '
answered Nov 25 '18 at 2:55
kstullichkstullich
213318
213318
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.
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%2f53463877%2fstore-sql-query-as-a-variable-with-a-cgi-python-script%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