RMySQL dbWriteTable与field.types [英] RMySQL dbWriteTable with field.types
问题描述
我有一个名为df
的数据框,看起来像这样:
I have a data frame, called df
, that looks like this:
dte, val
2012-01-01, 23.2323
2012-01-02, 34.343
列上的类型是日期和数字.我想使用已经打开的连接将其写入MySQL数据库.连接正常,因为我可以查询数据库.我尝试运行以下命令:
The type on the columns is date and numeric. I would like to write this to a MySQL database using an already open connection. The connection works fine as I am able to query the db fine. I try to run the following:
dbWriteTable(con, name="table_name", value=df, field.types=list("date", "double(20,10)"))
这会产生错误:
函数(类,fdef,mtable)中的错误:无法找到函数"make.db.names",签名"MySQLConnection","NULL"的继承方法
Error in function (classes, fdef, mtable) : unable to find an inherited method for function "make.db.names", for signature "MySQLConnection", "NULL"
如果我未指定field.types,则运行:
If I do not specify a field.types, and run:
dbWriteTable(con, name="table_name", value=df)
我得到了错误:
mysqlExecStatement(conn,statement,...)中的错误:RS-DBI驱动程序:(无法运行语句:密钥规范中使用的BLOB/TEXT列'dte'没有密钥长度)
Error in mysqlExecStatement(conn, statement, ...) : RS-DBI driver: (could not run statement: BLOB/TEXT column 'dte' used in key specification without a key length)
任何人都可以对此有所了解吗?
Can anyone shed some light on this?
谢谢
推荐答案
玩了之后,我意识到了问题所在:field.types
必须是一个命名的 list 向量,而不仅仅是一个列表;此外,不得包含行名,否则我们需要三种字段类型.对于上面的示例,以下工作正常:
After playing with things, I realized what the problem is: field.types
must be a NAMED list vector, not simply a list; additionally the row names must not be included otherwise we need three field types. For the above example, the following works fine:
dbWriteTable(con, name="table_name", value=df,
field.types = c(dte="date", val="double(20,10)"), row.names=FALSE)
这篇关于RMySQL dbWriteTable与field.types的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!