【发布时间】:2017-07-19 22:27:04
【问题描述】:
有人可以帮我理解如何选择在 sql RESULT 中显示的字段值吗?我想使用该值来运行新的不同查询。
我正在使用 Python 连接 html、apache 和 sql server 来创建一个将运行 SQL 查询并显示结果的网站。一旦我得到结果,我想在新的 SQL 查询中使用一列的值。有没有办法做到这一点,而不是在表单中手动输入值?
代码 1:下面的代码执行数据库调用,根据 sql 查询显示结果。
# prepare a cursor object using cursor() method
cursor = db1.cursor()
SQLQuery = ("SELECT I.OpinionID,
I.TimeStamp,
I.Strategy,
I.ExpectedTradingPrice,
I.ExpectedDate,
I.CPAMAnalyst,
I.Conviction,
I.Status,
N.DebtTicker,
N.InstrumentType,
N.ExpectedMaturity,
N.OrderSize,
N.Allocation
From [CpamTestAugustTestSteve].[dbo].[NewIssueOpinion] I
INNER JOIN [CpamTestAugustTestSteve].[dbo].[NewIssue] N ON I.NewIssueID = N.NewIssueID
WHERE N.ExpectedIssueDate >= GetDate()-30
AND I.Status = 'Suggested' AND I.Active = 1;")
#Execute the SQL command
cursor.execute(SQLQuery)
#Fetch all the rows in a list of lists.
results=cursor.fetchall()
y = len(results)
print "<br> <br>"
print "<body2>"
print """<form action="http://localhost/NewIssueStatusUpdate.py" method="post">
<div> <input type="Submit" class= "submitbutton" value="Submit" /> </div> <br> <br>
</form>"""
if not cursor.rowcount:
print "<b> <mark> NO RECORDS FOUND!!</mark> </b>"
else:
print "<TABLE>"
print "<tablebody>"
print"<tr>"
print "<tr><th>S.No</th><th>OpinionID</th> <th>TimeStamp</th> <th>Strategy</th> <th>ExpectedTradingPrice</th> <th>ExpectedDate</th> <th>CPAMAnalyst</th> <th>Conviction</th> <th>Status</th> <th>DebtTicker</th> <th>InstrumentType</th> <th>ExpectedMaturity</th> <th>OrderSize</th> <th>Allocation</th><th>New Status</th>"
for p in range(y):
print "<tr><td>%s</td>" %(p+1)
for q in range(len(results[p])):
r= results[p][q]
print "<td> %s </td>" % (r)
print "<td><select id='statusSelect' onchange='myChangeHandler(this)'><option value='Suggested'>Select</option><option value='accept'>Accept</option><option value='reject'>Reject</option><option value='terminated'>Terminated</option></select></td>"
print "</TABLE>"
from tabulate import tabulate
我在“新状态”上方的结果中添加了一个自定义列,我们可以在其中选择要分配给记录的新状态。
我想写一个可以用来更新数据库的代码。查询是:
"UPDATE NewIssueOpinion " & _
"SET Status = '" & Status & "' " & _ #Getting Status using getformvalue.
"WHERE OpinionID = " & OpinionID & ";" #How can we select the Opinion ID value from the result displayed from code1.
更新代码:
# prepare a cursor object using cursor() method
cursor = db1.cursor()
SQLQuery = ("SELECT I.OpinionID,
I.TimeStamp,
I.Strategy,
I.ExpectedTradingPrice,
I.ExpectedDate,
I.CPAMAnalyst,
I.Conviction,
I.Status,
N.DebtTicker,
N.InstrumentType,
N.ExpectedMaturity,
N.OrderSize,
N.Allocation
From [CpamTestAugustTestSteve].[dbo].[NewIssueOpinion] I
INNER JOIN [CpamTestAugustTestSteve].[dbo].[NewIssue] N ON I.NewIssueID = N.NewIssueID
WHERE N.ExpectedIssueDate >= GetDate()-30
AND I.Status = 'Suggested' AND I.Active = 1;")
#Execute the SQL command
cursor.execute(SQLQuery)
#Fetch all the rows in a list of lists.
results=cursor.fetchall()
y = len(results)
#define function
def Review():
cursor.close()
cursor = db1.cursor()
for result in results:
my_OpinionID = result[1]
my_StatusID = result[14]
cursor.execute('UPDATE [CpamTestAugustTestSteve].[dbo].[NewIssueOpinion] SET Status = ? WHERE OpinionID = ?', (my_StatusID, my_OpinionID))
db1.commit()
cursor.close()
db1.close()
location.reload()
print "<br> <br>"
print "<body2>"
print "<form method='post' onclick='return Review();'>"
print "<div> <input type='Submit' class= 'submitbutton' value='Submit' /> </div> <br> <br>"
print "</form>"
if not cursor.rowcount:
print "<b> <mark> NO RECORDS FOUND!!</mark> </b>"
else:
print "<TABLE>"
print "<tablebody>"
print"<tr>"
print "<tr><th>S.No</th><th>OpinionID</th> <th>TimeStamp</th> <th>Strategy</th> <th>ExpectedTradingPrice</th> <th>ExpectedDate</th> <th>CPAMAnalyst</th> <th>Conviction</th> <th>Status</th> <th>DebtTicker</th> <th>InstrumentType</th> <th>ExpectedMaturity</th> <th>OrderSize</th> <th>Allocation</th><th>New Status</th>"
for p in range(y):
print "<tr><td>%s</td>" %(p+1)
for q in range(len(results[p])):
r= results[p][q]
print "<td> %s </td>" % (r)
print "<td><select id='statusSelect'><option value='Suggested'>Select</option><option value='Accepted'>Accept</option><option value='rejected'>Reject</option><option value='terminated'>Terminated</option></select></td>"
print "</TABLE>"
from tabulate import tabulate
print "</body2>"
db1.commit()
当我单击提交按钮时,页面正在重新加载,但数据库没有更新。不知道出了什么问题!
【问题讨论】:
-
嗨,艾迪。我知道你会发现这里有很多人喜欢帮助其他处于困境的人,但都懒得为你写一个完整的程序。如果您通过添加您目前使用的代码以及有问题或缺失的部分来编辑您的帖子,它可能会有所不同(并且反对票将停止)
-
非常感谢 Ettore,:) 我已更新问题以添加代码。我非常感谢您的回复,以帮助我获得更好的帮助/回复。我也是 Stackoverflow 的新手。到目前为止,我只用它来研究现有问题,最近我开始提出问题。仍在想办法做到这一点。
-
读者确实想在这里提供帮助,但是对于社区的新手来说显然不能成为乞求志愿者免费工作的借口。明白某事只对发帖人来说是紧急的,而对其他人根本没有,这一点很重要。
-
谢谢哈弗。不知道知识共享请求如何被解释为乞讨!!
-
我赞成答案,但似乎我需要 15+ 的声誉才能显示我的投票。他们正在存储我的反馈但没有显示。我正在尝试我在这里得到的所有答案,但我的代码仍然没有工作。可能是缺少了什么。我会根据输入不断更新我的代码。
标签: python html css sql-server