How to Import Rows into PostgreSQL from STDIN Using Go?

Susan Sarandon
Release: 2024-10-24 16:06:01
Original
302 people have browsed it

How to Import Rows into PostgreSQL from STDIN Using Go?

Importing Rows into PostgreSQL from STDIN Using Go

In Go, you can import rows into PostgreSQL from standard input (STDIN) using the pq package. This approach directly feeds the data into the database without the need for intermediate files.

To achieve direct row importing from STDIN, follow these steps:

  1. Open a PostgreSQL Connection: Establish a connection to your PostgreSQL database using the sql.Open function.
  2. Begin a Transaction: Initiate a database transaction using the Begin function to group multiple statements together.
  3. Prepare Copy Statement: Create a prepared statement for copying data using pq.CopyIn. This statement specifies the target table name and the columns to import.
  4. Import Rows: Execute the prepared statement iteratively for each row of data you want to import. Use the Exec function to send each row to the database.
  5. Terminate Copy Statement: After adding all the rows, execute the prepared statement again without any arguments to finalize the import process.
  6. Close Prepared Statement: Release resources associated with the prepared statement by calling the Close method.
  7. Commit Transaction: Commit the transaction to make the changes permanent in the database.

Example Code:

Here's a code example that demonstrates row importing from STDIN using Go:

<code class="go">package main

import (
    "database/sql"
    "fmt"
    "io"
    "log"

    "github.com/lib/pq"
)

func main() {
    db, err := sql.Open("postgres", "host=localhost port=5432 user=postgres password=mysecret dbname=mydatabase")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    rows := [][]string{
        {"Rob", "Pike"},
        {"Ken", "Thompson"},
        {"Robert", "Griesemer"},
    }

    txn, err := db.Begin()
    if err != nil {
        log.Fatal(err)
    }

    stmt, err := txn.Prepare(pq.CopyIn("test", "first_name", "last_name"))
    if err != nil {
        log.Fatal(err)
    }

    for _, r := range rows {
        if _, err = stmt.Exec(r[0], r[1]); err != nil {
            log.Fatal(err)
        }
    }

    if _, err = stmt.Exec(); err != nil {
        log.Fatal(err)
    }

    if err = stmt.Close(); err != nil {
        log.Fatal(err)
    }

    if err = txn.Commit(); err != nil {
        log.Fatal(err)
    }

    fmt.Println("Rows imported successfully.")
}</code>
Copy after login

By following these steps and utilizing the pq package, you can efficiently import data into PostgreSQL directly from STDIN in your Go programs.

The above is the detailed content of How to Import Rows into PostgreSQL from STDIN Using Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!