GVKun编程网logo

PostgreSQL 字符串处理与日期处理操作

23

针对PostgreSQL字符串处理与日期处理操作这个问题,本篇文章进行了详细的解答,同时本文还将给你拓展C++字符串处理操作符重载、centos7下源码编译安装php支持PostgreSQLpostg

针对PostgreSQL 字符串处理与日期处理操作这个问题,本篇文章进行了详细的解答,同时本文还将给你拓展C++字符串处理操作符重载、centos 7下源码编译安装php支持PostgreSQL postgresql手册 postgresql官网下载 postgresql视频教、Hibernate插入批处理与分区PostgreSQL、MacOS下对postgresql的简单管理操作等相关知识,希望可以帮助到你。

本文目录一览:

PostgreSQL 字符串处理与日期处理操作

PostgreSQL 字符串处理与日期处理操作

  项目招商找A5 快速获取精准代理名单

这篇文章主要介绍了PostgreSQL 字符串处理与日期处理操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧。

字符串长度、大小写

1SELECT CHAR_LENGTH('test') -- 字符串长度
SELECT LENGTH('test')
LENGTH(string,encoding name)
SELECT LENGTH('测试','UTF-8');
LOWER(string) 或者 UPPER(string) -- 大小写
ASCII(string)
SELECT ASCII('abc') -- 结果是'a'的ascii码

 

字符串格式化

1FORMAT(formatstr text [,formatarg "any" [, ...] ]) -- 类似于printf

字符串拼接

SELECT 'number' || 123 --字符串连接
CONCAT(str "any" [, str "any" [, ...] ])
CONCAT_WS(sep text, str "any" [,str "any" [, ...] ])
SELECT * FROM CONCAT_WS('#','hello','world')

 

字符串剪切与截取

LPAD(string text, length int [,fill text])
RPAD(string text, length int [,fill text])
SELECT LPAD('12345', 10,'0') -- 结果 "0000012345"
TRIM([leading | trailing | both] [characters] from string)
SELECT TRIM(both ' ' from ' hello world') -- 结果是'hello world'
BTRIM(string text [, characters text])
RTRIM(string text [, characterstext])
LTRIM(string text [, characterstext])
SELECT BTRIM('yyhello worldyyyy','y') -- 结果是'hello world'
LEFT(str text, n int) -- 返回字符串前n个字符,n为负数时返回除最后|n|个字符以外的所有字符
RIGHT(str text, n int)
SUBSTRING(string from int [for int])
SELECT SUBSTRING('hello world' from 7 for 5) -- 结果是'world'

 

字符串加引号

QUOTE_IDENT(string text)
QUOTE_LITERAL(STRING TEXT)
QUOTE_LITERAL(value anyelement)
SELECT 'l''host"' -- 结果是'l'host"'
SELECT QUOTE_LITERAL('l''host"') -- 结果是'l''host"'

 

字符串分割

SPLIT_PART(string text,delimiter text, field int)
REGEXP_SPLIT_TO_ARRAY(stringtext, pattern text [, flags text])
REGEXP_SPLIT_TO_TABLE(stringtext, pattern text [, flagstext])
SELECT SPLIT_PART('hello#world','#',2) -- 结果是'world'
SELECT REGEXP_SPLIT_TO_ARRAY('hello#world','#') -- 结果是{hello,world}
SELECT REGEXP_SPLIT_TO_TABLE('hello#world','#') as split_res -- 结果是两行,第一行hello,第二行world

 

字符串查找、反转与替换

POSITION(substring in string) -- 查找
SELECT POSITION('h' in 'hello world') -- 结果是1,这里从1开始计数
REVERSE(str)
REPEAT(string text, number int)
REPLACE(string,string,string)
SELECT REPLACE('hello world',' ','#')
REGEXP_MATCHES(string text,pattern text [, flags text])
REGEXP_REPLACE(string text,pattern text,replacement text[, flags text])
SELECT REGEXP_MATCHES('hello world','.o.','g') -- 返回两行,第一行是'lo ',第二行是'wor'
SELECT REGEXP_MATCHES('hello world','.o.') -- 返回第一个匹配,'lo '

 

时间处理

SELECT TO_CHAR(TO_TIMESTAMP(CREATE_TIME),'YYYY-MM-DD HH24:MI:SS')
SELECT EXTRACT(YEAR FROM NOW());

 

补充:postgresql处理时间函数 截取hh:mm/yyyy-mm-dd

1.to_timestamp:

1AND to_timestamp(a.upload_time,'yyyy-MM-dd')>='"+startTime+"' and to_timestamp(a.upload_time,'yyyy-MM-dd') <= '"+endTime+"'

