Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7572723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:59:48+00:00 2026-05-30T15:59:48+00:00

This must be something very basic. I’m not familiar with the syntax, and this

  • 0

This must be something very basic. I’m not familiar with the syntax, and this is my first most difficult problem. connection below returns something of type IO Connection – but I don’t understand how IO and Connection are connected / what does it mean. If you can, please tell what is the purpose of IO in this operation and how can I loose it.

{-# LANGUAGE OverloadedStrings #-}

import Database.MySQL.Simple

-- This returns `IO Connection'
connection :: Connection
connection = do
             c <- connect defaultConnectInfo
                          { connectPassword = "password",
                            connectDatabase = "database" }
             return c
-- Possibly need to do something to `c', but don't know what

first_table :: IO String
first_table = do
              [Only i] <- query_ connection "show tables"
              return i

test_query :: IO Int
test_query = do
             [Only i] <- query_ connection "select 2 + 2"
             return i

However, my final goal was to find a way to only connect to the database once, store the connection handle somewhere and reuse it when first or second functions are called. I’m nowhere near understanding how that may be done (in Erlang, for example, I’d need to open a separate process and wait for messages requesting the connection, which is extremely cumbersome and uncomfortable – so I hope, I wouldn’t need to go through anything like that…).

Thanks in advance.

EDIT:

{-# LANGUAGE OverloadedStrings #-}

import Database.MySQL.Simple
-- I need, instead of this function something that only connects once to the databse.
connection :: IO Connection
connection = do
             c <- connect defaultConnectInfo
                          { connectPassword = "password",
                            connectDatabase = "database" }
             return c

first_table :: IO String
first_table = do
              c <- connection
              [Only i] <- query_ c "show tables"
              return i

test_query :: IO Int
test_query = do
             c <- connection
             [Only i] <- query_ c "select 2 + 2"
             return i

Think of connection as if it was, well, let it be Java for a change:

public Connection getConnection() {
    if (connection == null) connection = /* JDBC does it's dirty job 
                             and creates connection handle */
    return connection;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:59:50+00:00Added an answer on May 30, 2026 at 3:59 pm

    IO is parametrized type (called type constructor in Haskell or generic in C#/Java). In this case String is its type argument.

    IO String in Haskell would be written as IO<String> in Java/C#/C++.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems very basic and I must be missing something, but here goes anyways...
Now I must be missing something here, as this seems a very basic issue
I must be missing something very simple, but can't find the answer to this.
I find this very strange, must be something I'm doing wrong, but still... I'm
I must be missing something very basic in the CSS. My jQuery anchor button
This must be something very simple for the JavaScript experts out there. In the
This must be a basic question but I am struggling with this problem for
Something must be wrong with this code right here: + (UIImage*)captureView:(UIView *)theView { UIGraphicsBeginImageContext(theView.frame.size);
I have a UI-dialog something like this: You must choose a book from a
I know SQL well but I must be missing something really dumb here. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.