我對 Postgresql 還很陌生,每天都在學習新東西。所以我有這個博客專案,我想在其中使用 PostgreSQL 作為資料庫。但我有點卡在拋出錯誤的最基本的插入查詢上。我有三個表posts,authors和categories。我猜我可以正確創建表,但是當我嘗試插入資料時,出現此錯誤:
error: syntax error at or near
length: 95,
severity: 'ERROR',
code: '42601',
detail: undefined,
hint: undefined,
position: '122',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'scan.l',
line: '1180',
routine: 'scanner_yyerror'
現在我不知道問題出在哪里,而且 Postgres 的錯誤也不是那么具體。
誰能告訴我哪里可能出錯?
以下是表格:
const createInitialTables = `
CREATE TABLE authors (
id UUID NOT NULL,
author_name VARCHAR(100) NOT NULL UNIQUE CHECK (author_name <> ''),
author_slug VARCHAR(100) NOT NULL UNIQUE CHECK (author_slug <> ''),
PRIMARY KEY (id)
);
CREATE TABLE posts (
id UUID NOT NULL,
post VARCHAR(500) NOT NULL CHECK (post<> ''),
post_slug VARCHAR(500) NOT NULL CHECK (post_slug <> ''),
author_id UUID NOT NULL,
PRIMARY KEY (id),
CONSTRAINT fk_authors FOREIGN KEY(author_id) REFERENCES authors(id)
);
CREATE TABLE categories (
id UUID NOT NULL,
category_name VARCHAR(50) NOT NULL CHECK (category_name <> ''),
category_slug VARCHAR(50) NOT NULL CHECK (category_slug <> ''),
post_id UUID NOT NULL,
PRIMARY KEY (id),
CONSTRAINT fk_posts FOREIGN KEY(post_id) REFERENCES posts(id)
);
`;
這是我進行插入查詢的異步函式:
const insertAuthor = async() => {
try {
const data = await fs.readFile( path.join( __dirname '../../data/data.json' ) );
const parsedData = JSON.parse( data.toString() );
const authorID = short.generate();
const authorName = parsedData[ 0 ].author;
const authorSlug = slugify( parsedData[ 0 ].author, {
strict: true,
lower: true
} );
const insertData = `
INSERT INTO authors (id, author_name, author_slug)
VALUES
(${authorID}, ${authorName}, ${authorSlug});
`;
await pool.query( insertData );
console.log( 'Data inserted successfully!' );
} catch ( e ) {
console.log( e );
}
};
insertAuthor();
更新 - - - - - - - - - - - - - - - - - - -
這是 Postgres 日志檔案的樣子:
2021-10-18 01:23:16.885 06 [5964] ERROR: syntax error at or near "Paton" at character 122
2021-10-18 01:23:16.885 06 [5964] STATEMENT:
INSERT INTO authors (id, author_name, author_slug)
VALUES
(an3cxZh8ZD3tdtqG4wuwPR, Alan Paton, alan-paton);
uj5u.com熱心網友回復:
在 INSERT 查詢中,在引號中添加字串值 -
const insertData = `
INSERT INTO authors (id, author_name, author_slug)
VALUES
('${authorID}', '${authorName}', '${authorSlug}');`; // added the quotes
uj5u.com熱心網友回復:
INSERT INTO authors (id, author_name, author_slug)
VALUES
(an3cxZh8ZD3tdtqG4wuwPR, Alan Paton, alan-paton);
您的字串值沒有被參考。應該是...
INSERT INTO authors (id, author_name, author_slug)
VALUES
('an3cxZh8ZD3tdtqG4wuwPR', 'Alan Paton', 'alan-paton');
您可以在查詢中添加引號,但不要。您撰寫的查詢不安全并且容易受到SQL 注入攻擊。不要使用字串連接將值插入到查詢中。
相反,使用引數。
const insertSQL = `
INSERT INTO authors (id, author_name, author_slug)
VALUES ($1, $2, $3);
`;
await pool.query( insertSQL, [authorID, authorName, authorSlug] );
Postgres 將為您處理報價。這更安全、更安全、更快。
請注意,這an3cxZh8ZD3tdtqG4wuwPR不是有效的 UUID。UUID 是一個 128 位整數,通常表示為 32 個字符的十六進制字串。
請注意,您可能還想使用自動遞增的主鍵,而不是自己生成 ID。對于 UUID 主鍵,加載uuid-ossp 包并使用其 UUID 函式作為默認值。
create extension "uuid-ossp";
create table authors (
id uuid primary key default uuid_generate_v4(),
-- There's no point in arbitrarily limiting the size of your text fields.
-- They will only use as much space as they need.
author_name text not null unique check (author_name <> ''),
author_slug text not null unique check (author_slug <> '')
);
insert into authors (author_name, author_slug)
values ('Alan Paton', 'alan-paton');
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/321567.html
標籤:sql 节点.js PostgreSQL 表达