2.substring:

1substring('2019-04-08 14:18:09',index,k):

数值代表含义 index:代表从index开始截取数据,k代表从index开始截取到第k个数据

处理对象:时间为字符串格式的数据

eg:

截取时间到 年-月-日:

1SELECT substring(upload_time,1,10) from table WHERE upload_time='2019-04-08 14:18:09'

结果:2019-04-08

截取时间到 时:分:

1SELECT substring(upload_time,12,5) from table WHERE upload_time='2019-04-08 14:18:09'

结果:14:18

文章来源:

来源地址:https://www.jb51.net/article/205167.htm

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 yy@haotui.cn 举报,一经查实,本站将立刻删除。

C++字符串处理操作符重载

C++字符串处理操作符重载

&& || 不能进行运算符重载,因为运算符重载不能进行截断操作
截断操作就是当 a || b,a为真的时候就不会再判断b了,但是运算符重载不能达到这效果。

类定义

// 类定义

#include <iostream>
using namespace std;

//c中没有字符串 字符串类(c风格的字符串)
//空串 ""
class  MyString
{
   
   
	friend ostream& operator<<(ostream &out, MyString &s);
	friend istream& operator>>(istream &in, MyString &s);
public:
	MyString(int len = 0);
	MyString(const char *p);
	MyString(const MyString& s);
	~MyString();

public: //重载=号操作符
	MyString& operator=(const char *p);
	MyString& operator=(const MyString &s);
	char& operator[] (int index);

public: //重载 == !== 
	bool operator==(const char *p) const;
	bool operator==(const MyString& s) const;
	bool operator!=(const char *p) const;
	bool operator!=(const MyString& s) const;

public:
	int operator<(const char *p);
	int operator>(const char *p);
	int operator<(const MyString& s);
	int operator>(const MyString& s);

	//把类的指针 露出来
public:
	char *c_str()
	{
   
   
		return m_p;
	}
	const char *c_str2()
	{
   
   
		return m_p;
	}
	int length()
	{
   
   
		return m_len;
	}
private:
	int		m_len;
	char	 *m_p;

};

  • << 和 >> 操作符重载
ostream& operator<<(ostream &out, MyString &s)
{
   
   
	out<<s.m_p;
	return out;
}

istream& operator>>(istream &in, MyString &s)
{
   
   
	cin>>s.m_p;
	return in;
}
  • copy构造函数
//拷贝构造函数
//MyString s3 = s2;

MyString::MyString(const MyString& s)
{
   
   
	this->m_len = s.m_len;
	this->m_p = new char[m_len +1];
	strcpy(this->m_p, s.m_p);
}
  • =运算符重载
 s4 = "s2222";
MyString& MyString::operator=(const char *p)
{
   
   
	//1 旧内存释放掉
	if (m_p != NULL)
	{
   
   
		delete [] m_p;
		m_len = 0;
	}
	//2 根据p分配内存
	
	if (p == NULL)
	{
   
   
		m_len = 0;
		m_p = new char[m_len + 1];
		strcpy(m_p, "");
	}
	else
	{
   
   
		m_len = strlen(p);
		m_p = new char[m_len + 1];
		strcpy(m_p, p);
	}
	return *this;
}

// s4 = s2;
MyString& MyString::operator=(const MyString &s)
{
   
   
	//1 旧内存释放掉
	if (m_p != NULL)
	{
   
   
		delete [] m_p;
		m_len = 0;
	}
	//2 根据s分配内存
	m_len = s.m_len;
	m_p = new char[m_len + 1];
	strcpy(m_p, s.m_p);

	return *this;
}

[]运算符重载


 s4 = "s2222";
MyString& MyString::operator=(const char *p)
{
   
   
	//1 旧内存释放掉
	if (m_p != NULL)
	{
   
   
		delete [] m_p;
		m_len = 0;
	}
	//2 根据p分配内存
	
	if (p == NULL)
	{
   
   
		m_len = 0;
		m_p = new char[m_len + 1];
		strcpy(m_p, "");
	}
	else
	{
   
   
		m_len = strlen(p);
		m_p = new char[m_len + 1];
		strcpy(m_p, p);
	}
	return *this;
}

// s4 = s2;
MyString& MyString::operator=(const MyString &s)
{
   
   
	//1 旧内存释放掉
	if (m_p != NULL)
	{
   
   
		delete [] m_p;
		m_len = 0;
	}
	//2 根据s分配内存
	m_len = s.m_len;
	m_p = new char[m_len + 1];
	strcpy(m_p, s.m_p);

	return *this;
}

