Skip to content
Discussion options

You must be logged in to vote

@chenmonster the ? in the error is the giveaway. when you write await mysql\${ddl}``, bun's tagged template sends the entire DDL string as a bind parameter via mysql's prepared statement protocol. mysql can't parameterize DDL statements (table names, column definitions, etc.), only DML values.

fix: use sql.unsafe() for DDL, keep parameterized queries for DML:

const mysql = new SQL("mysql://xxx:xxx@mysql.sqlpub.com:3306/test");

// DDL -- use .unsafe() since table/column names can't be parameterized
const ddl = Object.keys(datas[0])
  .reduce((sql, key) => sql.concat(`  \`${key}\` VARCHAR(255),\n`), "CREATE TABLE IF NOT EXISTS `ais_data` (\n")
  .slice(0, -2)
  .concat("\n)");

await mysql.u…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@chenmonster
Comment options

Answer selected by chenmonster
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants