PostgreSQL入门使用手册,pgsql登陆连接数据库

网站管理员 Published the article • 0 comments • 21 views • 2019-07-31 18:27 • 来自相关话题

PostgreSQL Getting Started manual, pgsql login to connect to the database.


### pgsl命令解释,查看命令
```bash
psql --help
```
pgsql我们用的命令不多,常用的就那么几个参数,常用的,连接,输入格式和输出格式。
`PostgreSQL`远程或者本地ip,一般可以不用填写,默认就是`localhost`或者`127.0.0.1`。

```
General options:
-c, --command=COMMAND run only single command (SQL or internal) and exit
-d, --dbname=DBNAME database name to connect to (default: "dev")

Connection options:
-h, --host=HOSTNAME database server host or socket directory (default: "local socket")
-p, --port=PORT database server port (default: "5432")
-U, --username=USERNAME database user name (default: "dev")
-w, --no-password never prompt for password
-W, --password force password prompt (should happen automatically)
```

### 连接数据库

```bash
psql -h 127.0.0.1 -Udev -p 3433 -l

# output
List of databases
Name | Owner | Encoding | Collation | Ctype | Access privileges
------------+------------+----------+-----------+-------+---------------------------
logs | 123 | UTF8 | C | C |
postgres | 123 | UTF8 | C | C |

```


### pgsql命令行的命令

```pgsql
party_logs=> help
You are using psql, the command-line interface to PostgreSQL.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
party_logs=> \?
```

`help`可以查看所有的命令,`\?`可以查看`pgsql commands`,`\h`可以查看`SQL commands`。

#### \h