== 和 !=运算符重载


//if (s2 == "s222222")
bool MyString::operator==(const char *p) const
{
   
   
	if (p == NULL)
	{
   
   
		if (m_len == 0)
		{
   
   
			return true;
		}
		else
		{
   
   
			return false;
		}
	}
	else
	{
   
   
		if (m_len == strlen(p))
		{
   
   
			return !strcmp(m_p, p);
		}
		else
		{
   
   
			return false;
		}
	}
}

bool MyString::operator!=(const char *p) const
{
   
   
	return !(*this == p);
}


bool MyString::operator==(const MyString& s)  const
{
   
   
	if (m_len != s.m_len)
	{
   
   
		return false;
	}
	return !strcmp(m_p, s.m_p);
}

bool MyString::operator!=(const MyString& s) const
{
   
   
	return !(*this == s);
}

> 和<操作符重载

//if (s3 < "bbbb")
int MyString::operator<(const char *p)
{
   
   
	return strcmp(this->m_p , p);
}

int MyString::operator>(const char *p)
{
   
   
	return strcmp(p, this->m_p);
}

int MyString::operator<(const MyString& s)
{
   
   
	return strcmp(this->m_p , s.m_p);
}

int MyString::operator>(const MyString& s)
{
   
   
	return  strcmp(s.m_p, m_p);
}

centos 7下源码编译安装php支持PostgreSQL postgresql手册 postgresql官网下载 postgresql视频教

centos 7下源码编译安装php支持PostgreSQL postgresql手册 postgresql官网下载 postgresql视频教

1. 下载源码

$ mkdir /usr/downloads
$ wget -c http://cn2.php.net/distributions/php-5.6.20.tar.gz
$ tar -xvf php-5.6.20.tar.gz
$ mv php-5.6.20 /usr/local/src
$ cd !$ &amp; cd php-5.6.20
登录后复制

2. 阅读安装指导

$ ls -also
$ less README
$ less INSTALL
登录后复制

3. 安装依赖包

$ yum install apr apr-util apr-devel apr-util-devel prce lynx
登录后复制

4. 安装httpd

$ wget -c http://apache.fayea.com//httpd/httpd-2.4.20.tar.gz
$ tar -xvf httpd-2.4.20.tar.gz
$ cd httpd-2.4.20
$ ./configure \
--prefix=/usr/local/programs/apache2 \
--enable-rewrite \
--enable-so \
--enable-headers \
--enable-expires \
--with-mpm=worker \
--enable-modules=most \
--enable-deflate \
--enable-module=shared
$ make
$ make install
$ cd /usr/local/programs/apache2
$ cp bin/apachectl /etc/init.d/httpd ## 复制启动脚本
$ /etc/init.d/httpd start ## 启动apache服务器,访问http://localhost/
$ egrep -v ''^[ ]*#|^$'' /usr/local/apache2/conf/httpd.conf | nl ## 查看apache服务器的配置
## 将apache加入系统服务
vi /etc/rc.d/rc.local
```
/usr/local/programs/apache2/bin/apachectl start
```
$ cat /etc/rc.local
登录后复制

4. 安装postgresql

立即学习“PHP免费学习笔记(深入)”;

$ yum install readline-devel  ## 安装readline依赖
$ cd /usr/downloads
$ wget -c https://ftp.postgresql.org/pub/source/v9.5.0/postgresql-9.5.0.tar.bz2
$ tar -xvf postgresql-9.5.0.tar.bz2
$ cd postgresql-9.5.0
$ ./configure --prefix=/usr/local/programs/postgresql
$ make
$ su
$ make install
$ /sbin/ldconfig /usr/local/programs/postgresql/lib ## 刷新下共享动态库
$ cd /usr/local/programs/postgresql
$ bin/psql --version  ## 检查运行情况
## 开始对postgresql的配置
$ vi /etc/profile.d/postgresql.sh ## 增加环境变量,不推荐直接在/etc/profile中添加,系统更新升级时会需要merge
``` 
PATH=/usr/local/programs/postgresql:$PATH
export PATH
```
$ source /etc/profile ## 更新环境变量

## 增加用户和其他文件夹
$ adduser postgres
$ passwd postgres 
$ mkdir /usr/local/programs/postgresql/logs
$ mkdir /usr/local/programs/postgresql/data
$ chown postgres /usr/local/programs/postgresql/data
$ su - postgres

## 初始化数据库
$ ./bin/initdb -D ./data
$ ./bin/createdb test
$ ./bin/psql test
## 已有数据库,可导入data文件夹后尝试root访问,假如带密码,可能需要进一步研究下
$ ./bin/postgres -D ./data &gt;./logs/start-log-1.log 2&gt;&amp;1 &amp;
$ ./bin/psql --list  ##列出数据库
## ok,安装完成

