zoukankan      html  css  js  c++  java
  • 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'')
    
  • 相关阅读:
    近来无聊 写了一个google音乐的下载器
    HTML编辑器 HtmlArea
    Html编辑器 TinyMCE
    IE浏览器自定义地址协议 通过B/S程序中打开C/S程序
    html 元素的 onpropertychange 事件
    asp.net 服务器控件防止重复提交表单
    用 BindingSource 绑定窗体中控件不失去焦点无法更新数据源的问题
    动态创建大量 html dom 元素时提升性能
    很黄的日期格式化。。。
    Asp.net 导出 .html,.txt 等格式的文件
  • 原文地址:https://www.cnblogs.com/kai-/p/13786795.html
Copyright © 2011-2022 走看看