-
Notifications
You must be signed in to change notification settings - Fork 45
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
Insert/Update a whole row using ToRow
instance
#96
Comments
At the moment, for {-# LANGUAGE ScopedTypeVariables #-}
import Control.Monad (void)
import Control.Monad.IO.Class (MonadIO (liftIO))
import Data.Maybe
import Data.Text as T (Text, intercalate)
import Database.SQLite.Simple
import Database.SQLite.Simple.FromField
import Database.SQLite.Simple.ToField
type TableName = Text
insertOne :: forall row m. (FromRow row, ToRow row, MonadIO m) => Connection -> TableName -> row -> m ()
insertOne conn' table row = do
statement <- liftIO $
openStatement conn' (
Query $
"INSERT INTO " <>
table <>
" values (" <>
(
T.intercalate "," (
-- Currently, the table columns have to be in the same order as the rows in the datatype.
replicate (length . toRow $ row) "?"
) <>
")"
)
)
liftIO $ bind statement row
void (liftIO $ nextRow statement :: m (Maybe row))
Ugh. There ought to be a way to grab the correct field names and do a proper SQL |
This might be fixable with the help of #95 as I'm now investigating getting field names which needs |
This should be easier to work with now that |
Is there a way to say
INSERT INTO table_name (something) values (something)
orUPDATE table_name SET a = "b", c = "d"
, etc., using theToRow
instance to automatically fill in these fields for me?Otherwise I would have to have a super long convoluted API call with all the fields being specific. Maybe I'm missing something in the instances, but this isn't in any examples, so I'm not sure if it exists.
The text was updated successfully, but these errors were encountered: