diff --git a/src/database.ts b/src/database.ts index df4740a..228abed 100644 --- a/src/database.ts +++ b/src/database.ts @@ -40,7 +40,7 @@ import { sqlite3_stmt, sqlite3_total_changes, } from "./ffi.ts"; -import { cstr } from "./util.ts"; +import { cstr, encode } from "./util.ts"; import { fromFileUrl } from "../deps.ts"; /** SQLite version string */ @@ -429,7 +429,9 @@ export class PreparedStatement { break; case "string": { - const buffer = this.#cstr(value); + // Bind parameters do not need C string, + // because we specify it's length. + const buffer = encode(value); this.#bufferRefs.add(buffer); sqlite3_bind_text( this.db.unsafeRawHandle, diff --git a/test/test.ts b/test/test.ts index e5b6673..198837c 100644 --- a/test/test.ts +++ b/test/test.ts @@ -117,6 +117,18 @@ Deno.test("sqlite", async (t) => { } }); + await t.step("query with string param", () => { + const row = db.queryArray<[number, string, number, Uint8Array, null]>( + "select * from test where text = ?", + "hello world", + )[0]; + assertEquals(row[0], 0); + assertEquals(row[1], "hello world"); + assertEquals(row[2], 3.14); + assertEquals(row[3], new Uint8Array([1, 2, 3])); + assertEquals(row[4], null); + }); + await t.step("drop table", () => { db.execute("drop table test"); });