Prepared Statements don't seem to be working

I did post about this in #d1-database , but not sure if this is a better place for it - https://discord.com/channels/595317990191398933/992060581832032316/1227027355898220646
router.post('/:brand/:slug', async (r,e,c) => {
const brand = r.brand;
const slug = r.slug;
const db = e.LINK_DB
const body = await r.json();

const stmt = db.prepare(`INSERT INTO ? (slug, type, data, clicks) VALUES ('?', "ArtistLanding", '?', 0)`)
stmt.bind(brand,slug,body.data)
return await stmt.run();
})
router.post('/:brand/:slug', async (r,e,c) => {
const brand = r.brand;
const slug = r.slug;
const db = e.LINK_DB
const body = await r.json();

const stmt = db.prepare(`INSERT INTO ? (slug, type, data, clicks) VALUES ('?', "ArtistLanding", '?', 0)`)
stmt.bind(brand,slug,body.data)
return await stmt.run();
})
will always return
{
"status": 500,
"error": "D1_ERROR: near \"?\": syntax error at offset 12"
}
{
"status": 500,
"error": "D1_ERROR: near \"?\": syntax error at offset 12"
}
The first ? always seems to be the site of a syntax error. This is always during the bind phase (creating the statement and not binding does not cause an error)
2 Replies
kian
kian8mo ago
You can't use ? for table names
Parameters may not be used for column or table names, or as values for constraints or default values.
https://www.sqlite.org/cintro.html
PhilH
PhilHOP8mo ago
This is it! Thank you so much! - Knew I was going stupid somewhere
Want results from more Discord servers?
Add your server