Skip to content

Commit becb77a

Browse files
authored
fix(demo): make python_quickstart/demo.py work on sqlalchemy 2.0.27 (#3979)
* fix(demo): replace connection.execute with connection.exec_driver_sql to make python_quickstart/demo.py work * fix(.github): docker-compose command not found
1 parent 2b89bb3 commit becb77a

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

.github/workflows/openmldb-docker.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ jobs:
3737
- name: Setup Docker Buildx
3838
uses: docker/setup-buildx-action@v2
3939

40+
- name: Setup docker-compose
41+
uses: KengoTODA/actions-setup-docker-compose@v1.2.2
42+
with:
43+
version: '2.29.2'
44+
4045
- name: Docker Compose Test
4146
working-directory: demo
4247
run: |
43-
docker-compose -f docker-compose.test.yml -- up --exit-code-from sut
48+
docker compose -f docker-compose.test.yml up --exit-code-from sut
4449
4550
- name: Decide Push
4651
run: |

demo/python_quickstart/demo.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121

2222
import openmldb.dbapi
2323

24+
2425
# dbapi接口如果执行失败,会抛出异常,本例不捕获异常,暴露错误
2526

2627
# 连接集群版OpenMLDB
2728
db = openmldb.dbapi.connect(zk="127.0.0.1:2181", zkPath="/openmldb")
2829

2930
# 连接单机版OpenMLDB
30-
# db = openmldb.dbapi.connect(host="$host", port="$port")
31+
# db = openmldb.dbapi.connect(host="127.0.0.1", port="6527")
3132

3233
cursor = db.cursor()
3334

@@ -104,33 +105,33 @@
104105

105106
### 3.2 创建数据库
106107
try:
107-
connection.execute("CREATE DATABASE db1")
108+
connection.exec_driver_sql("CREATE DATABASE db1")
108109
except Exception as e:
109110
print(e)
110111

111-
connection.execute("USE db1")
112+
connection.exec_driver_sql("USE db1")
112113

113114
### 3.3 创建表
114115
try:
115-
connection.execute(
116+
connection.exec_driver_sql(
116117
"CREATE TABLE t1 ( col1 bigint, col2 date, col3 string, col4 string, col5 int, index(key=col3, ts=col1))"
117118
)
118119
except Exception as e:
119120
print(e)
120121

121122
### 3.4 插入数据到表中
122123
try:
123-
connection.execute(
124+
connection.exec_driver_sql(
124125
"INSERT INTO t1 VALUES(1000, '2020-12-25', 'guangdon', 'shenzhen', 1);"
125126
)
126127
except Exception as e:
127128
print(e)
128129

129-
# 使用`connection.execute(ddl, data)`接口执行带planceholder的SQL的插入语句,可以动态指定插入数据,也可插入多行:
130+
# 使用`connection.exec_driver_sql(ddl, data)`接口执行带planceholder的SQL的插入语句,可以动态指定插入数据,也可插入多行:
130131
try:
131132
insert = "INSERT INTO t1 VALUES(1002, '2020-12-27', ?, ?, 3);"
132-
connection.execute(insert, ({"col3": "fujian", "col4": "fuzhou"}))
133-
connection.execute(
133+
connection.exec_driver_sql(insert, ({"col3": "fujian", "col4": "fuzhou"}))
134+
connection.exec_driver_sql(
134135
insert,
135136
[
136137
{"col3": "jiangsu", "col4": "nanjing"},
@@ -142,11 +143,11 @@
142143

143144
### 3.5 执行SQL批式查询
144145
try:
145-
rs = connection.execute("SELECT * FROM t1")
146+
rs = connection.exec_driver_sql("SELECT * FROM t1")
146147
for row in rs:
147148
print(row)
148-
rs = connection.execute("SELECT * FROM t1 WHERE col3 = ?;", ("hefei"))
149-
rs = connection.execute(
149+
rs = connection.exec_driver_sql("SELECT * FROM t1 WHERE col3 = ?;", ("hefei"))
150+
rs = connection.exec_driver_sql(
150151
"SELECT * FROM t1 WHERE col3 = ?;", [("hefei"), ("shanghai")]
151152
)
152153
except Exception as e:
@@ -155,9 +156,9 @@
155156

156157
### 3.6 执行SQL请求式查询
157158

158-
# 使用`connection.execute(sql, request)`接口执行SQL批式查询语句:请求式查询,可以把输入数据放到execute的第二个参数中
159+
# 使用`connection.exec_driver_sql(sql, request)`接口执行SQL批式查询语句:请求式查询,可以把输入数据放到execute的第二个参数中
159160
try:
160-
rs = connection.execute(
161+
rs = connection.exec_driver_sql(
161162
"SELECT * FROM t1",
162163
(
163164
{
@@ -174,12 +175,12 @@
174175

175176
### 3.7 删除表
176177
try:
177-
connection.execute("DROP TABLE t1")
178+
connection.exec_driver_sql("DROP TABLE t1")
178179
except Exception as e:
179180
print(e)
180181

181182
### 3.8 删除数据库
182183
try:
183-
connection.execute("DROP DATABASE db1")
184+
connection.exec_driver_sql("DROP DATABASE db1")
184185
except Exception as e:
185186
print(e)

0 commit comments

Comments
 (0)