zoukankan      html  css  js  c++  java
  • sqlalchemy处理麻烦, 原sql处理

    一.处理:

    # -*- coding: utf-8-*-
    import psycopg2
    
    ## 连接到一个给定的数据库
    conn = psycopg2.connect(database="******", user="******",password="******", host="127.0.0.1", port="5432")
    ## 建立游标,用来执行数据库操作
    cursor = conn.cursor()
    print("====cursor====")
    print(cursor)
    ## 执行SQL SELECT命令
    # cursor.execute("select * from accounts")
    # cursor.execute("select * from developer_key_account_bindings")
    #cursor.execute("select count(*) from users")
    
    sql = "select order_contexts.context_id, count(order_contexts.context_id) from mooc_orders inner join order_contexts on mooc_orders.id=order_contexts.mooc_order_id where mooc_orders.college_id=1 and order_contexts.context_type='book' GROUP BY order_contexts.context_id ORDER BY count(order_contexts.context_id) DESC LIMIT 10;"
    cursor.execute(sql)
    
    # t = cursor.execute("insert into developer_key_account_bindings(id,account_id,developer_key_id,workflow_state) values(1, 1, 2, 'on')")
    # print("===t=====")
    # print(t)
    ## 获取SELECT返回的元组
    rows = cursor.fetchall()
    print("=========rows=============")
    print(rows)
    #for row in rows:
    #    print('id = ',row[0], '2 = ', row[1],'3 = ', row[2], '
    ')
    
    # 关闭游标
    cursor.close()
    
    # 关闭数据库连接
    conn.close()
    
            
  • 相关阅读:
    U-Boot新手入门
    安装交叉编译工具
    Makefile 工程管理
    gcc基本用法
    poj 3264 Balanced Lineup
    hdoj 1166 敌兵布阵
    poj 1363 Rails
    poj 1028 Web Navigation
    zoj 3621 Factorial Problem in Base K
    poj1861最小生成树
  • 原文地址:https://www.cnblogs.com/rixian/p/13541500.html
Copyright © 2011-2022 走看看