database/sql Exec COPY FROM stdin syntax error

agolangf · · 708 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Heya,</p> <p>I&#39;m working to populate a Postgres database using a pg_dump file and I&#39;m getting a syntax error on a COPY FROM stdin line inside the dump. The basic format of the query is like this:</p> <pre><code>COPY the_thing (id, policy_id, created_at) FROM stdin; 1 3 2016-02-26 18:10:40.193308+00 2 3 2016-02-26 18:10:40.193308+00 3 8 2016-02-26 18:10:40.205101+00 4 8 2016-02-26 18:10:40.205101+00 \. </code></pre> <p>And I&#39;m getting this error: pq: syntax error at or near &#34;1&#34; (code = 42601 pos = 12191)</p> <p>All I&#39;m doing is using ioutil.Readfile to pump the entire pg_dump file into a string, then passing to Exec on the DB object. I&#39;ve tried using a TX as well, and I can&#39;t make a prepared statement as it&#39;s multiple statements in the string.</p> <p>Any ideas? I feel like &#34;stdin&#34; in this context is a misnomer, but I am no expert on this matter. Thoughts?</p> <p>(edited for formatting)</p> <hr/>**评论:**<br/><br/>dinkumator: <pre><p>Error message itself is probably due to the semicolon, but... There&#39;s a special handler method for bulk imports, you shouldn&#39;t use a normal query: <a href="https://godoc.org/github.com/lib/pq#hdr-Bulk_imports" rel="nofollow">https://godoc.org/github.com/lib/pq#hdr-Bulk_imports</a></p> <p>Although if you dig into the details you might be able to do more (looks like each row will have to be sent over the wire, not in bulk): <a href="https://github.com/lib/pq/blob/master/copy.go#L20" rel="nofollow">https://github.com/lib/pq/blob/master/copy.go#L20</a></p></pre>junkdrome: <pre><p>I did notice the bulk copy API last night while scrambling around and figured it&#39;d be my huckleberry.</p> <p>Your response is very appreciated.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

708 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传