Skip to content

Commit

Permalink
fix(demo): make python_quickstart/demo.py work on sqlalchemy 2.0.27 (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
Shouren committed Sep 13, 2024
1 parent 2b89bb3 commit becb77a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/openmldb-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ jobs:
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Setup docker-compose
uses: KengoTODA/[email protected]
with:
version: '2.29.2'

- name: Docker Compose Test
working-directory: demo
run: |
docker-compose -f docker-compose.test.yml -- up --exit-code-from sut
docker compose -f docker-compose.test.yml up --exit-code-from sut
- name: Decide Push
run: |
Expand Down
31 changes: 16 additions & 15 deletions demo/python_quickstart/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -104,33 +105,33 @@

### 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:
print(e)

### 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"},
Expand All @@ -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:
Expand All @@ -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",
(
{
Expand All @@ -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)

0 comments on commit becb77a

Please sign in to comment.