From 60ad0a32adafcef2adb59c649a3464f93d460d33 Mon Sep 17 00:00:00 2001 From: shouren Date: Fri, 6 Sep 2024 17:42:45 +0800 Subject: [PATCH] fix(demo): replace connection.execute with connection.exec_driver_sql to make python_quickstart/demo.py works --- demo/python_quickstart/demo.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/demo/python_quickstart/demo.py b/demo/python_quickstart/demo.py index d8d672476ac..c1a710a2de2 100644 --- a/demo/python_quickstart/demo.py +++ b/demo/python_quickstart/demo.py @@ -21,13 +21,14 @@ import openmldb.dbapi + # dbapi接口如果执行失败,会抛出异常,本例不捕获异常,暴露错误 # 连接集群版OpenMLDB db = openmldb.dbapi.connect(zk="127.0.0.1:2181", zkPath="/openmldb") # 连接单机版OpenMLDB -# db = openmldb.dbapi.connect(host="$host", port="$port") +# db = openmldb.dbapi.connect(host="127.0.0.1", port="6527") cursor = db.cursor() @@ -104,15 +105,15 @@ ### 3.2 创建数据库 try: - connection.execute("CREATE DATABASE db1") + connection.exec_driver_sql("CREATE DATABASE db1") except Exception as e: print(e) -connection.execute("USE db1") +connection.exec_driver_sql("USE db1") ### 3.3 创建表 try: - connection.execute( + connection.exec_driver_sql( "CREATE TABLE t1 ( col1 bigint, col2 date, col3 string, col4 string, col5 int, index(key=col3, ts=col1))" ) except Exception as e: @@ -120,17 +121,17 @@ ### 3.4 插入数据到表中 try: - connection.execute( + connection.exec_driver_sql( "INSERT INTO t1 VALUES(1000, '2020-12-25', 'guangdon', 'shenzhen', 1);" ) except Exception as e: print(e) -# 使用`connection.execute(ddl, data)`接口执行带planceholder的SQL的插入语句,可以动态指定插入数据,也可插入多行: +# 使用`connection.exec_driver_sql(ddl, data)`接口执行带planceholder的SQL的插入语句,可以动态指定插入数据,也可插入多行: try: insert = "INSERT INTO t1 VALUES(1002, '2020-12-27', ?, ?, 3);" - connection.execute(insert, ({"col3": "fujian", "col4": "fuzhou"})) - connection.execute( + connection.exec_driver_sql(insert, ({"col3": "fujian", "col4": "fuzhou"})) + connection.exec_driver_sql( insert, [ {"col3": "jiangsu", "col4": "nanjing"}, @@ -142,11 +143,11 @@ ### 3.5 执行SQL批式查询 try: - rs = connection.execute("SELECT * FROM t1") + rs = connection.exec_driver_sql("SELECT * FROM t1") for row in rs: print(row) - rs = connection.execute("SELECT * FROM t1 WHERE col3 = ?;", ("hefei")) - rs = connection.execute( + rs = connection.exec_driver_sql("SELECT * FROM t1 WHERE col3 = ?;", ("hefei")) + rs = connection.exec_driver_sql( "SELECT * FROM t1 WHERE col3 = ?;", [("hefei"), ("shanghai")] ) except Exception as e: @@ -155,9 +156,9 @@ ### 3.6 执行SQL请求式查询 -# 使用`connection.execute(sql, request)`接口执行SQL批式查询语句:请求式查询,可以把输入数据放到execute的第二个参数中 +# 使用`connection.exec_driver_sql(sql, request)`接口执行SQL批式查询语句:请求式查询,可以把输入数据放到execute的第二个参数中 try: - rs = connection.execute( + rs = connection.exec_driver_sql( "SELECT * FROM t1", ( { @@ -174,12 +175,12 @@ ### 3.7 删除表 try: - connection.execute("DROP TABLE t1") + connection.exec_driver_sql("DROP TABLE t1") except Exception as e: print(e) ### 3.8 删除数据库 try: - connection.execute("DROP DATABASE db1") + connection.exec_driver_sql("DROP DATABASE db1") except Exception as e: print(e)