当我运行以下代码时:
with sqlite3.connect("example.db") as con:
c=con.cursor()
c.execute("CREATE TABLE test (id,name) ")一切都很好。
但是当我使用mysql.connector时
import mysql.connector as mariadb
with mariadb.connect(user='root', password='root', database='publications') as con:
c = con.cursor()
c.execute("CREATE TABLE test (id INT,name VARCHAR(45))")我得到了这个错误:
File "<...>", line 4, in <module>
with mariadb.connect(user='root', password='root', database='publications') as con:
AttributeError: __enter__有人能告诉我这是什么原因吗?正如我在"PEP 343 -- The "with" Statement"中读到的那样,要使用带有with语句的对象,您需要实现__enter__()和__exit__()-method。因此,它似乎是在sqlite3中实现的,而不是在mysql.connection中实现的。但这是什么原因呢?有没有更深层次的原因,或者Mysql只是没有实现它?
谢谢您:)
发布于 2020-04-15 21:41:26
在SQLite中,列没有类型(默认类型)。在MySQL中,列需要有一个显式类型。添加类型和已解决的问题。
例如,SQL语句可能如下所示:
CREATE TABLE test (id int, name varchar(50))https://stackoverflow.com/questions/61230167
复制相似问题