简体   繁体   English

用Go插入Postgresql表

[英]Inserting into a Postgresql table with Go

I have the following table: 我有下表:

CREATE TABLE Users (
  user_id BIGSERIAL PRIMARY KEY,
  email VARCHAR(50) NOT NULL,
  password_hash VARCHAR(100) NOT NULL,
  points INT DEFAULT 0,
  created_at TIMESTAMP NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)

I take in a email and password, encrypt the password and attempt to insert them into the table: 我输入电子邮件和密码,加密密码,然后尝试将其插入表格中:

import (
  _ "github.com/lib/pq"
  "database/sql"
  "code.google.com/p/go.crypto/bcrypt"
)

conn := OpenConnection()
defer conn.Close()
email := r.FormValue("email")
password, _ := bcrypt.GenerateFromPassword([]byte(r.FormValue("password")), bcrypt.DefaultCost)
res, err := conn.Exec("INSERT INTO users (email, password_hash) VALUES (?, ?)", email, password)
http.Redirect(w, r, "/", http.StatusFound)

The insert throws pq: P:"51" S:"ERROR" L:"1002" C:"42601" M:"syntax error at or near \\",\\"" F:"scan.l" R:"scanner_yyerror" . 插入引发pq: P:"51" S:"ERROR" L:"1002" C:"42601" M:"syntax error at or near \\",\\"" F:"scan.l" R:"scanner_yyerror" The same error is thrown if i replace the byte type password with a simple string. 如果我用简单的字符串替换字节类型密码,则会引发相同的错误。

What am I doing wrong? 我究竟做错了什么?

I will post as an answer in case the link goes away: From https://github.com/lib/pq/issues/65 如果链接消失,我将作为答案发布:来自https://github.com/lib/pq/issues/65

by dpapathanasiou : I've figured out the problem; dpapathanasiou :我已经解决了问题; it's not a bug in pq but in the way I was creating the prepared statements: I need to use ($n) instead of ? 这不是pq中的错误,而是以我创建预准备语句的方式:我需要使用($ n)而不是? for the bound parameters. 绑定参数。

So all of these statements work correctly: 因此,所有这些语句都可以正常工作:

stmt, err := db.Prepare("select id from people where firstname = ($1) and lastname = ($2)")
stmt, err := db.Prepare("select id from people where firstname ~* ($1) and lastname ~* ($2)")
stmt, err := db.Prepare("select id from people where firstname = ($1)")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将表的行以及单独的值插入到Postgresql中的另一个表中 - Inserting rows of a table along with a separate value into another table in postgresql <postgresql>使用json值将数据从临时表插入主表 - <postgresql> inserting data from temporary table to main table with json values SQL-插入postgresql表会产生分号错误 - SQL - Inserting into postgresql table produces error on semi-colon 在PostgreSQL中将新记录插入到行号为id的表中 - Inserting new records into table with row number as id with PostgreSQL 使用JDBC从MATLAB将数据插入到PostgreSQL表中会抛出BatchUpdateException - Inserting data into PostgreSQL table from MATLAB with JDBC throws BatchUpdateException 如何在PostgreSQL中的psycopg2 / python中将数据插入到一对多(必须)关系中 - How would I go about inserting data into one-to-many(must) relation, psycopg2/python in PostgreSQL 如何使用Go函数更新PostgresQL表中的几列 - How to update several columns in PostgresQL table with a Go function 如何一次性从PostgreSQL表中删除所有NOT NULL约束 - How to drop all NOT NULL constraints from a PostgreSQL table in one go Postgresql函数与表作为输入? 是plpgsql函数中的动态SQL的方法吗? - Postgresql function with table as input? Is dynamic SQL in a plpgsql function the way to go? 在PostgreSQL中将值插入数组 - Inserting values into array in PostgreSQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM