python读取postgresql


def pgsql():
    ### pip install psycopg2
    import psycopg2
    ## 连接到一个给定的数据库
    conn = psycopg2.connect(database="jw", user="jw_biz",
                            password="jw_biz", host="172.16.4.15", port="5432")
    ## 建立游标,用来执行数据库操作
    cursor = conn.cursor()

    ## 执行SQL命令
    #cursor.execute("DROP TABLE test_conn")
    #cursor.execute("CREATE TABLE test_conn(id int, name text)")
    #cursor.execute("INSERT INTO test_conn values(1,'haha')")

    ## 提交SQL命令
    conn.commit()

    ## 执行SQL SELECT命令
    cursor.execute("select * from abc")

    ## 获取SELECT返回的元组
    rows = cursor.fetchall()
    for row in rows:
        print('id = ',row[0], 'name = ', row[1])

    ## 关闭游标
    cursor.close()

    ## 关闭数据库连接
    conn.close()

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM