0
我不斷收到22P02錯誤語法:invalid input syntax for integer: "{1,2,3}"
(見服務電話註釋)。PostgreSQL的錯誤22P02無效輸入整數
這裏是我的服務電話:
...
using (var command = new NpgsqlCommand("mediabase.create_media", connection))
{
command.CommandType = System.Data.CommandType.StoredProcedure;
command.Parameters.Add("title", NpgsqlTypes.NpgsqlDbType.Varchar).Value = media.title;
command.Parameters.Add("uniqueFilename", NpgsqlTypes.NpgsqlDbType.Varchar).Value = media.uniqueFilename;
command.Parameters.Add("description", NpgsqlTypes.NpgsqlDbType.Varchar).Value = media.description;
command.Parameters.Add("categoryIds", NpgsqlTypes.NpgsqlDbType.Varchar).Value = media.categoryIds; /* here the value is "1,2,3" */
command.Parameters.Add("typeId", NpgsqlTypes.NpgsqlDbType.Integer).Value = media.typeId;
command.Parameters.Add("ownerId", NpgsqlTypes.NpgsqlDbType.Integer).Value = media.ownerId;
command.Parameters.Add("statusId", NpgsqlTypes.NpgsqlDbType.Integer).Value = media.statusId;
command.Parameters.Add("gpsLocation", NpgsqlTypes.NpgsqlDbType.Varchar).Value = media.gpsLocation;
command.Parameters.Add("locationNameId", NpgsqlTypes.NpgsqlDbType.Integer).Value = media.locationId;
command.Parameters.Add("uploadUserId", NpgsqlTypes.NpgsqlDbType.Integer).Value = media.uploadUserId;
command.Parameters.Add("uploadDate", NpgsqlTypes.NpgsqlDbType.Varchar).Value = media.uploadDate;
command.Parameters.Add("uploadIp", NpgsqlTypes.NpgsqlDbType.Varchar).Value = media.uploadIp;
command.Parameters.Add("metadataId", NpgsqlTypes.NpgsqlDbType.Integer).Value = metadataId;
command.Parameters.Add("sysEnvironment", NpgsqlTypes.NpgsqlDbType.Varchar).Value = media.sysEnvironment;
command.Parameters.Add("languageId", NpgsqlTypes.NpgsqlDbType.Integer).Value = media.languageId;
command.Parameters.Add("publicationIds", NpgsqlTypes.NpgsqlDbType.Varchar).Value = media.publicationIds;
command.Parameters.Add("limitations", NpgsqlTypes.NpgsqlDbType.Varchar).Value = media.limitations;
mediaId = Convert.ToInt32(command.ExecuteScalar());
}
pgTransaction.Commit();
...
,並且存儲過程:
CREATE OR REPLACE FUNCTION mediabase.create_media(
title character varying,
uniquefilename character varying,
description character varying,
categoryids character varying,
typeid integer,
ownerid integer,
statusid integer,
gpslocation character varying,
locationnameid integer,
uploaduserid integer,
uploaddate character varying,
uploadip character varying,
metadataid integer,
sysenvironment character varying,
languageid integer,
publicationids character varying,
limitations character varying)
RETURNS integer AS
$BODY$
declare _mediaId integer;
declare _point varchar;
declare _gps_location geometry;
declare _id text;
begin
IF (gpslocation <> '') THEN
_point = 'POINT(' || gpslocation || ')';
_gps_location = ST_Transform(ST_GeomFromText(_point, 4326), 900913);
ELSE
_gps_location = NULL;
END IF;
insert into mediabase.media (
title,
unique_filename,
description,
owner_id,
status_id,
gps_location,
type_id,
location_name_id,
upload_user_id,
upload_date,
upload_ip,
metadata_id,
system_environment,
language_id,
limitations)
values (
title,
uniqueFilename,
description,
ownerId,
statusId,
_gps_location,
typeId,
locationNameId,
uploadUserId,
uploadDate,
uploadIp,
metadataId,
sysEnvironment,
languageid,
limitations)
returning id into _mediaId;
-- insert category ids
FOR _id IN SELECT string_to_array (categoryids,',')
LOOP
INSERT into mediabase.media_categories (media_id, category_id)
values (_mediaId, (_id::int));
END LOOP;
-- insert publication ids
FOR _id IN SELECT string_to_array (publicationids,',')
LOOP
INSERT into mediabase.media_publications (media_id, publication_id)
values (_mediaId, (_id::int));
END LOOP;
return _mediaId;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
正如你可以在服務方法看,參數categoryids
的輸入字符串"1,2,3"
,和NpgsqlDbType
是Varchar
。在存儲過程中,輸入類型爲character varying
。
下面是執行日誌:
2013-01-17 08:56:42 CET ERROR: invalid input syntax for integer: "{1,2,3}"
2013-01-17 08:56:42 CET CONTEXT: SQL statement "INSERT into mediabase.media_categories (media_id, category_id)
values (_mediaId, (_id::integer))"
PL/pgSQL function "create_media" line 54 at SQL statement
2013-01-17 08:56:42 CET STATEMENT: select * from mediabase.create_media(('Penguins')::varchar,('079117ec-676f-4022-9950-69e55c2a2600_Penguins.jpg')::varchar,('Description...')::varchar,('1,2,3')::varchar,(1)::int4,(1)::int4,(1)::int4,('')::varchar,(1)::int4,(1)::int4,('17/01/2013 08:56:42')::varchar,('::1')::varchar,(399)::int4,('dev')::varchar,(1)::int4,('1,2')::varchar,('Limitations...')::varchar)
爲什麼這個錯誤坡平了?
爲什麼不定義'characterids'爲'整數[]'?然後,您可以簡單地省略所有的強制轉換,並使用'generate_subscripts()'作爲插入而不是循環。無論如何,'string_to_array('{1,2,3}',',')'產生'{「{1」,2,「3}」}'。 – dezso
@dezso我更多的是C#開發人員,然後是SQL鯊魚。你可以添加一個示例代碼,它顯示了使用'generate-subscripts()'和正確的字符串拆分。謝謝。 – Ambran
你是絕對肯定的是,'media.categoryIds'作爲新加入的一個參數是一個字符串'1,2,3',而不是一個數組? '{1,2,3}'是數組'[1,2,3]'的PostgreSQL表示,所以也許你正在傳遞一個數組並且nPgSQL正在爲你轉換它?否則,括號從哪裏來? –