Hi @rocketmaniac
The provided syntax appears to be a mix of C# code that interacts with a database using a `PrestoDataAdapter`, `PrestoCommand`, and `PrestoParameter` for executing SQL queries against a Presto database.
However, a few points need clarification or adjustment:
1. Deletion Command Parameters: Ensure that the parameter you're using in the deletion query matches the parameter you're adding to the `PrestoCommand`.
2. Delete Rows: The code snippet you provided seems to be trying to delete rows based on a specific condition (`_id = '657636888a673e46b8a4dce0'`). It's important to ensure that the condition is correctly specified and matches your data.
3. AcceptChanges() Method: Depending on your use case, calling `AcceptChanges()` might not be necessary after removing rows. This method commits the changes made to the `DataTable`, marking all the remaining rows as unchanged.
Here's an adjusted version of your code:
PrestoDataAdapter dataAdapter = new PrestoDataAdapter("SELECT _id FROM hive.transactional_schema..TableName]", connection);
dataAdapter.DeleteCommand = new PrestoCommand("DELETE FROM hive.transactional_schema.cached_reports WHERE _id = @_id", connection);
// Ensure the parameter matches the one used in the query
dataAdapter.DeleteCommand.Parameters.Add(new PrestoParameter("@_id", DbType.String, "_id"));
DataTable table = new DataTable();
dataAdapter.Fill(table);
DataRoww] rowsToRemove = table.Select("_id = '657636888a673e46b8a4dce0'");
foreach (var rowToDelete in rowsToRemove)
{
// You can either remove the row from the table or mark it as deleted
rowToDelete.Delete();
// If you want to remove the row from the DataTable, uncomment the line below:
// table.Rows.Remove(rowToDelete);
}
// Call Update to reflect changes back to the database
dataAdapter.Update(table);
Make sure to replace `hive.transactional_schema.aTableName]` and `hive.transactional_schema.cached_reports` with the actual table names in your Presto database. Also, verify that the column name `_id` matches the one used in your database schema.
Remember, it's crucial to handle exceptions, connection handling, and data validation/error checking appropriately in a production environment. Additionally, testing thoroughly with your specific database setup and schema is recommended.
Please give it a try and let me know if it works.
Hi @rocketmaniac,
You can use the DELETE Statement listed in our documentation to achieve your goal → https://cdn.cdata.com/help/ORJ/ado/pg_delete.htm
Your code would look something like this:
using (PrestoConnection connection = new PrestoConnection(connectionString))
{
PrestoCommand cmd = new PrestoCommand("DELETE FROM RTableName] WHERE _id = @_id", connection);
cmd.Parameters.Add(new PrestoParameter("_id", "657636888a673e46b8a4dce0"));
int rowsAffected = cmd.ExecuteNonQuery();
Console.Write("Rows affected: " + rowsAffected);
}
Or if you want to keep the same format as the abovementioned, I was able to modify the code and got it to work using the following:
using (PrestoConnection connection = new PrestoConnection(connectionString))
{
PrestoDataAdapter dataAdapter = new PrestoDataAdapter(
"SELECT E_id] FROM RTableName]", connection);
dataAdapter.DeleteCommand = new PrestoCommand(
"DELETE FROM RTableName] WHERE _id = @_id", connection);
// Add the parameters for the DeleteCommand.
dataAdapter.DeleteCommand.Parameters.Add(new PrestoParameter(
"@_id", "657636888a673e46b8a4dce0", DbType.Int32));
//dataAdapter.DeleteCommand.Parameterst0].SourceVersion = DataRowVersion.Original;
dataAdapter.DeleteCommand.BeginExecuteNonQuery();
}
In the second section, I have placed the value of the Id parameter directly and then began the executing process, using the format → new PrestoParameter(VariableName, Value, DataType)
Please give this method a try and let me know if the issue is fixed.
Thanks @Ankit Singh the key change that made it work for me was excluding AcceptChanges(). After excluding that, my original code worked, and the DELETE propagated to Trino and the row was deleted there. Apparently, once you AcceptChanges() then the dataAdapter has no record of the deleted row in “table”. I guess it makes sense, but it’s just not documented well anywhere that I found that if you call AcceptChanges, the delete will not propagate on dataAdapter.Update(table).
Thanks @Luçjano Capo! As mentioned in my comment to @Ankit Singh my original code worked once I removed the call to AcceptChanges(), so I didn’t actually test out your suggestions, but I’m sure they work as suggested.