## 自定义设置,权限控制等,可以跳过,等熟悉使用后再做
## 编辑数据库配置及权限文件:
$ vi /usr/local/programs/postgresql/data/postgresql.conf   ## 数据库配置文件
$ chown postgres postgresql.conf
$ chmod 644 postgresql.conf
$ vi /usr/local/programs/postgresql/data/pg_hba.conf   ## 权限文件
$ vi /usr/local/programs/postgresql/data/pg_ident.conf

## 设置开机自启动:
$ vi /etc/rc.d/rc.local    ## 添加如下内容
```
/usr/local/programs/postgresql/bin/postgresql start
```
登录后复制

5. 安装php

## 源码已经在第一步中下载,现在开始安装:
$ yum install libxml2 libxml2-devel libpng libpng-devel libjpeg libjpeg-devel freetype freetype-devel
$ ./configure \
--prefix=/usr/local/programs/php \
--with-apxs2=/usr/local/programs/apache2/bin/apxs \
--with-zlib \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-zlib-dir \
--enable-mbstring \
--with-pgsql=/usr/local/programs/postgresql \
--with-pdo-pgsql=/usr/local/programs/postgresql 
$ make
$ make test
&gt; Bug #42718 (unsafe_raw filter not applied when configured as default filter) [ext/filter/tests/bug42718.phpt]  XFAIL REASON: FILTER_UNSAFE_RAW not applied when configured as default filter, even with flags
&gt; Bug #67296 (filter_input doesn''t validate variables) [ext/filter/tests/bug49184.phpt]  XFAIL REASON: See Bug #49184
&gt; Bug #53640 (XBM images require width to be multiple of 8) [ext/gd/tests/bug53640.phpt]  XFAIL REASON: Padding is not implemented yet
&gt; zend multibyte (7) [ext/mbstring/tests/zend_multibyte-07.phpt]  XFAIL REASON: https://bugs.php.net/bug.php?id=66582
&gt; zend multibyte (9) [ext/mbstring/tests/zend_multibyte-09.phpt]  XFAIL REASON: https://bugs.php.net/bug.php?id=66582
&gt;Bug #70470 (Built-in server truncates headers spanning over TCP packets) [sapi/cli/tests/bug70470.phpt]  XFAIL REASON: bug is not fixed yet

## 查阅官方的bug,发现:
&gt; id=66582: status : Closed. Fixed in master (PHP7)
&gt; id=42718: status : Assigned
&gt; id=42718: reference to id=49184, unsolved for many years
## 那就不关心了,直接装吧
$ make install
&gt; You may want to add: /usr/local/programs/php/lib/php to your php.ini include_path

## 那就按它说的设置吧
$ cp php.ini-development /usr/local/programs/php/lib/php.ini
```
include_path = ".;/usr/local/programs/php/lib/php"

## 然后,编辑httpd的设置,确保其能正确解析php文件
```
...
LoadModule php5_module modules/libphp5.so
...
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .php5

...
<ifmodule dir_module>
    DirectoryIndex index.html index.php
