sql/

directory
v0.0.0-...-86d22a7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2017 License: Apache-2.0

README

SQL-like Databases

Although there is a good bit of hype around interesting NoSQL databases and key/values stores, SQL-like databases are still ubiquitous. You can access, query, and otherwise interact with such databases via the databases/sql package in Go stdlib. There are a variety of libraries for databases/sql that allow you to connect to MySQL, Postgres, etc. In the following, we will be using a Postgres database hosted by ElephantSQL to illustrate database/sql functionality.

Get a Postgres server

  • Create an account on ElephantSQL.
  • Create a free "tiny turtle" instance.
  • Get the URL from the details page of your "tiny turtle" instance.
  • Export this URL to a environmental variable PGURL.
  • Install the psql CLI tool.

Notes

  • A sql.DB value is not a database connection.
  • Using the sql.DB value, databases/sql (i) opens and closes connections to the actual underlying database, via the specified driver, and (ii) manages a pool of connections as needed.
  • If you fail to release connections, you can cause databases/sql to open a lot of connections.
  • After creating a sql.DB value, you can use it to query the database, execute statements, and commit transactions.
  • When you iterate over rows and scan them into variables, Go performs data type conversions behind the scenes.

Go database/sql tutorial
Common Go pitfalls when working with database/sql

Code Review

Open a database, ping the connection
Load data into a database
Retrieve data from a database
Modify data in a database

Exercises

Exercise 1

Query the iris table returning the sum of values in each of the sepal length, sepal width, petal length, and petal width columns grouped by species. Output the results to standard out.

Template | Answer

Exercise 2

Remove any rows in the iris table with sepal_length greater than 6.0.

Template | Answer


All material is licensed under the Apache License Version 2.0, January 2004.

Directories

Path Synopsis
Sample program to connect to and ping a database connection.
Sample program to connect to and ping a database connection.
Sample program to load the iris dataset into a database.
Sample program to load the iris dataset into a database.
Sample program to retrieve results from a database.
Sample program to retrieve results from a database.
Sample program to modify data in a database.
Sample program to modify data in a database.
exercises
exercise1
Sample program to retrieve results from a database.
Sample program to retrieve results from a database.
exercise2
Sample program to delete rows in a database table.
Sample program to delete rows in a database table.
template1
Sample program to retrieve results from a database.
Sample program to retrieve results from a database.
template2
Sample program to delete rows in a database table.
Sample program to delete rows in a database table.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL