【问题标题】:how to insert a numpy array having only one element to mysql db?如何将一个只有一个元素的numpy数组插入mysql db?
【发布时间】:2017-03-13 09:52:55
【问题描述】:

我有一个 python 程序,它有一个带有单个元素的 numpy 数组,比如 a=['this is the element'] 我需要将此值插入我的数据库(MySQL)。

我使用了execute(),但它显​​示一个属性错误

我从表中选择数据的 sql 部分是这样的:

import pandas as pd
import mysql.connector
mysql_cn=mysql.connector.connect(user='root', password='',
                              host='127.0.0.1',port='3388',
                              database='proj')

X = pd.read_sql('select stamp from test;', con=mysql_cn)
mysql_cn.close()

既然 mysql_cn.execute() 不起作用,那么必须做什么? 我也试过 pd.to_sql 但它也不起作用

这是execute()的错误

Traceback (most recent call last):
  File "C:\Python34\data\test.py", line 132, in <module>
    haa=mysql_cn.execute('select stamp from test;')// this is my simple query not my actual query
AttributeError: 'MySQLConnection' object has no attribute 'execute'

【问题讨论】:

  • 错误是什么?
  • 当我使用 mysql_cn.execute() 我得到一个属性错误,说执行不是一个属性
  • 因为执行需要查询作为参数。
  • 实际上我在评论时跳过了查询..我尝试了查询..但即使我有一个查询它的显示属性错误

标签: python mysql arrays numpy


【解决方案1】:

你需要一个光标。你必须从你的mysql_cn创建一个游标,然后你可以在游标上调用execute。

cursor = mysql_cn.cursor()
# Now call cursor.execute ....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    相关资源
    最近更新 更多