【问题标题】:MySqlDb throws Operand should contain 1 column(s) on insert ignore statementMySqlDb 在插入忽略语句中抛出操作数应包含 1 列
【发布时间】:2014-04-03 19:20:42
【问题描述】:

在查看堆栈交换提供的一些 websocket 方法时,我想将一些数据点保存到 MySQL 数据库中。但是,当我尝试运行 executemany 命令时,出现以下错误:

_mysql_exceptions.OperationalError: (1241, 'Operand should contain 1 column(s)')

在查看 SO 时,我发现了许多此错误的示例,但他们已经处理了删除 SELECT 语句上的括号。我没有使用SELECT。我正在尝试INSERT

我的代码的一个简短的包含示例如下所示:

import MySQLdb as mdb
db = mdb.connect(host='localhost',user='myuser',db='qs',passwd='mypass',use_unicode=True,charset='utf8')
cur = db.cursor()
db_qry = """INSERT IGNORE INTO questions (id, site_base, title, body_sum, tags, last_act_dt, url, owner_url, owner, api_site) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"""


parms = [(u'mathematica.stackexchange.com', 
43248, 
u'How to plot “Normalized distance” in this problem', 
u"Problem: there are two particles whose equationsof motion both satisfy -n Abs[x[t]]^n/x[t] == x''[t]. But their initial conditions are different: one is x'[0] == 0, x[0] == 2;another is x'[0] == 0, ...", 
[u'plotting', u'equation-solving', u'differential-equations', u'numerical-integration', u'notebooks'],
1393801095,
u'http://mathematica.stackexchange.com/questions/43248/how-to-plot-normalized-distance-in-this-problem',
u'http://mathematica.stackexchange.com/users/12706/lawerance', u'Lawerance', u'mathematica')]

cur.executemany(db_qry, parms)
cur.commit()

我使用executemany 不正确吗?或者在传递给executemany 之前缺少我需要清理的parms 列表的另一个方面?

【问题讨论】:

    标签: python mysql mysql-python executemany


    【解决方案1】:

    问题在于数据进入tags 列。它试图传递一个列表而不是一个字符串。

    对于我原始问题中的示例,我使用此代码将其转换为字符串。

    ','.join([u'plotting', u'equation-solving', u'differential-equations', u'numerical-integration', u'notebooks'])
    

    还应该注意,我搞砸了我的提交行。应该是db.commit() 而不是cur.commit()

    【讨论】:

      【解决方案2】:

      我在尝试通过pandas.DataFrame.to_sql 将包含一列列表的 df 保存到 mysql 时遇到了这个问题,这可以自动化其中的一些过程。我通常不使用join,而是使用json.dumps() 将列表转换为编码字符串。 json 包还可以使用json.loads() 轻松将其全部加载回其原始格式。

      在原始示例的情况下,我会将parms 转换为数据框

      parms = pd.DataFrame(parms)
      parms
                                     0      1  \
      0  mathematica.stackexchange.com  43248
      
                                                         2  \
      0  How to plot “Normalized distance” ...
      
                                                         3  \
      0  Problem: there are two particles whose equatio...
      
                                                         4           5  \
      0  [plotting, equation-solving, differential-equa...  1393801095
      
                                                         6  \
      0  http://mathematica.stackexchange.com/questions...
      
                                                         7          8            9
      0  http://mathematica.stackexchange.com/users/127...  Lawerance  mathematica
      

      然后将列表的列转换为json字符串:

      parms[4] = parms[4].apply(json.dumps)
      parms
      
      0  mathematica.stackexchange.com  43248
      
                                                         2  \
      0  How to plot “Normalized distance” ...
      
                                                         3  \
      0  Problem: there are two particles whose equatio...
      
                                                         4           5  \
      0  ["plotting", "equation-solving", "differential...  1393801095
      
                                                         6  \
      0  http://mathematica.stackexchange.com/questions...
      
                                                         7          8            9
      0  http://mathematica.stackexchange.com/users/127...  Lawerance  mathematica
      

      启动 MySQL 连接(使用上面提供的详细信息):

      import sqlalchemy
      call = 'mysql+mysqldb://myuser:mypass@localhost:3306/qs' 
      engine = sqlalchemy.create_engine(call)
      

      然后使用内置的 pandas 函数转储它:

      parms.to_sql('questions', engine, if_exists='append', chunksize=5000)
      

      【讨论】:

        猜你喜欢
        • 2023-03-29
        • 2018-12-16
        • 1970-01-01
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 2019-02-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多