doucong1887
2018-09-13 05:47
浏览 130

Golang和PostgreSQL的创建表给我的问题

Using and following the documentation: https://godoc.org/github.com/lib/pq but can't see after hours and hours and research online to find any good example of passing variables to the db.Exec() I'm building a program that will create new tables depending on certain names entered on the command arguments.

db.Exec(`CREATE TABLE $1(
    ID INT PRIMARY KEY NOT NULL,
    HOST    TEXT    NOT NULL,
    PORTS   TEXT,
    BANNERS TEXT,
    JAVASCRIPT TEXT,
    HEADERS TEXT,
    COMMENTS TEXT,
    ROBOTS  TEXT,
    EMAILS  TEXT,
    CMS     TEXT,
    URLS    TEXT,
    BUSTIN  TEXT,
    VULN    TEXT
    )`,  tablename)

But no luck, I obviously have try to change things around, even I have try to build the CREATE TABLE syntax on a string and have try to pass that instead of db.Exec(string) but no luck neither... can someone give me a hand?

Thanks

图片转代码服务由CSDN问答提供 功能建议

使用并遵循以下文档: https://godoc.org/github.com/lib/pq 但几小时后都看不到,并且无法在线研究以找到有关 将变量传递给db.Exec() 我正在构建一个程序,该程序将根据在命令参数上输入的某些名称来创建新表。

  db.Exec(` 创建表$ 1(
 ID INT主键不为空,
主机文本不为空,
端口文本,
横幅文本,
 JAVASCRIPT文本,
标题文本,
注释文本,
机器人文本,  
电子邮件文本,
 CMS文本,
 URLS文本,
简短文本,
 VULN TEXT 
),表名)
   
 
 

但没有 运气,我显然已经尝试过改变,甚至我已经尝试 在字符串上构建CREATE TABLE语法,并尝试通过它而不是db.Exec(string)来传递 但也没有运气... c 有人帮我吗?

谢谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanqiao8925 2018-09-14 03:12
    已采纳

    You can check on https://golang.org/src/database/sql/sql.go?s=39599:39668#L1437, at line 1478, that sql statements will be first prepared then executed.

    In PostgreSQL, prepare are only valid for SELECT, INSERT, UPDATE, DELETE, or VALUES, https://www.postgresql.org/docs/10/static/sql-prepare.html .

    Here you can use Go's fmt.Sprintf to support creating different tables, and check table name manually, SQL table names can contain many special characters, but you can narrow it, mine validation is regexp.MustCompile("^[a-zA-Z_]+[0-9a-zA-Z_]*$") .

    点赞 打赏 评论

相关推荐 更多相似问题