python - 使用 %s 运算符在 Python 中创建 MySQL 表列
问题描述
类似于使用 %s 运算符在 Python 中创建 MySQL 数据库中的Brunonono (即使使用相同的包),我正在尝试使用 Python 中的 %s 运算符将列从 excel 表添加到 mysql 表。错误是一样的:
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 '%s %s)' at line 1
代码如下
mydb=mysql.connector.connect(host="localhost",user="root")
#init cursor
mycursor=mydb.cursor()
column_title="ID"
cell_type="INT(10)"
mycursor.execute("ALTER TABLE Test ADD (%s %s)"),(column_title, cell_type)
可悲的是,我不知道如何应用上面帖子中提供的解决方案,其中
mycursor.execute("CREATE DATABASE (%s)", (db_name))
被替换为
create_statement = "CREATE DATABASE {:s}".format(db_name)
mycursor.execute(create_statement)
解决方案
mycursor.execute("ALTER TABLE Test ADD (%s %s)"),(column_title, cell_type)
是你目前在做什么
相反,
mycursor.execute("ALTER TABLE Test ADD (%s %s)" % (column_title, cell_type))
除非我不正确地了解 MySQL 语法,否则这会起作用
推荐阅读
- drools - Drools - 来自字符串的规则在内存中创建
- python - 如何解决读取字典元素的问题,无论构成其键的夫妇的顺序如何
- javascript - 多个作业的 Cronjob 超时
- java - OpenWeatherMap REST API 总是返回 HTTP 301
- python - 我在这段代码中得到一个 IndexError 你能帮我吗?
- google-app-engine - 在 App Engine Standard 中为自定义域启用 SSL
- java - 检查对象是否相等但仅比较两个对象中定义的键
- pandas - 根据 pandas 上的 2 个值创建新的日期时间值
- azure-application-insights - Microsoft 扩展框架向记录器添加自定义维度而不是每次调用?
- typescript - 在打字稿中使用泛型时收到错误消息