Postgresバルクインポーターにtxtを書き込もうとしています。postgresに挿入されるはずの文字列が有効なUTF8ではないため、コードは現在クラッシュします。pq: invalid byte sequence for encoding UTF8: 0x00
私のコードでは、文字列が有効なUTF8かどうかを確認しています。
何が欠けていますか?
コード:
for {
line, more := <-lineChannel
splitLine := strings.SplitN(line, ":", 2)
if len(splitLine) == 2 {
if utf8.Valid([]byte(splitLine[0])) && utf8.Valid([]byte(splitLine[1])) {
lineCount++
_, err = stmt.Exec(splitLine[0], splitLine[1])
if lineCount%int64(copySize) == 0 {
_, err = stmt.Exec()
if err != nil {
log.Fatal("Failed at stmt.Exec", err)
}
err = stmt.Close()
if err != nil {
log.Fatal("Failed at stmt.Close", err)
}
err = txn.Commit()
if err != nil {
log.Fatal("failed at txn.Commit", err)
}
txn, err = db.Begin()
if err != nil {
log.Fatal("failed at db.Begin", err)
}
stmt, err = txn.Prepare(pq.CopyIn("pwned", "username", "password"))
if err != nil {
log.Fatal("failed at txn.Prepare", err)
}
if lineCount%(int64(copySize)*10) == 0 {
log.Printf("Inserted %v lines", lineCount)
}
}
if err != nil {
log.Println("error:", splitLine[0], splitLine[1])
log.Fatal(err)
}
}
編集:エラーを発生させる行:
バイト[]: [116 109 97 105 108 46 99 111 109 58 104 117 115 104 112 117 112 112 105 101 115 108 111 118 101]
ライン: [email protected]:hushpuppieslove
splitLine [0] + splitLine [1]: [email protected] hushpuppieslove
0x00はnull文字であり、postgresは文字列でこれを許可しません。ドキュメントから:
テキストデータタイプはそのようなバイトを格納できないため、NULL(0)文字は使用できません。
ヌル文字を取り除く必要があります。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加