有人可以帮我理解如何选择sql RESULT中显示的字段值吗?我想使用该值来运行一个新的不同查询。
我正在使用Python连接html,apache和sql server来创建一个运行SQL查询并显示结果的网站。一旦我得到结果,我想在新的SQL查询中使用一列的值。有没有办法做到这一点,而不是手动在表单中键入值?
Code1 :下面的代码执行数据库调用,根据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()
单击提交按钮时页面正在重新加载,但数据库未更新。不确定出了什么问题!
答案 0 :(得分:0)
这里有2点:
cur.fetchall返回一个元组元组。 results[0][0]
将是查询返回的数据中第一行的 OpinionID 。 results[1][0]
将成为第二行的 OpinionID 等。results [0][7]
将成为第一行的状态等。
不要使用Python的字符串连接构建查询。使用安全方法。
my_first_OpinionID = results[0][0]
my_first_statusID = results[0][7]
cur.execute("UPDATE NewIssueOpinion SET Status = ? WHERE OpinonID = ?", (my_first_OpinionID, my_first_statusID))
您可能会发现这些链接很有用:
<http://zetcode.com/db/mysqlpython/>
<http://bobby-tables.com/python>
<http://sebastianraschka.com/Articles/2014_sqlite_in_python_tutorial.html>
答案 1 :(得分:0)
表示cursor.execute(SQLQuery,multi = True): 如果result.with_rows:
在这里,上次我试图连接到db并打印我使用的结果