better-sqlite3 icon indicating copy to clipboard operation
better-sqlite3 copied to clipboard

get result from db.exec

Open mccoysc opened this issue 3 years ago • 3 comments

some time,we need run multiline sql and get the execute result,but now the api "exec" return no execute result

mccoysc avatar Jul 17 '22 00:07 mccoysc

The goal of db.exec() is to be an easy-to-use function for executing large SQL scripts, typically stored in files (for example, database migrations). In such cases, the query results are generally not needed.

There are many problems with adding query results to db.exec(). For one, since there can be multiple statements, it's not easy to know which results are actually desired. Second, there are different types of results a user might be interested in (for example, prepared statements have four different methods, each for getting different types of results: run, get, all, and iterate), plus various methods for deciding the output format (pluck, expand, raw).

Because of these complications, the API for db.exec() would need to be quite complicated to support all use-cases. For this reason, it's purposefully dumb, and users are encouraged to use prepared statements for most tasks.

JoshuaWise avatar Jul 18 '22 04:07 JoshuaWise

maybe it can return the last execute result as the final result. some time,the sql is from other people and i can't get only 'one' sql.

mccoysc avatar Jul 18 '22 04:07 mccoysc

Perhaps there could be a function like db.parse(), which could take a multi-statement SQL string and return an array of prepared statement objects. That way, the user would still have complete control over how each statement gets executed, and which results they are interested in.

JoshuaWise avatar Jul 18 '22 04:07 JoshuaWise