GVKun编程网logo

在Postgresql中存储任意长度的字符串

4

本文将带您了解关于在Postgresql中存储任意长度的字符串的新内容,另外,我们还将为您提供关于angularjs–如何在postgresql中存储Oauth响应?、arrays–在postgres

本文将带您了解关于在Postgresql中存储任意长度的字符串的新内容,另外,我们还将为您提供关于angularjs – 如何在postgresql中存储Oauth响应?、arrays – 在postgresql中将列从字符串更改为字符串数组、PHP产生任意长度的字符串+数字随机数、Postgres SQL中的字符串字段长度的实用信息。

本文目录一览:

在Postgresql中存储任意长度的字符串

在Postgresql中存储任意长度的字符串

我有一个Spring应用程序,该应用程序使用最初由Spring Roo创建的JPA( Hibernate )。我需要存储任意长度的字符串,因此,我用
@Lob 注释了该字段:

public class MyEntity{    @NotNull    @Size(min = 2)    @Lob    private String message;    ...}

该应用程序可以在本地主机上正常运行,但是我已将其部署到外部服务器,并且出现了编码问题。因此,我想检查存储在PostgreSQL数据库中的数据是否正确。该应用程序自动创建/更新表。并且为该字段(消息)创建了一个类型为列的列:

文字NOT NULL

问题是在存储数据后,如果我浏览表或只是对该列进行SELECT,我看不到文本,而是看到数字。这些数字似乎是存储该信息的“某处”的标识符。

谁能告诉我这些标识符的确切含义,以及是否可以通过pgAdmin或select子句查看@Lob列中存储的数据?

有没有更好的方法在JPA中存储任意长度的字符串?

谢谢。

答案1

小编典典

我建议跳过“ @Lob”注释,并使用columnDefinition像这样:

@Column(columnDefinition="TEXT")

查看这是否有助于在浏览数据库本身时查看数据。

angularjs – 如何在postgresql中存储Oauth响应?

angularjs – 如何在postgresql中存储Oauth响应?

我正在为我的应用程序使用带有角度js(前端)和 postgresql(DB)的loopback框架(Node.js),在我的应用程序中,我使用了第三方登录“passport-google-oauth”.

在google身份验证之后,回调url会返回响应,如下所述.

EX:http://localhost:3000/gauthServer

文件:routes.js

app.get('/gauthServer',function(req,res) {
        console.log(req.cookies.currentUserEmail);
        console.log(req.cookies.currentUsername);
        console.log(req.cookies.currentUserId);
        console.log(req.cookies.accesstoken);
        //res.redirect("/");
    });

在上面的代码中,它正确地返回我在console.log()中打印的响应.

期待结果:我希望上述结果存储在postgresql DB中.

解决方法

答案很简单,但过于宽泛.

基本上你要做的是:

>使用slc loopback:datasource command创建数据源并选择postgresql连接器
>请执行npm i loopback-connector-postgresql –save
>使用slc loopback:model command创建包含上述属性的模型并将其连接到数据库.再次向导将帮助您这样做.
>使用模型上的create method将数据保存到数据库

您可能还会发现this answer很有用.

arrays – 在postgresql中将列从字符串更改为字符串数组

arrays – 在postgresql中将列从字符串更改为字符串数组

以下是名为“容器”的表的片段.
Column       |            Type             |            Modifiers            
--------------------+-----------------------------+---------------------------------
 id                 | uuid                        | not null
 name               | character varying(255)      | 
 products           | character varying           | default '{}'::character varying

如何将products列更改为“character varying []”,并将相应的修饰符更改为默认的“{}”:: character varying []?本质上,我想将字符串转换为字符串数组.请注意,products列对字符数没有限制.

alter table "containers" alter "products" type character varying[];

抛出以下错误

ERROR: column “products” cannot be cast to type character varying[]

Postgres中没有从varchar到varchar []的隐式转换.
您必须指明如何执行类型转换.
您应该在USING表达式子句中执行此操作(请参阅文档中的 ALTER TABLE).
在这种情况下,您必须删除并重新创建列的默认值,如文档中所述:

The USING option of SET DATA TYPE can actually specify any expression involving the old values of the row; that is,it can refer to other columns as well as the one being converted. This allows very general conversions to be done with the SET DATA TYPE Syntax. Because of this flexibility,the USING expression is not applied to the column’s default value (if any); the result might not be a constant expression as required for a default. This means that when there is no implicit or assignment cast from old to new type,SET DATA TYPE might fail to convert the default even though a USING clause is supplied. In such cases,drop the default with DROP DEFAULT,perform the ALTER TYPE,and then use SET DEFAULT to add a suitable new default.

alter table containers alter products drop default;
alter table containers alter products type text[] using array[products];
alter table containers alter products set default '{}';

这三个操作可以在一个语句中完成:

alter table containers 
    alter products drop default,alter products type text[] using array[products],alter products set default '{}';

PHP产生任意长度的字符串+数字随机数

PHP产生任意长度的字符串+数字随机数

可以自定义产生什么字符串以及多长

  1. function random($length)
  2. {
  3. $hash = '''';
  4. $chars = ''ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'';
  5. $max = strlen($chars) - 1;
  6. mt_srand((double)microtime() * 1000000);
  7. for($i = 0; $i {
  8. $hash .= $chars[mt_rand(0, $max)];
  9. }
  10. return $hash;
  11. }
复制代码

PHP


Postgres SQL中的字符串字段长度

Postgres SQL中的字符串字段长度

我在SQL数据库中有一个字符串,表示一个URL。有些网址很短,有些很长。我真的不知道这是我可能遇到的最长的URL,所以为了安全起见,我会采用较大的值,例如256或512。

当我定义最大字符串长度时(例如,使用SQLAlchemy):

url_field = Column(String(256))

即使实际的字符串较短,这是否也会占用每一行的空间(存储空间)?

我假设这与实现细节有关。我正在使用postgreSQL,但对sqlite和mysql也很感兴趣。

我们今天的关于在Postgresql中存储任意长度的字符串的分享已经告一段落,感谢您的关注,如果您想了解更多关于angularjs – 如何在postgresql中存储Oauth响应?、arrays – 在postgresql中将列从字符串更改为字符串数组、PHP产生任意长度的字符串+数字随机数、Postgres SQL中的字符串字段长度的相关信息,请在本站查询。

本文标签: