#sql #mysql #sql-query

e9571_mysql_lib

A MySQL database utility library for executing queries and assembling SQL statements with SQL injection prevention

2 releases

Uses new Rust 2024

0.1.1 Aug 14, 2025
0.1.0 Aug 9, 2025

#1667 in Database interfaces

MIT license

19KB
194 lines

e9571_mysql_lib Usage Examples

This document demonstrates the usage of the e9571_mysql_lib module in a Rust program, designed for casino-related database operations such as inserting, updating, querying, and deleting records.

Source Code Example

Below is a Rust program showcasing various database operations using the e9571_mysql_lib module. The code connects to a MySQL database, performs CRUD (Create, Read, Update, Delete) operations, and handles errors using Rust's Result type.

use e9571_mysql_lib::e9571_mysql_lib::*;
use std::collections::HashMap;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Database configuration
    let database_config = "mysql://root:@127.0.0.1:3306/test";

    // 1. Insert a new record
    let mut insert_data = HashMap::new();
    insert_data.insert("number".to_string(), "42".to_string());
    insert_data.insert("aggregate_value".to_string(), "100".to_string());
    insert_data.insert("list".to_string(), "5".to_string());
    let insert_sql = assemble_insert(insert_data, "test1");
    println!("Insert SQL: {}", insert_sql);
    let insert_result = mysql_connect_query_insert_api(&insert_sql, database_config)?;
    println!("Inserted record ID: {}", insert_result);

    // Verify insertion
    let verify_sql = format!("SELECT COUNT(*) FROM test1 WHERE id = {}", insert_result);
    let count: String = mysql_query_data(&verify_sql, database_config)?;
    println!("Inserted record count: {}", count);

    // 2. Update a single field
    update_field(&insert_result.to_string(), "number", "99", "test1", database_config)?;
    println!("Updated field for ID: {}", insert_result);

    // 3. Query all records
    let query_sql = sql_create("test1", "WHERE 1=1");
    println!("Query SQL: {}", query_sql);
    let query_result = mysql_connect_query_config_api(&query_sql, database_config)?;
    println!("Queried {} rows", query_result.len());
    for (i, row) in query_result.iter().enumerate() {
        println!("Row {}: {:?}", i + 1, row);
    }

    // 4. Query a single value
    let single_query_sql = format!("SELECT number FROM test1 WHERE id = {}", insert_result);
    let single_result = mysql_query_data(&single_query_sql, database_config)?;
    println!("Queried number for ID {}: {}", insert_result, single_result);

    // 5. Update multiple fields
    let mut update_data = HashMap::new();
    update_data.insert("aggregate_value".to_string(), "200".to_string());
    update_data.insert("list".to_string(), "10".to_string());
    let mut where_clause = HashMap::new();
    where_clause.insert("id".to_string(), insert_result.to_string());
    let update_sql = assemble_update(update_data, where_clause, "test1");
    println!("Update SQL: {}", update_sql);
    let affected_rows = mysql_connect_query_update_api(&update_sql, database_config)?;
    println!("Update affected rows: {}", affected_rows);

    // 6. Delete the record
    let mut delete_where = HashMap::new();
    delete_where.insert("id".to_string(), insert_result.to_string());
    let delete_sql = assemble_delete(delete_where, "test1");
    println!("Delete SQL: {}", delete_sql);
    let affected_rows = mysql_connect_query_update_api(&delete_sql, database_config)?;
    println!("Delete affected rows: {}", affected_rows);

    // Verify deletion
    let verify_delete_sql = format!("SELECT COUNT(*) FROM test1 WHERE id = {}", insert_result);
    let delete_count: String = mysql_query_data(&verify_delete_sql, database_config)?;
    println!("Remaining record count after deletion: {}", delete_count);

    Ok(())
}

Explanation of Functions

The e9571_mysql_lib module provides utility functions for interacting with a MySQL database, suitable for casino applications such as managing betting records, user funds, or game logs.

  1. assemble_insert:

    • Generates an SQL INSERT statement from a HashMap of column-value pairs for a specified table.
    • Use Case: Adding new betting records or user data.
  2. mysql_connect_query_insert_api:

    • Executes an INSERT query and returns the inserted record's ID.
    • Use Case: Recording a new bet or transaction.
  3. mysql_query_data:

    • Executes a SELECT query and returns a single value (e.g., count or field value).
    • Use Case: Verifying record existence or retrieving specific data.
  4. update_field:

    • Updates a single field in a table for a specified record ID.
    • Use Case: Modifying a bet amount or status.
  5. sql_create:

    • Generates a SELECT query with a customizable WHERE clause.
    • Use Case: Retrieving all records for analysis or display.
  6. mysql_connect_query_config_api:

    • Executes a SELECT query and returns a vector of rows (as HashMaps).
    • Use Case: Fetching all betting records for a table.
  7. assemble_update:

    • Generates an SQL UPDATE statement from HashMaps of update values and WHERE conditions.
    • Use Case: Updating multiple fields in a betting record.
  8. assemble_delete:

    • Generates an SQL DELETE statement based on a WHERE clause from a HashMap.
    • Use Case: Removing a record after a bet is settled.
  9. mysql_connect_query_update_api:

    • Executes an UPDATE or DELETE query and returns the number of affected rows.
    • Use Case: Confirming the success of updates or deletions.

Casino Scenario Usage

These functions are ideal for casino applications, such as:

  • Inserting Records: Storing new bets or user transactions (assemble_insert, mysql_connect_query_insert_api).
  • Updating Records: Modifying bet amounts or statuses (update_field, assemble_update).
  • Querying Data: Retrieving betting history or user balances (sql_create, mysql_connect_query_config_api).
  • Deleting Records: Removing settled bets or obsolete data (assemble_delete).
  • Verification: Checking record existence or data integrity (mysql_query_data).

Example Output

The output depends on the database state and e9571_mysql_lib implementation. An example output might look like:

=== Insert Record ===
Insert SQL: INSERT INTO test1 (number, aggregate_value, list) VALUES ('42', '100', '5')
Inserted record ID: 1
Inserted record count: 1

=== Update Single Field ===
Updated field for ID: 1

=== Query All Records ===
Query SQL: SELECT * FROM test1 WHERE 1=1
Queried 1 rows
Row 1: {"id": "1", "number": "99", "aggregate_value": "100", "list": "5"}

=== Query Single Value ===
Queried number for ID 1: 99

=== Update Multiple Fields ===
Update SQL: UPDATE test1 SET aggregate_value = '200', list = '10' WHERE id = '1'
Update affected rows: 1

=== Delete Record ===
Delete SQL: DELETE FROM test1 WHERE id = '1'
Delete affected rows: 1
Remaining record count after deletion: 0

Notes

  • Database Setup: Ensure the MySQL database (test) and table (test1) exist with columns id, number, aggregate_value, and list.
  • Error Handling: The code uses Result for robust error handling, suitable for production environments.
  • Security: Avoid hardcoding credentials (mysql://root:@...). Use environment variables or a configuration file in production.
  • Casino Context: The table test1 could represent betting records, with number as bet amount, aggregate_value as total stake, and list as a game type ID.
  • GitHub Rendering: This Markdown uses clear headings, Rust syntax highlighting, and structured explanations for optimal display.

Dependencies

~12–27MB
~372K SLoC