简体   繁体   English

创建 MySQL 表时出现编程错误

[英]Programming Error while creating MySQL Table

@I am translating input from a user into a MySQL command but it says i have the syntax wrong and Idk how I can fix it. @我正在将用户的输入转换为 MySQL 命令,但它说我的语法错误并且我知道如何修复它。 Essentially I made a program in python (not a problem with the python code) that has the ability to create MySQL tables and translates user input into a MySQL command to create the table, but I think i have worded the command wrong and need someone to help explain what I did wrong and how I can fix it or another way to do it (no new python packages just different MySQL command). Essentially I made a program in python (not a problem with the python code) that has the ability to create MySQL tables and translates user input into a MySQL command to create the table, but I think i have worded the command wrong and need someone to帮助解释我做错了什么以及如何修复它或其他方法(没有新的 python 包,只是不同的 MySQL 命令)。

#Error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''c1', 'c2') VALUES [('r1', 'r2')]' at line 1

#here is what i was collecting in my attempt to fix the bug
'''
ROW SYNTAX: [('r1', 'r2')]
COLUMN SYNTAX: ('c1', 'c2')
MySQL COMMAND: CREATE TABLE test ('c1', 'c2') VALUES [('r1', 'r2')]
'''
#just the values none of the ROW SYNTAX or COLUMN SYNTAX is actually put into the mysql command

I was expecting it to create the table with:我期待它创建表:

2 columns: c1 and c2 2 列:c1 和 c2

1 row with 2 values: r1 and r2 1 行有 2 个值:r1 和 r2

Yet as I have explained above... it doesn't.然而,正如我在上面所解释的......它没有。 I don't work with MySQL just python really so I'm probably missing something really easy or big that I just don't understand.我不使用 MySQL 只是 python 真的所以我可能错过了一些我不明白的非常简单或大的东西。

i've seen some example syntax similar to this but again I don't understand it.我已经看到了一些与此类似的示例语法,但我还是不明白。

UPDATE更新

'''
ROW SYNTAX: [('r1', 'r3'), ('r2', 'r4')]
COLUMN SYNTAX TCOLN: (c1, c2)
COLUMN SYNTAX COLNAMES: (c1 VCHAR(255), c2 VCHAR(255))

CREATE TABLE COMMAND: CREATE TABLE test (c1 VCHAR(255), c2 VCHAR(255));
INSERT TABLE COMMAND: INSERT INTO test (c1, c2) VALUES [('r1', 'r3'), ('r2', 'r4')];
'''
'''
Traceback (most recent call last):
  line 472, in cmd_query
    raw_as_string=raw_as_string)
_mysql_connector.MySQLInterfaceError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VCHAR(255), c2 VCHAR(255))' at line 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  line 558, in ctable
    cursor.execute(sqlCol) #slqCol is CREATE TABLE test (c1 VCHAR(255), c2 VCHAR(255));
  line 266, in execute
    raw_as_string=self._raw_as_string)
  line 475, in cmd_query
    sqlstate=exc.sqlstate)
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VCHAR(255), c2 VCHAR(255))' at line 1
'''

Thanks to your suggestions i am using VARCHAR now but i still seem to be running into close to the same problem感谢您的建议,我现在正在使用 VARCHAR,但我似乎仍然遇到了同样的问题

Thanks to @Martin for explaining what varchar is ect, it also turns out VCHAR is not the same as VARCHAR (thanks @robsiemb).感谢@Martin 解释什么是 varchar,事实证明 VCHAR 与 VARCHAR 不同(感谢@robsiemb)。 Now the table is creating BUT not putting the data values in?现在该表正在创建但没有将数据值放入? (this might be a python problem, hope not) (这可能是 python 问题,希望不是)

'''
INSERT TABLE COMMAND: INSERT INTO test (c1, c2) VALUES ('r1', 'r2'),('rr1', 'rr2');
'''

# LATEST UPDATE
New Error when inserting values
```python

#CREATE TABLE COMMAND: CREATE TABLE test (c1 VARCHAR(255), c2 VARCHAR(255));
#INSERT TABLE COMMAND: INSERT INTO test (c1, c2) VALUES (r1,r3),(r2,r4);

'''
Traceback (most recent call last):
  line 472, in cmd_query
    raw_as_string=raw_as_string)
_mysql_connector.MySQLInterfaceError: Unknown column 'r1' in 'field list'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  line 563, in ctable
    cursor.execute(sqlVal)
  line 266, in execute
    raw_as_string=self._raw_as_string)
  line 475, in cmd_query
    sqlstate=exc.sqlstate)
mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'r1' in 'field list'
'''

Python problem, thanks for answering my question! Python 问题,感谢您回答我的问题!

The problem with what you are doing is in this statement:您正在做的事情的问题在于此声明:

CREATE TABLE test ('c1', 'c2') VALUES [('r1', 'r2')]

This is not a valid CREATE TABLE statement.这不是有效的CREATE TABLE语句。

It looks like you're trying to create the table, and then insert some values into it.看起来您正在尝试创建表,然后在其中插入一些值。 Is that the case?是这样吗?

If so, this should be broken into two statements.如果是这样,这应该分为两个陈述。 First create the table:首先创建表:

CREATE TABLE test (c1 DATATYPE, c2 DATATYPE);

Replace DATATYPE with the type of the column you are creating (ie INT , VARCHAR(10) , etc.).DATATYPE替换为您正在创建的列的类型(即INTVARCHAR(10)等)。

Second, insert your values into the table:其次,将您的值插入表中:

INSERT INTO test (c1, c2) VALUES (r1, r2);

You should review the syntax and example for how to create a MySQL table.您应该查看有关如何创建 MySQL 表的语法示例

There are 2 issues with your CREATE TABLE statement.您的 CREATE TABLE 语句有 2 个问题。

  • You need to specify the types of the columns, something like:您需要指定列的类型,例如:
CREATE TABLE test (c1 INTEGER, c2 INTEGER);
  • You can't supply the values at the same time as create, but you can follow up with an insert statement :您不能在创建的同时提供值,但您可以使用插入语句跟进:
INSERT INTO test (c1, c2) VALUES (1, 3),(2, 4);

The above gives a table with two rows, like this:上面给出了一个有两行的表,如下所示:

mysql> SELECT * FROM test;
+------+------+
| c1   | c2   |
+------+------+
|    1 |    3 |
|    2 |    4 |
+------+------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM