pub fn delete<T: SqlCommand + SqlParams>(
conn: &Connection,
entity: T,
) -> Result<usize, Error>
Expand description
§delete
Deletes a record from the database.
§Parameters
conn
: SQLite database connectionentity
: Data object containing deletion parameters (must implement SqlCommand and SqlParams traits)
§Return Value
Result<usize, Error>
: On success, returns the number of deleted records; on failure, returns Error
§Struct Definition
Structs used with this function should be annotated with the following derive macros:
#[derive(Deletable, SqlParams)] // Required macros
#[table("table_name")] // Table name to delete from
#[where_clause("id = ?")] // Delete condition
pub struct MyEntity {
pub id: i64, // Field used in the condition
}
Deletable
: Automatically generates SQL DELETE statementsSqlParams
: Automatically generates SQL parameters#[table("table_name")]
: Specifies the table name for the deletion#[where_clause("id = ?")]
: Specifies the delete condition (?
will be replaced with parameter value)
§Example Usage
use rusqlite::{Connection, Result};
use parsql_macros::{Deletable, SqlParams};
use parsql_sqlite::delete;
fn main() -> Result<()> {
// Create database connection
let conn = Connection::open("test.db")?;
conn.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)", [])?;
conn.execute("INSERT INTO users (id, name, email) VALUES (1, 'John', 'john@example.com')", [])?;
// Define a delete query
#[derive(Deletable, SqlParams)]
#[table("users")]
#[where_clause("id = ?")]
pub struct DeleteUser {
pub id: i64,
}
// Create delete parameters (delete user with ID 1)
let delete_query = DeleteUser { id: 1 };
// Execute delete
let delete_result = delete(&conn, delete_query)?;
println!("Delete result: {:?}", delete_result);
Ok(())
}