doudou3935 2017-02-12 10:43
浏览 29
已采纳

无效的sql语句 - 撇号? [重复]

This question already has an answer here:

i programm some PHP Scripts and i wrote this sql query (for example):

INSERT INTO \`table1\` (\`article\`, \`typ\`)
    VALUES(\`test\`, \`test2\`)

this query works.

my problem is that if i write the tablename and columns like this 'table1' i get an sql error: SQL Error(1064): You have an error in your SQL Syntax; check the manual that corresponds to your MariaDB server Version for the right syntax to use near ''article', 'typ') VALUES('test', 'test1')'

Does anybody know why i have to write it like this `table1` and why it doesnt work with normal --> ' ?

Server-Typ: MariaDB

Server-Version: 10.1.9-MariaDB - mariadb.org binary distribution

Server-Zeichensatz: UTF-8 Unicode (utf8)

</div>
  • 写回答

1条回答 默认 最新

  • douqiang7462 2017-02-12 10:47
    关注

    This is too long for a comment. The right way to write the code is:

    INSERT INTO table1(article, typ)
        VALUES ('test', 'test2')
    

    All of the identifiers (table and column names) are valid names. They do not need to be escaped. Hence, no backticks are necessary.

    You do need single quotes for quoted strings, not backticks. If the backticks are part of the name, you could just do:

    INSERT INTO table1(article, typ)
        VALUES ('`test`', '`test2`');
    

    But that seems highly unlikely.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部