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> </p>
<p><span style="font-family:Consolas">connect_list = {"host":"<strong><</strong><strong>域名></strong>",</span></p>
<p><span style="font-family:Consolas"> "port":<strong><</strong><strong>端口></strong>,</span></p>
<p><span style="font-family:Consolas"> "user":"username",</span></p>
<p><span style="font-family:Consolas"> "password":" password ",</span></p>
<p><span style="font-family:Consolas"> "dbname":"<strong><Database></strong>"</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> </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('SELECT VERSION()')</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 "Database version : %s " % 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>
提交成功!非常感谢您的反馈,我们会继续努力做到更好!