python - 无效请求错误 : VARCHAR requires a length on dialect mysql

标签 python mysql sql sqlalchemy

我正在尝试在运行 12.04 的 Ubuntu 机器上使用 mysql 创建一个远程数据库。

它有一个启用远程登录且没有密码的root用户。我已经启动了服务器。

输出

sudo netstat -tap | grep mysql

显示

tcp        0      0 localhost:mysql         *:*                     LISTEN      13246/mysqld

我使用创建了一个名为 nwtopology 的数据库(如上所述,root 还没有密码。)

 create database nwtopology
 grant all privileges on *.* to root@192.168.129.221
 FLUSH PRIVILEGES;

在同样运行 Ubuntu 12.04 的客户端机器上,我使用 python 脚本使用 sqlalchemy 连接到远程 mysql 数据库。

from pox.core import core
import pox.openflow.libopenflow_01 as of
import re
import datetime
import time
from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Date, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql.expression import exists

log = core.getLogger()
engine = create_engine('mysql://root@192.168.129.139/nwtopology', echo=False)
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()

class SourcetoPort(Base):
    """"""
    __tablename__ = 'source_to_port'
    id = Column(Integer, primary_key=True)
    port_no        = Column(Integer)
    src_address    = Column(String,index=True)

    #-----------------------------------------
    def __init__(self, src_address,port_no):
        """"""
        self.src_address = src_address
        self.port_no     = port_no


#create tables
Base.metadata.create_all(engine)

最后一行

Base.metadata.create_all(engine)

返回错误

 File "/usr/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 1699, in visit_string
    return self.visit_VARCHAR(type_)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/dialects/mysql/base.py", line 1654, in visit_VARCHAR
    self.dialect.name)
InvalidRequestError: VARCHAR requires a length on dialect mysql

这是什么意思?如何在 mysql 上设置 VARCHAR 长度?我对 sqlalchemy 和 mysql 很陌生。

最佳答案

将长度添加到您的 String 列:

src_address = Column(String(16), index=True)

关于python - 无效请求错误 : VARCHAR requires a length on dialect mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16472725/

相关文章:

python - 在Python中创建(空格分隔的)文本文件的列(或行)数组

python - 为什么关系中的重复项不违反 UniqueConstraint?

python - 如果值相等,则将连续的子列表分组在一起,然后提取具有属性 = 'R' 的值

mysql - 从一行链接到多个外部行

mysql - 表上的过程 UNION

python - 用来自同一行但不同列的值填充字典

php - 每次用户刷新页面时,从 URL 运行 PHP 脚本

mysql - 使用SQL计算时间序列中的时间间隔

php - 一张mysql表中的类别和子类别

以下场景的 MYSQL 查询