出现以下错误:
psycopg2.errors.StringDataRightTruncation: value too long for type character varying(64)
CONTEXT: COPY fngenderguessallprospssinglebatchv1, line 5396, column firstname: "Unsub-Ylvsccfmmlvnsclscdfhgdhvnrswzfrrbsfrffsasdfefasdfccqwtsb@wherever.Com"
我正在尝试在我创建的模式中创建表(称为
dbo
,这对我来说不是默认的)。首先我制作表格,然后我尝试从 csv 文件中读取以填充它们。注意:起初,我指定 Varchar(64)
作为表格列的 type
。看到上面的错误后,我把它改成了Varchar(200)
,然后就简单的Varchar
,但是错误并没有解决。最后,我尝试了Text
类型,但我仍然得到完全相同的错误!那么,为什么我的 type
处方被忽略,随后生成上述错误(这表明 type
现在是 Varchar(64)
,在我尝试使事情尽可能灵活之后???
创建表:
import psycopg2
from psycopg2 import sql
import os
import glob
connection = psycopg2.connect('postgresql://porter_test2:porter_test2@localhost:5432/porter_test2')
print("Establishing database connection")
db_schema_name = "dbo"
csvPath = r"D:\work_files\porter_project\demo_setup\csv_files"
csvPath = csvPath + "\\"
# Loop through each CSV
for file_name in glob.glob(csvPath+"*.csv"):
table_name = file_name.replace(csvPath, "").replace(".csv", "")
if len(db_schema_name) > 0:
table_name = db_schema_name + "." + table_name
# Open file
fileInput = open(file_name, "r")
# Extract first line of file
firstLine = fileInput.readline().strip()
# Split columns into an array [...]
columns = firstLine.split(",")
sqlQueryCreate = 'DROP TABLE IF EXISTS ' + table_name + ";\n"
sqlQueryCreate += 'CREATE TABLE ' + table_name + "("
# Define columns for table
for column in columns:
#sqlQueryCreate += column + " VARCHAR,\n"
sqlQueryCreate += column + " TEXT,\n"
sqlQueryCreate = sqlQueryCreate[:-2]
sqlQueryCreate += ");"
cursor = connection.cursor()
cursor.execute(sqlQueryCreate)
connection.commit()
cursor.close()
connection.close()
填充表(错误“发生”的地方):
import psycopg2
from psycopg2 import sql
import os
import glob
import csv
connection = psycopg2.connect('postgresql://porter_test2:porter_test2@localhost:5432/porter_test2')
print("Establishing database connection")
# specify database schema name
db_schema_name = "dbo"
csvPath = r"D:\work_files\porter_project\demo_setup\csv_files"
csvPath = csvPath + "\\"
# Loop through each CS
for file_name in glob.glob(csvPath+"*.csv"):
table_name = file_name.replace(csvPath, "").replace(".csv", "")
copy_sql = "COPY {table_name} FROM '{file}' WITH CSV HEADER DELIMITER as ',';".format(
table_name = table_name,
file = file_name,
)
with open(file_name, 'r') as f:
cursor = connection.cursor()
cursor.copy_expert(sql=copy_sql, file=f)
connection.commit()
cursor.close()
connection.close()