Skip to content

Commit 92b2cfd

Browse files
committed
Minor updates
1 parent 68102f2 commit 92b2cfd

File tree

4 files changed

+21
-143
lines changed

4 files changed

+21
-143
lines changed

07_Create_First_Cosmos_DB_Project/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ client = CosmosClient.from_connection_string(CONNECTION_STRING)
5151

5252
### Creating a database
5353

54-
The `create_database` method is used to create a database. If the database already exists, an exception is thrown, therefore verify the database already exists before creating it.
54+
The `create_database_if_not_exists` method is used to create a database. If the database already exists, the method will retrieve the existing database.
5555

5656
```python
5757
db: DatabaseProxy = client.create_database(database_name)

Labs/lab_1_first_application.ipynb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"\n",
3535
">**Note**: If you are running using the **local emulator**, append the following value to the connection string: `&retrywrites=false&tlsallowinvalidcertificates=true`.\n",
3636
"\n",
37-
"To create a NoSQL database in Cosmos DB, first instantiate a `CosmosClient` object. Before attempting to create the database, check for its existence using the `list_databases` method, as trying to create a database that already exists will raise an exception. If the database does not exist, create it using the `create_database` method, passing the database name as an argument. If the database already exists, you can load it by calling the `get_database_client` method with the database name as an argument."
37+
"To create a NoSQL database in Cosmos DB, first instantiate a `CosmosClient` object, use the `create_database_if_not_exists` method to create a database if it does not exist to avoid any exceptions should the database already exist. This method will create a database with the specified name if it does not exist, otherwise it will return the existing database."
3838
]
3939
},
4040
{
@@ -51,19 +51,7 @@
5151
"\n",
5252
"# Create or load the cosmic_works database\n",
5353
"database_name = \"cosmic_works\"\n",
54-
"db = None\n",
55-
"databases = list(client.list_databases())\n",
56-
"# Check if the database already exists\n",
57-
"for db_info in databases:\n",
58-
" if db_info['id'] == database_name:\n",
59-
" db = client.get_database_client(database_name)\n",
60-
" print(f\"Database '{database_name}' already exists and has been retrieved.\")\n",
61-
" break\n",
62-
"\n",
63-
"# Create the database if it does not exist\n",
64-
"if not db:\n",
65-
" db: DatabaseProxy = client.create_database(database_name)\n",
66-
" print(f\"Database '{database_name}' created successfully.\")"
54+
"db = client.create_database_if_not_exists(id=database_name)"
6755
]
6856
},
6957
{

Labs/lab_2_load_data.ipynb

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,9 @@
3232
},
3333
{
3434
"cell_type": "code",
35-
"execution_count": 2,
35+
"execution_count": null,
3636
"metadata": {},
37-
"outputs": [
38-
{
39-
"name": "stdout",
40-
"output_type": "stream",
41-
"text": [
42-
"Database 'cosmic_works' already exists and has been retrieved.\n"
43-
]
44-
}
45-
],
37+
"outputs": [],
4638
"source": [
4739
"load_dotenv()\n",
4840
"CONNECTION_STRING = os.environ.get(\"COSMOS_DB_CONNECTION_STRING\")\n",
@@ -52,19 +44,7 @@
5244
"\n",
5345
"# Create or load the cosmic_works database\n",
5446
"database_name = \"cosmic_works\"\n",
55-
"db = None\n",
56-
"databases = list(client.list_databases())\n",
57-
"# Check if the database already exists\n",
58-
"for db_info in databases:\n",
59-
" if db_info['id'] == database_name:\n",
60-
" db = client.get_database_client(database_name)\n",
61-
" print(f\"Database '{database_name}' already exists and has been retrieved.\")\n",
62-
" break\n",
63-
"\n",
64-
"# Create the database if it does not exist\n",
65-
"if not db:\n",
66-
" db: DatabaseProxy = client.create_database(database_name)\n",
67-
" print(f\"Database '{database_name}' created successfully.\")"
47+
"db = client.create_database_if_not_exists(id=database_name)"
6848
]
6949
},
7050
{

0 commit comments

Comments
 (0)