• postgresql基础操作


    postgresql 基础操作

    查询

    import psycopg2.extras
    import json
    conn = psycopg2.connect(database='web_fetching', user='crawler', password='crawler', host='0.0.0.0', port='5432')
    
    cursor = conn.cursor()
    # sql = 'ALTER TABLE phone ALTER COLUMN name type varchar(9999) ;'
    sql = 'select * from phonedb '
    resp = cursor.execute(sql)
    
    err_list = []
    
    rows = cursor.fetchall()
    for row in rows:
        print(row)
    
    conn.commit()
    conn.close()
    
    

    创建

    import psycopg2.extras
    import psycopg2
    conn = psycopg2.connect(database='web_fetching', user='crawler', password='crawler', host='0.0.0.0', port='5432')
    
    
    cursor = conn.cursor()
    # sql = 'ALTER TABLE phone ALTER COLUMN id type varchar(9999) ;'
    
    cursor.execute("""CREATE TABLE phonedb 
           (ID INT PRIMARY KEY NOT NULL,
           NAME TEXT ,
           INFO TEXT )"""
                   )
    
    conn.commit()
    conn.close()
    
    
    

    删除表

    import psycopg2.extras
    conn = psycopg2.connect(database='web_fetching', user='crawler', password='crawler', host='0.0.0.0', port='5432')
    
    
    cursor = conn.cursor()
    # sql = 'ALTER TABLE phone ALTER COLUMN id type varchar(9999) ;'
    sql = 'drop table phonedb'
    resp = cursor.execute(sql)
    
    # rows = cursor.fetchall()
    # for row in rows:
    #     print(row)
    
    conn.commit()
    conn.close()
    
    print(f'')
    

    查看表结构

    import psycopg2.extras
    conn = psycopg2.connect(database='web_fetching', user='crawler', password='crawler', host='0.0.0.0', port='5432')
    
    cursor = conn.cursor()
    sql = '''
    SELECT a.attnum,
    a.attname AS field,
    t.typname AS type,
    a.attlen AS length,
    a.atttypmod AS lengthvar,
    a.attnotnull AS notnull,
    b.description AS comment
    FROM pg_class c,
    pg_attribute a
    LEFT OUTER JOIN pg_description b ON a.attrelid=b.objoid AND a.attnum = b.objsubid,
    pg_type t
    WHERE c.relname = '库名称'
    and a.attnum > 0
    and a.attrelid = c.oid
    and a.atttypid = t.oid
    ORDER BY a.attnum;
    '''
    # sql = 'delete from phone where id=3'
    resp = cursor.execute(sql)
    
    rows = cursor.fetchall()
    for row in rows:
        print(row)
    
    conn.commit()
    conn.close()
    
    print(f'')
    
  • 相关阅读:
    函数
    文件
    字符编码
    卸载python
    mysql修改密码
    playbook
    expect无交互操作
    用户认证
    配置并访问NFS共享
    配置Multipath多路径环境
  • 原文地址:https://www.cnblogs.com/kai-/p/13786795.html
Copyright © 2020-2023  润新知