How to use MySQL

The LAMP stack has been very popular on the internet in recent years, and the M in LAMP stand for MySQL. MySQL is famous because it's open source and easy to use. As such, it has become the de-facto database in the back-ends of many websites.

MySQL drivers

There are a couple of drivers that support MySQL in Go. Some of them implement the database/sql interface, and others use their own interface standards.

I'll use the first driver in the following examples (I use this one in my personal projects too), and I also recommend that you use it for the following reasons:

  • It's a new database driver and supports more features.

  • It fully supports database/sql interface standards.

  • Supports keep-alive, long connections with thread-safety.

Samples

In the following sections, I'll use the same database table structure for different databases, then create SQL as follows:

    CREATE TABLE `userinfo` (
        `uid` INT(10) NOT NULL AUTO_INCREMENT,
        `username` VARCHAR(64) NULL DEFAULT NULL,
        `department` VARCHAR(64) NULL DEFAULT NULL,
        `created` DATE NULL DEFAULT NULL,
        PRIMARY KEY (`uid`)
    );

The following example shows how to operate on a database based on the database/sql interface standards.

Let me explain a few of the important functions here:

  • sql.Open() opens a registered database driver. The Go-MySQL-Driver registered the mysql driver here. The second argument is the DSN (Data Source Name) that defines information pertaining to the database connection. It supports following formats:

  • db.Prepare() returns a SQL operation that is going to be executed. It also returns the execution status after executing SQL.

  • db.Query() executes SQL and returns a Rows result.

  • stmt.Exec() executes SQL that has been prepared and stored in Stmt.

Note that we use the format =? to pass arguments. This is necessary for preventing SQL injection attacks.

Last updated

Was this helpful?