To update data in an Excel file using Python, you can use the pandas library. Here’s a sample code to update data in an Excel file import pandas as pd # Read the Excel file excel_file = 'path/to/your/excel/file.xlsx' df = pd.read_excel(excel_file) # Update the data # Replace 'column_name' with the actual column name and 'condition_value' with the desired condition df.loc[df['column_name'] == 'condition_value', 'column_name'] = 'new_value' # Save the updated data to a new Excel file output_file = 'path/to/your/output/file.xlsx' df.to_excel(output_file, index=False) print("Data updated successfully!") In the code above, replace ‘path/to/your/excel/file.xlsx’ with the actual path to your Excel file. You also need to replace ‘column_name’ with the actual column name you want to update and ‘condition_value’ with the desired condition to identify the rows to be updated. Finally, replace ‘path/to/your/output/file.xlsx’ with the desired path to the output file. The code uses the pandas library to read the Excel file into a DataFrame using the pd.read_excel() function. It then updates the data in the DataFrame using the loc function, which allows you to select rows based on a condition and update specific columns. Finally, the updated DataFrame is saved to a new Excel file using the to_excel() function. Make sure to have the pandas library installed. You can install it using pip: pip install pandas Remember to handle any exceptions that may occur during file reading, updating, or writing for proper error handling.
Delete data in Azure SQL DB using Python
To delete data from an Azure SQL Database using Python, you can utilize the pyodbc package. Here’s a sample code to delete data in Azure SQL Database: import pyodbc # Connect to the database server = 'your_server_name.database.windows.net' database = 'your_database_name' username = 'your_username' password = 'your_password' driver = '{ODBC Driver 17 for SQL Server}' conn_str = f"DRIVER={driver};SERVER={server};DATABASE={database};UID={username};PWD={password}" connection = pyodbc.connect(conn_str) # Create a cursor cursor = connection.cursor() # Delete query delete_query = """ DELETE FROM your_table WHERE condition_column = ? """ # Delete data condition_value = "value_to_delete" cursor.execute(delete_query, condition_value) # Commit the changes connection.commit() # Close the cursor and the connection cursor.close() connection.close() print("Data deleted successfully!") In the code above, replace ‘your_server_name’, ‘your_database_name’, ‘your_username’, ‘your_password’, ‘your_table’, and ‘condition_column’ with the actual values corresponding to your Azure SQL Database, table, and condition. The code establishes a connection to the Azure SQL Database using the pyodbc.connect() method. You need to provide the server name, database name, username, password, and the appropriate driver for your SQL Server version. A delete query is defined with a placeholder (?) for the condition value. The condition value is provided as an argument to the cursor.execute() method. After deleting the data, the changes are committed using connection.commit(). Finally, the cursor and the connection are closed, and a success message is printed to the console. Make sure to have the pyodbc package installed. You can install it using pip: pip install pyodbc Remember to handle any exceptions that may occur during the database connection or delete operation for proper error handling.
Update data in Azure SQL DB using Python
To update data in an Azure SQL Database using Python, you can use the pyodbc package. Here’s a sample code to update data in Azure SQL Database import pyodbc # Connect to the database server = 'your_server_name.database.windows.net' database = 'your_database_name' username = 'your_username' password = 'your_password' driver = '{ODBC Driver 17 for SQL Server}' conn_str = f"DRIVER={driver};SERVER={server};DATABASE={database};UID={username};PWD={password}" connection = pyodbc.connect(conn_str) # Create a cursor cursor = connection.cursor() # Update query update_query = """ UPDATE your_table SET column1 = ?, column2 = ? WHERE condition_column = ? """ # Update data data = ("new_value1", "new_value2", "condition_value") cursor.execute(update_query, data) # Commit the changes connection.commit() # Close the cursor and the connection cursor.close() connection.close() print("Data updated successfully!") In the code above, replace ‘your_server_name’, ‘your_database_name’, ‘your_username’, ‘your_password’, ‘your_table’, ‘column1’, ‘column2’, and ‘condition_column’ with the actual values corresponding to your Azure SQL Database, table, columns, and condition. The code establishes a connection to the Azure SQL Database using the pyodbc.connect() method. You need to provide the server name, database name, username, password, and the appropriate driver for your SQL Server version. An update query is defined with placeholders (?) for the values to be updated. The data to be updated is provided as a tuple (data) where each element corresponds to a placeholder in the update query. The cursor.execute() method is used to execute the update query with the provided data. After updating the data, the changes are committed using connection.commit(). Finally, the cursor and the connection are closed, and a success message is printed to the console. Make sure to have the pyodbc package installed. You can install it using pip: pip install pyodbc Remember to handle any exceptions that may occur during the database connection or update operation for proper error handling.
Delete data in SQL DB using Python
To delete data from a SQL database using Python, you need to establish a connection to the database and execute a delete query. Here’s a sample code to delete data in a SQL database using the pyodbc package import pyodbc # Connect to the database connection = pyodbc.connect( "Driver={your_driver};" "Server=your_server;" "Database=your_database;" "UID=your_username;" "PWD=your_password;" ) # Create a cursor cursor = connection.cursor() # Delete query delete_query = """ DELETE FROM your_table WHERE condition_column = ? """ # Delete data condition_value = "value_to_delete" cursor.execute(delete_query, condition_value) # Commit the changes connection.commit() # Close the cursor and the connection cursor.close() connection.close() print("Data deleted successfully!") In the code above, replace ‘your_driver’, ‘your_server’, ‘your_database’, ‘your_username’, ‘your_password’, ‘your_table’, and ‘condition_column’ with the actual values corresponding to your SQL database, table, and condition. The code establishes a connection to the SQL database using the pyodbc.connect() method. A delete query is defined with a placeholder (?) for the condition value. The condition value is provided as an argument to the cursor.execute() method. After deleting the data, the changes are committed using connection.commit(). Finally, the cursor and the connection are closed, and a success message is printed to the console. Make sure to have the pyodbc package installed. You can install it using pip: pip install pyodbc Remember to handle any exceptions that may occur during the database connection or delete operation for proper error handling.
Update data in SQL DB using Python
To update data in a SQL database using Python, you need to establish a connection to the database and execute an update query. Here’s a sample code to update data in a SQL database using the pyodbc package: import pyodbc # Connect to the database connection = pyodbc.connect( "Driver={your_driver};" "Server=your_server;" "Database=your_database;" "UID=your_username;" "PWD=your_password;" ) # Create a cursor cursor = connection.cursor() # Update query update_query = """ UPDATE your_table SET column1 = ?, column2 = ? WHERE condition_column = ? """ # Update data data = ("new_value1", "new_value2", "condition_value") cursor.execute(update_query, data) # Commit the changes connection.commit() # Close the cursor and the connection cursor.close() connection.close() print("Data updated successfully!") In the code above, replace ‘your_driver’, ‘your_server’, ‘your_database’, ‘your_username’, ‘your_password’, ‘your_table’, ‘column1’, ‘column2’, and ‘condition_column’ with the actual values corresponding to your SQL database, table, columns, and condition. The code establishes a connection to the SQL database using the pyodbc.connect() method. An update query is defined with placeholders (?) for the values to be updated. The data to be updated is provided as a tuple (data) where each element corresponds to a placeholder in the update query. The cursor.execute() method is used to execute the update query with the provided data. After updating the data, the changes are committed using connection.commit(). Finally, the cursor and the connection are closed, and a success message is printed to the console. Make sure to have the pyodbc package installed. You can install it using pip: pip install pyodbc Remember to handle any exceptions that may occur during the database connection or update operation for proper error handling.
Write data in MongoDB using .NET
To write data to a MongoDB database using .NET, you can use the MongoDB .NET Driver. Here’s an example code using System; using MongoDB.Bson; using MongoDB.Driver; class Program { static void Main() { // Connection string for the MongoDB database string connString = "mongodb://localhost:27017"; // Create a new MongoClient MongoClient client = new MongoClient(connString); // Access the database IMongoDatabase database = client.GetDatabase("your_database"); // Access the collection IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("your_collection"); // Create a new document var document = new BsonDocument { { "name", "John Doe" }, { "age", 30 }, { "email", "[email protected]" } }; // Insert the document into the collection collection.InsertOne(document); Console.WriteLine("Document inserted successfully!"); } } In this example, replace ‘mongodb://localhost:27017’ with the actual connection string for your MongoDB database. The code creates a new MongoClient instance using the connection string. It then accesses the desired database using GetDatabase and the collection within that database using GetCollection. A new BsonDocument is created to represent the data you want to insert. It contains fields such as “name”, “age”, and “email” with their respective values. The InsertOne method is used to insert the document into the collection. Finally, a success message is displayed in the console. Make sure to have the MongoDB .NET Driver installed in your .NET project. You can install it via NuGet Package Manager or through the .NET CLI: dotnet add package MongoDB.Driver Remember to handle any exceptions that may occur during the connection or document insertion for proper error handling.
Read data in MongoDB using .NET
To read data from a MongoDB database using .NET, you can use the MongoDB .NET Driver. Here’s an example code: using System; using MongoDB.Driver; class Program { static void Main() { // Connection string for the MongoDB database string connString = "mongodb://localhost:27017"; // Create a new MongoClient MongoClient client = new MongoClient(connString); // Access the database IMongoDatabase database = client.GetDatabase("your_database"); // Access the collection IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("your_collection"); // Create a filter to retrieve documents var filter = Builders<BsonDocument>.Filter.Empty; // Retrieve documents from the collection var documents = collection.Find(filter).ToList(); // Iterate over the documents and print the values foreach (var document in documents) { string name = document["name"].AsString; int age = document["age"].AsInt32; string email = document["email"].AsString; Console.WriteLine($"Name: {name}, Age: {age}, Email: {email}"); } } } In this example, replace ‘mongodb://localhost:27017’ with the actual connection string for your MongoDB database. The code creates a new MongoClient instance using the connection string. It then accesses the desired database using GetDatabase and the collection within that database using GetCollection. A filter is created to retrieve all documents in the collection. The Find method is used to execute the query and retrieve the documents as a list. The code then iterates over the retrieved documents and accesses the desired fields by their names using the AsString or AsInt32 methods. Make sure to have the MongoDB .NET Driver installed in your .NET project. You can install it via NuGet Package Manager or through the .NET CLI: dotnet add package MongoDB.Driver Remember to handle any exceptions that may occur during the connection or query execution for proper error handling.
Write data to PostgreSQL using .NET
To write data to a PostgreSQL database using .NET, you can use the Npgsql library, which is a .NET data provider for PostgreSQL. Here’s an example code using System; using Npgsql; class Program { static void Main() { // Connection string for the PostgreSQL database string connString = "Host=your_host;Username=your_user;Password=your_password;Database=your_database"; // Create a new PostgreSQL connection using (NpgsqlConnection conn = new NpgsqlConnection(connString)) { conn.Open(); // SQL query to insert data string query = "INSERT INTO your_table (name, age, email) VALUES (@name, @age, @email)"; // Create a command object with the query and connection using (NpgsqlCommand cmd = new NpgsqlCommand(query, conn)) { // Set parameter values cmd.Parameters.AddWithValue("@name", "John Doe"); cmd.Parameters.AddWithValue("@age", 30); cmd.Parameters.AddWithValue("@email", "[email protected]"); // Execute the query int rowsAffected = cmd.ExecuteNonQuery(); Console.WriteLine($"Rows affected: {rowsAffected}"); } } } } In this example, replace ‘your_host’, ‘your_user’, ‘your_password’, and ‘your_database’ with the actual connection details for your PostgreSQL database. The code establishes a connection to the PostgreSQL database using the specified host, username, password, and database name. It then creates a command object with the SQL query and the connection. The example sets parameter values for the query using the AddWithValue() method. The @name, @age, and @email are placeholders. Finally, the query is executed using ExecuteNonQuery(), which returns the number of rows affected by the query. The example prints the number of rows affected to the console. Make sure to have the Npgsql library installed in your .NET project. You can install it via NuGet Package Manager or through the .NET CLI: dotnet add package Npgsql Remember to handle any exceptions that may occur during the database connection or query execution for proper error handling.
Read data from PostgreSQL using .NET
To read data from a PostgreSQL database using .NET, you can use the Npgsql library, which is a .NET data provider for PostgreSQL. Here’s an example code using System; using Npgsql; class Program { static void Main() { // Connection string for the PostgreSQL database string connString = "Host=your_host;Username=your_user;Password=your_password;Database=your_database"; // Create a new PostgreSQL connection using (NpgsqlConnection conn = new NpgsqlConnection(connString)) { conn.Open(); // SQL query to retrieve data string query = "SELECT * FROM your_table"; // Create a command object with the query and connection using (NpgsqlCommand cmd = new NpgsqlCommand(query, conn)) { // Execute the query and obtain a data reader using (NpgsqlDataReader reader = cmd.ExecuteReader()) { // Iterate over the rows and print the values while (reader.Read()) { // Access columns by their names or indexes string name = reader.GetString("name"); int age = reader.GetInt32("age"); string email = reader.GetString("email"); Console.WriteLine($"Name: {name}, Age: {age}, Email: {email}"); } } } } } } In this example, replace ‘your_host’, ‘your_user’, ‘your_password’, and ‘your_database’ with the actual connection details for your PostgreSQL database. The code establishes a connection to the PostgreSQL database using the specified host, username, password, and database name. It then creates a command object with the SQL query and the connection. The example executes the query using ExecuteReader() to obtain a data reader. It then iterates over the rows in the result set using Read(), and retrieves the values from each row using column names or indexes. Finally, the connection, command, and reader objects are disposed to release the resources. Make sure to have the Npgsql library installed in your .NET project. You can install it via NuGet Package Manager or through the .NET CLI: dotnet add package Npgsql Remember to handle any exceptions that may occur during the database connection or query execution for proper error handling.
Read data from MongoDB using Python
To read data from a MongoDB database using Python, you can use the pymongo library. Here’s an example code from pymongo import MongoClient # Connect to the MongoDB server client = MongoClient('mongodb://your_host:your_port/') # Access the database db = client['your_database'] # Access the collection collection = db['your_collection'] # Retrieve documents from the collection documents = collection.find() # Iterate over the documents and print each document for document in documents: print(document) # Close the connection client.close() In this example, replace ‘your_host’ and ‘your_port’ with the actual hostname and port number of your MongoDB server. The code connects to the MongoDB server using the specified host and port. It then accesses the database and collection using the respective names. The find() method retrieves all documents from the collection and returns a cursor. The code iterates over the cursor and prints each document. Finally, the connection is closed to release the resources. Make sure to have the pymongo library installed before running this code. You can install it using pip: pip install pymongo Remember to handle any exceptions that may occur during the database connection or query execution for proper error handling.