Python连接示例

<p>Python连接示例如下:</p> <table border="0" cellpadding="0" cellspacing="0" style="width:788px"> <tbody> <tr> <td style="background-color:#f7f8fa; width:784px"> <p><span style="font-family:Consolas">import psycopg2</span></p> <p>&nbsp;</p> <p><span style="font-family:Consolas">connect_list = {&quot;host&quot;:&quot;<strong>&lt;</strong><strong>域名&gt;</strong>&quot;,</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;port&quot;:<strong>&lt;</strong><strong>端口&gt;</strong>,</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;user&quot;:&quot;username&quot;,</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;password&quot;:&quot; password &quot;,</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;dbname&quot;:&quot;<strong>&lt;Database&gt;</strong>&quot;</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p> <p><span style="font-family:Consolas">connect = psycopg2.connect(**connect_list)</span></p> <p><span style="font-family:Consolas"># 使用cursor()方法获取操作游标</span></p> <p><span style="font-family:Consolas">cursor=connect.cursor()</span></p> <p><span style="font-family:Consolas"># 使用execute方法执行SQL语句</span></p> <p><span style="font-family:Consolas">cursor.execute(&#39;SELECT VERSION()&#39;)</span></p> <p><span style="font-family:Consolas"># 使用 fetchone() 方法获取一条数据</span></p> <p><span style="font-family:Consolas">data = cursor.fetchone()</span></p> <p><span style="font-family:Consolas">print &quot;Database version : %s &quot; % data</span></p> <p><span style="font-family:Consolas"># 关闭数据库连接</span></p> <p><span style="font-family:Consolas">connect.close()</span></p> </td> </tr> </tbody> </table>
以上内容是否解决了您的问题?
请补全提交信息!
咨询·建议

电话咨询

400-151-8800

邮件咨询

cloud@pingan.com

在线客服

工单支持

解决云产品相关技术问题