zoukankan      html  css  js  c++  java
  • python连接postgres数据库

    第一步:下载   psycopg2 库

    pip install psycopg2
    

    第二步:引入 psycopg2 库

    import psycopg2
    

    第三步:连接postgres数据库 

    conn = psycopg2.connect(database="tables", user="postgres", password="***", host="127.0.0.1", port="5432")
    cur = conn.cursor()
    

    第四步:执行sql语句  

    # 执行sql语句 查询
    cur.execute("SELECT name, years from students")
    # 获取所有
    rows = cur.fetchall()
    
    
    # 提交 , 关闭数据库
    # 如果只是查询可以只是 conn .close()    
    # 如果是修改了数据库就要都写 
    conn .commit()
    cur.close()
    conn .close()    
    

    第五步:完成postgres数据库的连接,正常使用即可。

  • 相关阅读:
    抽象工厂模式
    工厂方法模式
    单例模式
    适配器模式
    外观模式
    简单工厂模式
    设计模式开篇闲谈
    android ui更新
    android获取Context
    android 事件绑定
  • 原文地址:https://www.cnblogs.com/lxz123/p/13931139.html
Copyright © 2011-2022 走看看