|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | + |
| 6 | +from gaussdb import connect |
| 7 | + |
| 8 | +os.environ["GAUSSDB_IMPL"] = "python" |
| 9 | + |
| 10 | + |
| 11 | +def main(): |
| 12 | + dsn = os.environ.get("GAUSSDB_TEST_DSN") |
| 13 | + if not dsn: |
| 14 | + print("❌ Please set the GAUSSDB_TEST_DSN environment variable, for example:") |
| 15 | + print( |
| 16 | + ' export GAUSSDB_TEST_DSN="dbname=test01 user=root password=***' |
| 17 | + 'host=** port=8000"' |
| 18 | + ) |
| 19 | + sys.exit(1) |
| 20 | + |
| 21 | + drop_table_sql = "DROP TABLE IF EXISTS test" |
| 22 | + create_table_sql = ( |
| 23 | + "CREATE TABLE test (id serial PRIMARY KEY, num integer, data text)" |
| 24 | + ) |
| 25 | + insert_data_sql = "INSERT INTO test (num, data) VALUES (%s, %s)" |
| 26 | + update_data_sql = "UPDATE test SET data = 'gaussdb' WHERE num = 2" |
| 27 | + select_sql = "SELECT * FROM test" |
| 28 | + |
| 29 | + with connect(dsn, connect_timeout=10, application_name="gaussdb-demo") as conn: |
| 30 | + |
| 31 | + with conn.cursor() as cur: |
| 32 | + server_version = conn.execute("select version()").fetchall()[0][0] |
| 33 | + print(f"✅ Connected. Server version: {server_version}") |
| 34 | + print(f"Vendor: {conn.info.vendor}, Version: {conn.info.server_version}") |
| 35 | + |
| 36 | + cur.execute(drop_table_sql) |
| 37 | + cur.execute(create_table_sql) |
| 38 | + cur.execute(insert_data_sql, (1, "hello")) |
| 39 | + cur.execute(insert_data_sql, (2, "world")) |
| 40 | + |
| 41 | + cur.execute(select_sql) |
| 42 | + print("📄origin: ", cur.fetchall()) |
| 43 | + |
| 44 | + cur.execute(update_data_sql) |
| 45 | + cur.execute(select_sql) |
| 46 | + print("📄update: ", cur.fetchall()) |
| 47 | + |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + main() |
0 commit comments