-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
execute only executes first statement #44
Comments
Yes, the sqlite-simple Sqlite-simple doesn't look at the SQL query string at all (that'd require some level of parsing) but punts it directly to the native sqlite3 library. Thus it has no way of knowing how many statements are in the query string. I definitely agree the least that should be done would be to state this limitation clearly in the API docs. |
Specifically it's worth pointing out the behavior differs from the |
Is there a workaround to this, if I have, for example, a .sql file with several queries and I want to run it from Haskell? Is the workaround to import .Internal, get a connection handle, and drop to direct-sqlite? |
That'd be a reasonable work-around. I think it'd be fine to also change As I mentioned in #44 (comment), sqlite-simple does not look into or modify the input query string at all, but passes it directly to the native sqlite library and thus is limited by what sqlite prepared statements support. AFAIK, postgresql-simple does parameter substitution differently and can support multiple statements. |
@nurpax I don't see function that executes multiple SQL statements. Are you still waiting for PR for this feature? As I can see, sqlite-simple/Database/SQLite/Simple.hs Lines 380 to 384 in ada4594
Am I right that it should use So the implementation would look like this: -- | A version of 'execute' that does not perform query substitution.
execute_ :: Connection -> Query -> IO ()
execute_ conn template =
withStatement conn template $ \(Statement stmt) ->
void $ Base.exec (connectionHandle conn) (??? don't know what to pass here ???) |
For those looking for an example on how to connect to the database and possibly populate it with a schema query in one go: import qualified System.Posix.Files as File
import qualified Database.SQLite3 as Sqlite3
import qualified Database.SQLite.Simple as Sqlite
import Control.Monad (unless)
connect :: Sqlite.Query -> FilePath -> IO Sqlite.Connection
connect schema path = do
exists <- File.fileExist path
connection @ Sqlite.Connection {..} <- Sqlite.open path
unless exists $ Sqlite3.exec connectionHandle (Sqlite.fromQuery schema)
return connection |
The documentation for
execute
says "Execute an INSERT, UPDATE, or other SQL query that is not expected to return results.". However, ultimatelyexecute
callsDatabase.SQLite3.prepare
whose documentation says "Unlike exec, prepare only executes the first statement, and ignores subsequent statements." (i.e. ignores subsequent statements separated by a semicolon).Either
execute
should be rewritten somehow to support executing multiple statements (my preference)The text was updated successfully, but these errors were encountered: