2017-02-28 137 views
-5

有人可以帮我理解如何选择显示在SQL RESULT中的字段值吗?我想用这个值来运行一个新的不同的查询。从SQL结果中获取数据

我使用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() 

当我点击提交按钮的页面重新加载,但数据库未更新。不知道出了什么问题!

+2

嗨阿迪。我知道你会在这里找到很多喜欢帮助别人的人,但都懒得为你写一个完整的程序。如果您通过添加迄今为止使用的代码以及有问题或缺少的部分来编辑帖子,它可能会有所不同(并且downvotes将会停止) –

+1

非常感谢Ettore,:)我已经更新了要添加的问题代码。我非常感谢你的回应,帮助我获得更好的帮助/回应。我也是新来的Stackoverflow。到目前为止,我只用它来做研究思考存在的问题,最近我开始提问。仍然想出最好的方法来做到这一点。 – AddyB

+1

读者确实想在这里帮忙,但对社区来说,新人显然不是乞求志愿者免费工作的借口。了解某些东西只是海报的紧急事件,而对于其他任何人来说都很重要。 – halfer

回答

0

2点这里:

cur.fetchall返回元组的元组。 results[0][0]将针对您的数据中由查询返回的第一行的OpinionIDresults[1][0]将是OpinionID第二等results [0][7]将是第一行状态

DO使用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> 

+0

我相信,代码将只读取第一行中的意见ID和状态ID,我希望能够对所有显示为结果的行执行此操作。 – AddyB

+0

您通常会如何阅读元组或列表? '结果结果:my_OpinionID = result [0],my_StatusID [7]'等等。在我的回答中,第一个索引是* row *,第二个索引是行*中的*位置。 – Alan

+0

P.S.更新时不要忘记提交交易! – Alan

0

的结果cursor.execute(的SQLQuery,多=真): 如果result.with_rows:

相同在这里,上次我试图连接到分贝和打印结果我使用