diff --git a/README.rst b/README.rst index 48c70d724..308c622cc 100644 --- a/README.rst +++ b/README.rst @@ -55,6 +55,9 @@ Installation from PyPI: pip install gaussdb-pool python -c "import gaussdb; print(gaussdb.__version__)" # Outputs: 1.0.0.dev2 + # Run demo + python ./example/demo.py + You can also clone this repository to develop GaussDB:: # Create a new Python virtual environment in the .venv directory @@ -111,8 +114,8 @@ Please add ``--config-settings editable_mode=strict`` to the ``pip install Now hack away! You can run the tests using on GaussDB:: - # Create a new database named "test" with PostgreSQL compatibility enabled - gsql -c 'CREATE DATABASE test DBCOMPATIBILITY 'PG' ;' + # Create a new database named "test" with Default compatibility with Oracle enabled + gsql -c 'CREATE DATABASE test;' # Set the Python import path to include your local GaussDB Python project # Replace your_path with actual values @@ -154,8 +157,8 @@ Recommended Steps to Run OpenGauss with Python GaussDB Driver Testing (Assuming # Connect to the OpenGauss database using the gsql client gsql -d postgres -p 5432 -U omm - -- Create a new database named "test" with PostgreSQL compatibility enabled - CREATE DATABASE test DBCOMPATIBILITY 'PG'; + -- Create a new database named "test" with Default compatibility with Oracle enabled + CREATE DATABASE test; # Set the Python import path to include your local GaussDB Python project diff --git a/example/demo.py b/example/demo.py new file mode 100644 index 000000000..b34124f89 --- /dev/null +++ b/example/demo.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- + +import os +import sys + +from gaussdb import connect + +os.environ["GAUSSDB_IMPL"] = "python" + + +def main(): + dsn = os.environ.get("GAUSSDB_TEST_DSN") + if not dsn: + print("❌ Please set the GAUSSDB_TEST_DSN environment variable, for example:") + print( + ' export GAUSSDB_TEST_DSN="dbname=test01 user=root password=***' + 'host=** port=8000"' + ) + sys.exit(1) + + drop_table_sql = "DROP TABLE IF EXISTS test" + create_table_sql = ( + "CREATE TABLE test (id serial PRIMARY KEY, num integer, data text)" + ) + insert_data_sql = "INSERT INTO test (num, data) VALUES (%s, %s)" + update_data_sql = "UPDATE test SET data = 'gaussdb' WHERE num = 2" + select_sql = "SELECT * FROM test" + + with connect(dsn, connect_timeout=10, application_name="gaussdb-demo") as conn: + + with conn.cursor() as cur: + server_version = conn.execute("select version()").fetchall()[0][0] + print(f"✅ Connected. Server version: {server_version}") + print(f"Vendor: {conn.info.vendor}, Version: {conn.info.server_version}") + + cur.execute(drop_table_sql) + cur.execute(create_table_sql) + cur.execute(insert_data_sql, (1, "hello")) + cur.execute(insert_data_sql, (2, "world")) + + cur.execute(select_sql) + print("📄origin: ", cur.fetchall()) + + cur.execute(update_data_sql) + cur.execute(select_sql) + print("📄update: ", cur.fetchall()) + + +if __name__ == "__main__": + main()