我正在使用JavaScript和PostgreSQL,并且想知道代码和语法是否编写正确,因为在不同情况下会遇到奇怪的错误。
示例:
function putDbEvents({ description, creatorId, attendeeId }) {
return pool
.query(`INSERT INTO events (${description}, ${creatorId}), ${attendeeId}`)
.then(() => pool.end())
.catch(console.log);
}
我的问题:
INSERT INTO events ('description', creatorId, attendeeId) VALUES then (${}...
这是创建表的代码:
const createEventsRes = await pool.query(
"CREATE TABLE IF NOT EXISTS events (id serial PRIMARY KEY, description TEXT, creatorId INTEGER, attendeeId INTEGER)"
);
我想创建一个表,因此每次添加表时,它都应该自动向其中添加一个id,而您只需向其传递说明,id,id,第四个是可选的,因此在这种情况下不需要,并且想要查看如果语法正确。
答案 0 :(得分:1)
插入时需要命名列:
INSERT INTO events (description, creatorid, attendeeid)
values (${description}, ${creatorId}), ${attendeeId})
这是一个好习惯。当您省略列时(在这种情况下为id
),也是必需的。
答案 1 :(得分:0)
Nvm,我解决了。
function putDbEvents(description,creatorid,Attendeid){
回报池
.query(// Created by Bipin on 9/26/18.
// Copyright © 2018 profyl.org. All rights reserved.
//
import Foundation
import ObjectMapper
import Firebase
enum FirebaseDate {
case date(Date)
case serverTimestamp
var date: Date {
switch self {
case .date(let date):
return date
case .serverTimestamp:
return Date()
}
}
}
class FirebaseDateTransform: TransformType {
public typealias Object = FirebaseDate
public typealias JSON = Any
open func transformFromJSON(_ value: Any?) -> FirebaseDate? {
switch value {
case is [AnyHashable: Any]?:
return .serverTimestamp
default:
return nil
}
}
open func transformToJSON(_ value: FirebaseDate?) -> Any? {
switch value {
case .serverTimestamp?:
return ServerValue.timestamp()
default:
return nil
}
}
open func getFirebaseTimestamp()->String{
return String(transformFromJSON(ServerValue.timestamp())!.date.toMillis())
}
}
,[描述,creatorid,参与者ID])
.then(()=> pool.end())
.catch(console.log);
}