R dbBuildTableDefinition mysql rmysql 写表时出错 [英] R dbBuildTableDefinition mysql rmysql error writing a table
问题描述
我正在研究安装在 Linux/Ubuntu 系统上的 R.我想将表写入数据库.为什么它不起作用?
I am working on R installed on Linux/Ubuntu system. I want to write a table to a database. Why doesn't it work?
我能够删除表格,也能够显示表格中的数据.简而言之,我的 dbGetQuery 和 dbRemoveTable 工作得很好.
I was able to drop tables and also able to display data present in a table. In short my dbGetQuery and dbRemoveTable work perfectly.
employee <- c('John Doe','Peter Gynn','Jolie Hope')
salary <- c(3, 2, 1)
employ<- data.frame(employee, salary)
> employ
employee salary
1 John Doe 3
2 Peter Gynn 2
3 Jolie Hope 1
> dbBuildTableDefinition(dbh,"ee",employ)
[1] "CREATE TABLE ee \n( row_names text,\n\temployee text,\n\tsalary double \n)"
推荐答案
可以直接通过dbWriteTable
函数写表.如果 conn
是你的连接对象,在这种情况下你应该尝试:
You can directly write a table through the dbWriteTable
function. If conn
is your connection object, in this case you should try:
dbWriteTable(conn,"ee",employ)
您给出的命令只是构建定义表的 MySQL 命令.没有在数据库中创建实际的表.如果要创建表,可以将dbBuildTableDefinition
的值插入到dbGetQuery
中.例如:
The command you gave just builds the MySQL command that defines the table. No actual table is created in the database. If you want to create the table, you can take the value of dbBuildTableDefinition
and plug it into dbGetQuery
. For instance:
tableDef<-dbBuildTableDefinition(dbh,"ee",employ)
dbGetQuery(conn,tableDef)
请注意,通过这种方式,您只是创建了表的定义,而没有在其中放置任何值.
Note that in this way you just create the definition of the table, without putting any value in it.
这篇关于R dbBuildTableDefinition mysql rmysql 写表时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!