```
party_logs=> \h
Available help:
```
![eNe00K.png](https://s2.ax1x.com/2019/07/31/eNe00K.png)

```pgsql
party_logs=> \dp
Access privileges
Schema | Name | Type | Access privileges | Column access privileges
--------+-----------------+----------+---------------------------+--------------------------
public | logs | table | |
public | logs_log_id_seq | sequence | |
(2 rows)
``` 查看全部
PostgreSQL Getting Started manual, pgsql login to connect to the database.


### pgsl命令解释,查看命令
```bash
psql --help
```
pgsql我们用的命令不多,常用的就那么几个参数,常用的,连接,输入格式和输出格式。
`PostgreSQL`远程或者本地ip,一般可以不用填写,默认就是`localhost`或者`127.0.0.1`。

```
General options:
-c, --command=COMMAND run only single command (SQL or internal) and exit
-d, --dbname=DBNAME database name to connect to (default: "dev")

Connection options:
-h, --host=HOSTNAME database server host or socket directory (default: "local socket")
-p, --port=PORT database server port (default: "5432")
-U, --username=USERNAME database user name (default: "dev")
-w, --no-password never prompt for password
-W, --password force password prompt (should happen automatically)
```

### 连接数据库

```bash
psql -h 127.0.0.1 -Udev -p 3433 -l

# output
List of databases
Name | Owner | Encoding | Collation | Ctype | Access privileges
------------+------------+----------+-----------+-------+---------------------------
logs | 123 | UTF8 | C | C |
postgres | 123 | UTF8 | C | C |

```


### pgsql命令行的命令

```pgsql
party_logs=> help
You are using psql, the command-line interface to PostgreSQL.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
party_logs=> \?
```

`help`可以查看所有的命令,`\?`可以查看`pgsql commands`,`\h`可以查看`SQL commands`。

#### \h

```
party_logs=> \h
Available help:
```
![eNe00K.png](https://s2.ax1x.com/2019/07/31/eNe00K.png)

```pgsql
party_logs=> \dp
Access privileges
Schema | Name | Type | Access privileges | Column access privileges
--------+-----------------+----------+---------------------------+--------------------------
public | logs | table | |
public | logs_log_id_seq | sequence | |
(2 rows)
```

MySQL5.7 supports a native JSON data type, and how to use it?

网站管理员 Published the article • 0 comments • 134 views • 2019-07-11 15:52 • 来自相关话题

mysql5.7已经支持JSON的数据结构,意味着我们可以更加方便的对JSON结构的数据进行增删改成

### 什么是JSON?

> JSON: JavaScript Object Notation(JavaScript 对象表示法)
> JSON 是存储和交换文本信息的语法。类似 XML。
> JSON 比 XML 更小、更快,更易解析。
> JSON 具有自我描述性,更易理解

### 建立表,添加JSON的数据结构字段col

建表qtest,然后添加col的字段,其数据类型为JSON,插入一条JSON字符串,然后发现添加不是json结构的数据
mysql会给我们报如下错误```ERROR 3140 (22032) at line 2: Invalid JSON text:```,表明我们插入的是非法JSON。

```json
{"mascot": "Our mascot is a dolphin named \"Sakila\"."}
```

```mysql
mysql> CREATE TABLE qtest (col JSON);
Query OK, 0 rows affected (0.20 sec)

mysql> INSERT INTO t1 VALUES('{"mascot": "Our mascot is a dolphin named \"Sakila\"."}');
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO t1 VALUES('[1, 2,');
ERROR 3140 (22032) at line 2: Invalid JSON text:
"Invalid value." at position 6 in value (or column) '[1, 2,'.
```

### 如何查询JSON的某个字段

1. 字段指向一个字符串,由```$.name```组成,美元符号+字段名称

```mysql
mysql> SELECT col->"$.mascot" FROM qtest;
+---------------------------------------------+
| col->"$.mascot" |
+---------------------------------------------+
| "Our mascot is a dolphin named \"Sakila\"." |
+---------------------------------------------+
1 row in set (0.00 sec)
```

2. 查询数组```$[0]```,美元符号+中括号及数组索引

```mysql
mysql> select *from sourcedev;
+----+------------------------------+-----------+
| id | category | tags |
+----+------------------------------+-----------+
| 1 | {"id": 1, "name": "sourcedev.cc"} | [1, 2, 3] |
+----+------------------------------+-----------+
1 row in set (0.00 sec)

mysql> select id, category->'$.id', tags->'$[0]' from sourcedev;
+----+------------------+--------------+
| id | category->'$.id' | tags->'$[0]' |
+----+------------------+--------------+
| 1 | 1 | 1 |
+----+------------------+--------------+
1 row in set (0.00 sec)
```

### mysql5.7 JSON相关函数

1. JSON_TYPE JSON类型,ARRAY、STRING
所有的Json type类型如下
```
BLOB
BIT
OPAQUE
DATETIME
TIME
DATE
BOOLEAN
ARRAY
OBJECT
STRING
INTEGER, DOUBLE
NULL
```

2. JSON_ARRAY ```SELECT JSON_ARRAY('a', 1, NOW());```

```mysql
mysql> SELECT JSON_ARRAY('a', 1, NOW());
+----------------------------------------+
| JSON_ARRAY('a', 1, NOW()) |
+----------------------------------------+
| ["a", 1, "2015-07-27 09:43:47.000000"] |
+----------------------------------------+
```

3. JSON_OBJECT
```mysql
mysql> SELECT JSON_OBJECT('key1', 1, 'key2', 'abc');
+---------------------------------------+
| JSON_OBJECT('key1', 1, 'key2', 'abc') |
+---------------------------------------+
| {"key1": 1, "key2": "abc"} |
+---------------------------------------+
```
4. 等等


### 引用

1. [百度百科 #https://baike.baidu.com/item/JSON/2462549](https://baike.baidu.com/item/JSON/2462549)
2. [mysql5.7官方文档 #https://dev.mysql.com/doc/refman/8.0/en/json.html](https://dev.mysql.com/doc/refman/8.0/en/json.html) 查看全部

mysql5.7已经支持JSON的数据结构,意味着我们可以更加方便的对JSON结构的数据进行增删改成

### 什么是JSON?

> JSON: JavaScript Object Notation(JavaScript 对象表示法)
> JSON 是存储和交换文本信息的语法。类似 XML。
> JSON 比 XML 更小、更快,更易解析。
> JSON 具有自我描述性,更易理解

### 建立表,添加JSON的数据结构字段col

建表qtest,然后添加col的字段,其数据类型为JSON,插入一条JSON字符串,然后发现添加不是json结构的数据
mysql会给我们报如下错误```ERROR 3140 (22032) at line 2: Invalid JSON text:```,表明我们插入的是非法JSON。

```json
{"mascot": "Our mascot is a dolphin named \"Sakila\"."}
```

```mysql
mysql> CREATE TABLE qtest (col JSON);
Query OK, 0 rows affected (0.20 sec)

mysql> INSERT INTO t1 VALUES('{"mascot": "Our mascot is a dolphin named \"Sakila\"."}');
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO t1 VALUES('[1, 2,');
ERROR 3140 (22032) at line 2: Invalid JSON text:
"Invalid value." at position 6 in value (or column) '[1, 2,'.
```

### 如何查询JSON的某个字段

1. 字段指向一个字符串,由```$.name```组成,美元符号+字段名称

```mysql
mysql> SELECT col->"$.mascot" FROM qtest;
+---------------------------------------------+
| col->"$.mascot" |
+---------------------------------------------+
| "Our mascot is a dolphin named \"Sakila\"." |
+---------------------------------------------+
1 row in set (0.00 sec)
```

2. 查询数组```$[0]```,美元符号+中括号及数组索引

```mysql
mysql> select *from sourcedev;
+----+------------------------------+-----------+
| id | category | tags |
+----+------------------------------+-----------+
| 1 | {"id": 1, "name": "sourcedev.cc"} | [1, 2, 3] |
+----+------------------------------+-----------+
1 row in set (0.00 sec)

mysql> select id, category->'$.id', tags->'$[0]' from sourcedev;
+----+------------------+--------------+
| id | category->'$.id' | tags->'$[0]' |
+----+------------------+--------------+
| 1 | 1 | 1 |
+----+------------------+--------------+
1 row in set (0.00 sec)
```

### mysql5.7 JSON相关函数

1. JSON_TYPE JSON类型,ARRAY、STRING
所有的Json type类型如下
```
BLOB
BIT
OPAQUE
DATETIME
TIME
DATE
BOOLEAN
ARRAY
OBJECT
STRING
INTEGER, DOUBLE
NULL
```

2. JSON_ARRAY ```SELECT JSON_ARRAY('a', 1, NOW());```

```mysql
mysql> SELECT JSON_ARRAY('a', 1, NOW());
+----------------------------------------+
| JSON_ARRAY('a', 1, NOW()) |
+----------------------------------------+
| ["a", 1, "2015-07-27 09:43:47.000000"] |
+----------------------------------------+
```

3. JSON_OBJECT
```mysql
mysql> SELECT JSON_OBJECT('key1', 1, 'key2', 'abc');
+---------------------------------------+
| JSON_OBJECT('key1', 1, 'key2', 'abc') |
+---------------------------------------+
| {"key1": 1, "key2": "abc"} |
+---------------------------------------+
```
4. 等等


### 引用

1. [百度百科 #https://baike.baidu.com/item/JSON/2462549](https://baike.baidu.com/item/JSON/2462549)
2. [mysql5.7官方文档 #https://dev.mysql.com/doc/refman/8.0/en/json.html](https://dev.mysql.com/doc/refman/8.0/en/json.html)


windows PE header数据结构分析

网站管理员 Published the article • 0 comments • 101 views • 2019-06-18 10:44 • 来自相关话题

如何获取PE(Protable Executable)格式的header,PE的结构在winnt.h中可以获得,该头文件里定义了大量的PE数据结构体,方便我们对可执行文件数据结构进行分析。
如果我们不知道这些结构,我们可能需要逆向工程的技术对其结构进行分析猜想,还是包含一定的工作量。

PEheader的定义如下:

WORD的数据类型是short的无符号整型,```e_magic```是DOS签名,```e_lfanew```是NT的偏移量。一般的PE文件e_magic为```0x5a4d```。


### WORD的定义
```
typedef unsigned short WORD;
```

### PE header

```
typedef struct _IMAGE_DOS_HEADER {
WORD e_magic;
WORD e_cblp;
WORD e_cp;
WORD e_crlc;
WORD e_cparhdr;
WORD e_minalloc;
WORD e_maxalloc;
WORD e_ss;
WORD e_sp;
WORD e_csum;
WORD e_ip;
WORD e_cs;
WORD e_lfarlc;
WORD e_ovno;
WORD e_res[4];
WORD e_oemid;
WORD e_oeminfo;
WORD e_res2[10];
LONG e_lfanew;
} IMAGE_DOS_HEADER,*PIMAGE_DOS_HEADER;
```

### 示例代码

```c
#include
#include
#include

int main() {
FILE *file;
IMAGE_DOS_HEADER header;
file = fopen("cmake-build-debug\\c_demo.exe", "r");
fread(&header, sizeof(header), 1, file);
fclose(file);

printf("doc: 0x%x\n", header.e_magic);
return 0;
}
``` 查看全部

如何获取PE(Protable Executable)格式的header,PE的结构在winnt.h中可以获得,该头文件里定义了大量的PE数据结构体,方便我们对可执行文件数据结构进行分析。
如果我们不知道这些结构,我们可能需要逆向工程的技术对其结构进行分析猜想,还是包含一定的工作量。

PEheader的定义如下:

WORD的数据类型是short的无符号整型,```e_magic```是DOS签名,```e_lfanew```是NT的偏移量。一般的PE文件e_magic为```0x5a4d```。


### WORD的定义
```
typedef unsigned short WORD;
```

### PE header

```
typedef struct _IMAGE_DOS_HEADER {
WORD e_magic;
WORD e_cblp;
WORD e_cp;
WORD e_crlc;
WORD e_cparhdr;
WORD e_minalloc;
WORD e_maxalloc;
WORD e_ss;
WORD e_sp;
WORD e_csum;
WORD e_ip;
WORD e_cs;
WORD e_lfarlc;
WORD e_ovno;
WORD e_res[4];
WORD e_oemid;
WORD e_oeminfo;
WORD e_res2[10];
LONG e_lfanew;
} IMAGE_DOS_HEADER,*PIMAGE_DOS_HEADER;
```

### 示例代码

```c
#include
#include
#include

int main() {
FILE *file;
IMAGE_DOS_HEADER header;
file = fopen("cmake-build-debug\\c_demo.exe", "r");
fread(&header, sizeof(header), 1, file);
fclose(file);

printf("doc: 0x%x\n", header.e_magic);
return 0;
}
```

BF brute force 蛮力算法

网站管理员 Published the article • 0 comments • 282 views • 2018-06-29 12:24 • 来自相关话题

## 优点

易于理解

## 缺点

效率低下

## 复杂度

时间复杂度O(m + n) ~ O(m * n)

## php代码实现

```php 查看全部


## 优点

易于理解

## 缺点

效率低下

## 复杂度

时间复杂度O(m + n) ~ O(m * n)

## php代码实现

```php

$a = 'Hello World';
$b = 'orld';

function bf_match($a, $b) {
$len = strlen($a);
$b_len = strlen($b);
$i = 0;
$j = 0;
while ($i < $len && $j < $b_len) {
if ($a[$i] == $b[$j]) {
$i++;
$j++;
} else {
$i = $i - $j + 1;
$j = 0;
}
}

if ($j >= $b_len) {
return $i - $b_len;
}
return -1;
}

var_dump(bf_match($a, $b));

```