</ifmodule>     
```

## 重启httpd,测试
$ cd /usr/local/programs/apache2
$ bin/httpd -h
$ bin/httpd -k stop
$ bin/httpd -f conf/httpd.conf
## 默认设置的www页面在./htdocs/下,那就先去里面建一个测试页面吧
$ vi htdocs/index.php
```
<?php phpinfo(); ?>
```
$ curl http://localhost/index.php |grep postgresql
#ok
登录后复制

后续应该做的事

* 1. 启动时,不需要要手动指定配置文件
* 2. php初始化www目录设置
* 3. php 用户、权限管理等

'').addClass(''pre-numbering'').hide(); $(this).addClass(''has-numbering'').parent().append($numbering); for (i = 1; i '').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了centos 7下源码编译安装php支持PostgreSQL,包括了postgresql,centos 7方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Hibernate插入批处理与分区PostgreSQL

Hibernate插入批处理与分区PostgreSQL

有没有解决方案通过hibernate在分区后的PostgreSQL表中批量插入?目前我遇到这样的错误…

ERROR org.hibernate.jdbc.AbstractBatcher - Exception executing batch:org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1   at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)   at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)   at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)....

我已经找到此链接http://lists.jboss.org/pipermail/hibernate-
dev/2007-October/002771.html,但我在网上找不到任何地方可以解决此问题或如何解决该问题

答案1

小编典典

您可能想通过设置hibernate.jdbc.factory_class属性尝试使用自定义批处理程序。确保hibernate状态不会检查批处理操作的更新次数可能会解决您的问题,可以通过使自定义批处理程序扩展类BatchingBatcher,然后覆盖方法doExecuteBatch(…)来实现此目的:

    @Override    protected void doExecuteBatch(PreparedStatement ps) throws SQLException, HibernateException {        if ( batchSize == 0 ) {            log.debug( "no batched statements to execute" );        }        else {            if ( log.isDebugEnabled() ) {                log.debug( "Executing batch size: " + batchSize );            }            try {//              checkRowCounts( ps.executeBatch(), ps );                ps.executeBatch();            }            catch (RuntimeException re) {                log.error( "Exception executing batch: ", re );                throw re;            }            finally {                batchSize = 0;            }        }    }

请注意,新方法不会检查执行准备好的语句的结果。请记住,进行此更改可能会以某种意外的方式(或可能不会)影响hibernate状态。

MacOS下对postgresql的简单管理操作

MacOS下对postgresql的简单管理操作

如何安装在另一篇blog中有述,这里不再赘述.本篇简单说一下安装完postgresql之后的一些管理和查询操作.

首先安装完postgresql之后需要初始化数据库:

initdb /usr/local/var/postgres -E utf8

如果你不初始化,那么db的路径就是上面的/usr/local/var/postgres(在MacOS 10.11上),数据库编码类型就是utf8.

我们可以设置开机启动postgresql服务:

ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

第一句将postgresql的配置plist文件做软连接至系统的对应路径下,第二句加载其中的一个plist文件.有可能你的postgresql不是通过homebrew安装的,你的plist文件名会略有不同,你只需要自行到/usr/local/opt/postgresql/中找到正确的文件名就可以了.

下面是启动和停止postgresql服务的指令:

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

pg_ctl -D /usr/local/var/postgres stop -s -m fast

这里有一点就是往往我们用上面的停止命令会等待一会,然后提示无法停止服务:

pg_ctl -D /usr/local/var/postgres stop -s -m fast

pg_ctl: server does not shut down

这时你可以先卸载掉之前自动加载的服务,然后再尝试停止即可:

launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

在搞定了postgresql服务的启动和停止后,我们可以尝试添加postgresql用户:

createuser your_username -P
#Enter password for new role:
#Enter it again:

然后我们可以用刚创建的用户建立一个数据库:

createdb database_name -O username -E UTF8 -e

上面创建了一个名为database_name的数据库,数据库的所有者为username用户,数据库的编码utf7,最后一个-e参数表示把数据库执行操作的命令显示出来.更多信息你可以通过 createdb –help查看.

在MacOS中管理postgresql的数据库有2种方法,一种是console,另一种是通过gui,看个人喜好了.

我们先来看看console方式,你可以用psql之类来连接数据库:

psql -U username -d database_name -h 122.0.0.1

进入之后你可以用\h显示sql的各种命令,用\?来显示psql客户端自身的一些命令,比如\d是显示数据库中的表,\c database_name是连接到指定数据库等等.

如果你不连接postgresql的情况下,也可以看到已创建数据库的列表:

psql -l

如果你从windows转行过来,那么可能跟倾向于使用gui的方式管理postgresql数据库,pgadmin同样不会让你失望,大家可以到官网下载:

https://www.pgadmin.org

安装之后,我们需要创建一个到某个数据库的连接,右键点击browser下的Servers,选择创建服务:

然后和控制台中用psql连接方法类似我们要设置数据库所在地址,名称,用户名等等内容,然后点击保存.

当多个客户端连接到同一个数据库时,我们还可以一揽全局,看到它们的状态:

我们可以进入数据库中的表中去看看表结构,以及表内容:

postgresql不像sqlite3,它需要服务器才能正常工作,这就是为什么你在rails中用rake创建数据库时sqlite3可以在db目录中看到生成的数据库而postgresql下啥也看不到的原因,所有创建的postgresql数据库都放在一起,你得通过上面讲述的方法来操作哦.

今天关于PostgreSQL 字符串处理与日期处理操作的分享就到这里,希望大家有所收获,若想了解更多关于C++字符串处理操作符重载、centos 7下源码编译安装php支持PostgreSQL postgresql手册 postgresql官网下载 postgresql视频教、Hibernate插入批处理与分区PostgreSQL、MacOS下对postgresql的简单管理操作等相关知识,可以在本站进行查询。

本文标签:

上一篇PostgreSQL字符切割:substring函数的用法说明(sql字符串切割函数)

下一篇postgresql 中的to_char()常用操作