如何将单行 R data.frame 插入 SQL 服务器数据库?
How to insert a single row R data.frame into a SQL Server database?
我正在努力将新的一行数据追加到现有的 SQL 服务器数据库中。
这些:
sqlQuery(con, paste("INSERT INTO df1 SELECT * FROM df2"))
sqlQuery(con, paste("INSERT INTO df1 SELECT * FROM ", df2))
sqlQuery(con, paste("INSERT INTO df1 SELECT * FROM ", sqldf("SELECT * FROM df2")))
全部产生错误:
[1] "42S02 -1305 [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot find the input table or query 'col_1_val'. Make sure it exists and that its name is spelled correctly."
[2] "[RODBC] ERROR: Could not SQLExecDirect 'INSERT INTO df1 SELECT * FROM col_1_val'"
col_1_val
是 df2
中的第一列值
走 sqlSave
路线:
sqlSave(con, df2, tablename = "df1", append = TRUE, rownames = FALSE, colnames = FALSE)
抛出内存分配错误:
Error in odbcUpdate(channel, query, mydata, coldata[m, ], test = test, : 'Calloc' could not allocate memory (1073741824 of 1 bytes)
尝试
INSERT INTO table1 ( column1 )
SELECT col1
FROM table2
如果您想要来自 table 2
的列
INSERT INTO table1 ( column1, column2, someInt, someVarChar )
SELECT table2.column1, table2.column2, 8, 'some string etc.'
FROM table2
假设 df1
和 df2
中列的名称和顺序相同,这应该有效:
query <-
paste0("INSERT INTO df1 ",
"(", paste0(names(df2), collapse = ", "), ") ",
"VALUES (",
paste0(rep("?", length(df2)), collapse = ", "), ")")
library(RODBCext)
sqlExecute(con,
query,
data = df2)
当使用 mtcars
作为 df1
时,编写的查询如下所示。
"INSERT INTO df1 (mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb) VALUES ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?"
这允许您生成将所有列名添加到查询中的语句,而无需手动声明它们。使用 sqlExecute
调用参数化查询。然后将问号绑定到您的数据,然后作为语句的一部分执行。
我正在努力将新的一行数据追加到现有的 SQL 服务器数据库中。
这些:
sqlQuery(con, paste("INSERT INTO df1 SELECT * FROM df2"))
sqlQuery(con, paste("INSERT INTO df1 SELECT * FROM ", df2))
sqlQuery(con, paste("INSERT INTO df1 SELECT * FROM ", sqldf("SELECT * FROM df2")))
全部产生错误:
[1] "42S02 -1305 [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot find the input table or query 'col_1_val'. Make sure it exists and that its name is spelled correctly."
[2] "[RODBC] ERROR: Could not SQLExecDirect 'INSERT INTO df1 SELECT * FROM col_1_val'"
col_1_val
是 df2
走 sqlSave
路线:
sqlSave(con, df2, tablename = "df1", append = TRUE, rownames = FALSE, colnames = FALSE)
抛出内存分配错误:
Error in odbcUpdate(channel, query, mydata, coldata[m, ], test = test, : 'Calloc' could not allocate memory (1073741824 of 1 bytes)
尝试
INSERT INTO table1 ( column1 )
SELECT col1
FROM table2
如果您想要来自 table 2
的列INSERT INTO table1 ( column1, column2, someInt, someVarChar )
SELECT table2.column1, table2.column2, 8, 'some string etc.'
FROM table2
假设 df1
和 df2
中列的名称和顺序相同,这应该有效:
query <-
paste0("INSERT INTO df1 ",
"(", paste0(names(df2), collapse = ", "), ") ",
"VALUES (",
paste0(rep("?", length(df2)), collapse = ", "), ")")
library(RODBCext)
sqlExecute(con,
query,
data = df2)
当使用 mtcars
作为 df1
时,编写的查询如下所示。
"INSERT INTO df1 (mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb) VALUES ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?"
这允许您生成将所有列名添加到查询中的语句,而无需手动声明它们。使用 sqlExecute
调用参数化查询。然后将问号绑定到您的数据,然后作为语句的一部分执行。