问题
· 十二月 5, 2022

如何使用python连接cache2016数据库

公司需要统计数据,需要使用python连接数据库查询,请问一下如何使用python连接cache2016数据库

产品版本: Caché 2016.1
讨论 (1)2
登录或注册以继续

使用Python Binding,参见文档 https://cedocs.intersystems.com/ens20162/csp/docbook/DocBook.UI.Page.cls?KEY=GBPY_intro

    例如一些基本的操作:

    • 创建数据库连接
         conn = intersys.pythonbind.connection()
         conn.connect_now(url,user,password, None)
         database = intersys.pythonbind.database(conn)
    • 打开存在的对象
         person =  database.openid("Sample.Person",str(id),-1,-1)
    • 创建新的对象
         person =  database.create_new("Sample.Person", None)
    • 设置对象属性或者取值
         person.set("Name","Doe, Joe A")
         name = person.get("Name")
    • 运行一个方法
         answer = person.run_obj_method("Addition",[17,20])
    • 保存对象
         person.run_obj_method("%Save",[])
    • 获取保存对象的id 
         id = person.run_obj_method("%Id",[])
    • 执行一个查询 
         sqlstring ="SELECT ID, Name, DOB, SSN \
                     FROM SAMPLE.PERSON \
                     WHERE Name %STARTSWITH ?"
         query = intersys.pythonbind.query(database)
         query.prepare(sqlstring)
         query.set_par(1,"A")
         query.execute();
         while 1:
            cols = query.fetch([None])
            if len(cols) == 0: break
            print cols

    同时可以在安装目录的/dev/Python/samples/ 下查看更多例子,比如

    • CPTest6.py — Process the result set of a ByName query.
    • CPTest7.py — Process the result set of a dynamic SQL query.