thinkphp3.2连接 Postgres SQL
Posted jugt
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了thinkphp3.2连接 Postgres SQL相关的知识,希望对你有一定的参考价值。
1.开启php扩展
查看php 目录下的ext文件夹中,是否有php_pdo_pgsql.dll和php_pgsql.dll,如果没有,自己想办法
打开php.ini 写入下面两行
- extension=php_pdo_pgsql.dll
- extension=php_pgsql.dll
2.配置tp的数据库连接参数(config.php 或是 database.php)
return [
'DB_TYPE' => 'Pgsql', // 数据库类型
'DB_HOST' => 'localhost', // 服务器地址
'DB_NAME' => 'db_test', // 数据库名
'DB_USER' => 'postgres', // 用户名
'DB_PWD' => 'root', // 密码
'DB_PORT' => '5432', // 端口
'DB_PREFIX' => '', // 数据库表前缀
'DB_PARAMS' => [], // 数据库连接参数
'DB_DEBUG' => TRUE, // 数据库调试模式 开启后可以记录SQL日志
'DB_FIELDS_CACHE' => true, // 启用字段缓存
'DB_CHARSET' => 'utf8', // 数据库编码默认采用utf8
];
3.尝试连接,如果不报错误,连接完成
4.错误:
A.错误1
SQLSTATE[08006] [7] authentication method 10 not supported
修改PostgresSQL安装目录中data文件夹下的pg_hba.conf文件,
# IPv4 local connections:
#host all all 127.0.0.1/32 scram-sha-256
host all all 127.0.0.1/32 trust
将scram-sha-256改为trust,保存后,重启postgressql服务。
B.错误2
7:错误: 字段 “test” 不存在 LINE 1: …as “default”,fields_default as “extra” from table_msg(test); ^ [ SQL语句 ] : select fields_name as “field”,fields_type as “type”,fields_not_null as “null”,fields_key_name as “key”,fields_default as “default”,fields_default as “extra” from table_msg(test);
修改\\ThinkPHP核心文件目录\\Library\\Think\\Db\\Driver.class.php文件中,第41行,将
$result = $this->query('select fields_name as "field",fields_type as "type",fields_not_null as "null",fields_key_name as "key",fields_default as "default",fields_default as "extra" from table_msg('.$tableName.');');
修改为
$result = $this->query('select fields_name as "field",fields_type as "type",fields_not_null as "null",fields_key_name as "key",fields_default as "default",fields_default as "extra" from table_msg(\\''.$tableName.'\\');');
主要就是最后面的$tableName增加’转义
from table_msg(\\''.$tableName.'\\')
保存文件,尝试。
C.错误3
7:错误: 函数 table_msg(unknown) 不存在 LINE 1: …fault as “default”,fields_default as “extra” from table_msg(… ^ HINT: 没有匹配指定名称和参数类型的函数. 您也许需要增加明确的类型转换. [ SQL语句 ] : select fields_name as “field”,fields_type as “type”,fields_not_null as “null”,fields_key_name as “key”,fields_default as “default”,fields_default as “extra” from table_msg(‘test’);
在PostgresSQL中运行以下sql文件
CREATE OR REPLACE FUNCTION pgsql_type(a_type varchar) RETURNS varchar AS
$BODY$
DECLARE
v_type varchar;
BEGIN
IF a_type='int8' THEN
v_type:='bigint';
ELSIF a_type='int4' THEN
v_type:='integer';
ELSIF a_type='int2' THEN
v_type:='smallint';
ELSIF a_type='bpchar' THEN
v_type:='char';
ELSE
v_type:=a_type;
END IF;
RETURN v_type;
END;
$BODY$
LANGUAGE PLPGSQL;
CREATE TYPE "public"."tablestruct" AS (
"fields_key_name" varchar(100),
"fields_name" VARCHAR(200),
"fields_type" VARCHAR(20),
"fields_length" BIGINT,
"fields_not_null" VARCHAR(10),
"fields_default" VARCHAR(500),
"fields_comment" VARCHAR(1000)
);
CREATE OR REPLACE FUNCTION "public"."table_msg" (a_schema_name varchar, a_table_name varchar) RETURNS SETOF "public"."tablestruct" AS
$body$
DECLARE
v_ret tablestruct;
v_oid oid;
v_sql varchar;
v_rec RECORD;
v_key varchar;
BEGIN
SELECT
pg_class.oid INTO v_oid
FROM
pg_class
INNER JOIN pg_namespace ON (pg_class.relnamespace = pg_namespace.oid AND lower(pg_namespace.nspname) = a_schema_name)
WHERE
pg_class.relname=a_table_name;
IF NOT FOUND THEN
RETURN;
END IF;
v_sql='
SELECT
pg_attribute.attname AS fields_name,
pg_attribute.attnum AS fields_index,
pgsql_type(pg_type.typname::varchar) AS fields_type,
pg_attribute.atttypmod-4 as fields_length,
CASE WHEN pg_attribute.attnotnull THEN ''not null''
ELSE ''''
END AS fields_not_null,
'''' AS fields_default,
pg_description.description AS fields_comment
FROM
pg_attribute
INNER JOIN pg_class ON pg_attribute.attrelid = pg_class.oid
INNER JOIN pg_type ON pg_attribute.atttypid = pg_type.oid
LEFT OUTER JOIN pg_attrdef ON pg_attrdef.adrelid = pg_class.oid AND pg_attrdef.adnum = pg_attribute.attnum
LEFT OUTER JOIN pg_description ON pg_description.objoid = pg_class.oid AND pg_description.objsubid = pg_attribute.attnum
WHERE
pg_attribute.attnum > 0
AND attisdropped <> ''t''
AND pg_class.oid = ' || v_oid || '
ORDER BY pg_attribute.attnum' ;
FOR v_rec IN EXECUTE v_sql LOOP
v_ret.fields_name=v_rec.fields_name;
v_ret.fields_type=v_rec.fields_type;
IF v_rec.fields_length > 0 THEN
v_ret.fields_length:=v_rec.fields_length;
ELSE
v_ret.fields_length:=NULL;
END IF;
v_ret.fields_not_null=v_rec.fields_not_null;
v_ret.fields_default=v_rec.fields_default;
v_ret.fields_comment=v_rec.fields_comment;
SELECT constraint_name INTO v_key FROM information_schema.key_column_usage WHERE table_schema=a_schema_name AND table_name=a_table_name AND column_name=v_rec.fields_name;
IF FOUND THEN
v_ret.fields_key_name=v_key;
ELSE
v_ret.fields_key_name='';
END IF;
RETURN NEXT v_ret;
END LOOP;
RETURN ;
END;
$body$
LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
COMMENT ON FUNCTION "public"."table_msg"(a_schema_name varchar, a_table_name varchar)
IS '获得表信息';
---重载一个函数
CREATE OR REPLACE FUNCTION "public"."table_msg" (a_table_name varchar) RETURNS SETOF "public"."tablestruct" AS
$body$
DECLARE
v_ret tablestruct;
BEGIN
FOR v_ret IN SELECT * FROM table_msg('public',a_table_name) LOOP
RETURN NEXT v_ret;
END LOOP;
RETURN;
END;
$body$
LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
COMMENT ON FUNCTION "public"."table_msg"(a_table_name varchar)
IS '获得表信息';
以上是关于thinkphp3.2连接 Postgres SQL的主要内容,如果未能解决你的问题,请参考以下文章
THINKPHP3.2 中使用 soap 连接webservice 解决方案