WiseLibs / better-sqlite3

The fastest and simplest library for SQLite3 in Node.js.
MIT License
5.23k stars 390 forks source link

How to insert INTEGER? String vs Number #1144

Closed forgotPassword closed 4 months ago

forgotPassword commented 4 months ago

Hello,

I have a DB column set as INTEGER, and my Javascript data is (validated) integers as strings. Should I parseInt(str) beforehand? Does it matter to better-sqlite3? What is the proper/performant way? Thanks!

forgotPassword commented 4 months ago

https://github.com/WiseLibs/better-sqlite3/blob/a36b8e47c3ba8b57067dc4d49b785ae398481c79/src/better_sqlite3.cpp#L1943

Well, it seems BindValueFromJS checks if the value is number/string and calls sqlite3_bind_double/sqlite3_bind_text. I guess if want to avoid ambiguity can call parseInt first.

alternatively you can trust sqlite to do the correct thing, and check with select typeof(column).. to see how the value was saved.