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], ‘\n‘)

# 关闭游标
cursor.close()

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

        

 

sqlalchemy处理麻烦, 原sql处理

上一篇:数据库程序设计第八天--隔离人员权限


下一篇:K8s之MySQL实现数据持久化