Build a Parameterized Insert Query
Easy
Write a function build_insert(table, columns, values) that returns a tuple (sql, params) representing a parameterized MySQL INSERT statement using %s placeholders, where sql is the SQL string and params is a tuple of values, ready to pass to cursor.execute().
Input
A table name string, a list of column name strings, and a tuple/list of values.
Output
A tuple (sql_string, values_tuple).
Example
Input
table="users", columns=["name", "email"], values=("Alice", "alice@example.com")
Output
("INSERT INTO users (name, email) VALUES (%s, %s)", ('Alice', 'alice@example.com'))
Join the column names with ", " for the column list, and create the same number of "%s" placeholders joined with ", " for the VALUES clause.
10 XP on solve
Back to lesson
Your Solution
Output
Click "Run" to execute your code...
⏳ Loading Python runtime (first run may take a few seconds)...
Log in to submit your solution and earn XP.