PostgreSQL中连字符( - )的转义序列是什么

时间:2010-10-15 13:40:54

标签: sql postgresql escaping

我正在尝试将数据库重命名为带有连字符( - )的名称。

ALTER DATABASE one RENAME TO one-two;

psql返回错误:

ERROR:  syntax error at or near "-"

我应该使用什么作为“ - ”字符的转义序列或者上述方法是什么?

注意: 我试过'\ - '并且没有用。

感谢。

3 个答案:

答案 0 :(得分:60)

双引号应该这样做。但是,您必须始终在引用数据库的任何地方使用带引号的标识符。

ALTER DATABASE one RENAME TO "one-two";

答案 1 :(得分:2)

像这样混合双引号和单引号:

MembershipFormSet = forms.inlineformset_factory(Group, Membership, fields=('person', 'date_joined', 'invite_reason'))

class CreateGroupView(CreateView):
    model = Group
    ...

    def get(self, request, *args, **kwargs):
        self.object = None
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        membership_formset = MembershipFormSet()
        return self.render_to_response(
            self.get_context_data(
                form=form,
                membership_formset=membership_formset
            )
        )

    def post(self, request, *args, **kwargs):
        self.object = None
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        membership_formset = MembershipFormSet(self.request.POST)
        if (form.is_valid() and membership_formset.is_valid()):
            return self.form_valid(form, membership_formset)
        else:
            return self.form_invalid(form, membership_formset)

    def form_valid(self, form, membership_formset):
        self.object = form.save(commit=False)
        self.object.author = self.request.user
        self.object.save()
        membership_formset.instance = self.object
        membership_formset.save()
        return HttpResponseRedirect(self.get_success_url())

    def form_invalid(self, form, membership_formset):
        return self.render_to_response(
            self.get_context_data(
                form=form,
                membership_formset=membership_formset
            )
        )

答案 2 :(得分:0)

psql -d -c“如果不存在,则创建扩展名”'“ uuid-ossp”'“;”