delete in from DB from R - r

I am trying to delete data from R in mysql table with the data range
Actual statement :
dbSendQuery(conn,"delete from tab_name where date<='2018-07-29'")
tried with :
conn = dbConnect(MySQL(),user ="user_id",password = "pwd",dbname ="db_name",host ="localhost")
a="2018-07-29"
query <-"delete from table tab_name where date <= "
dbSendQuery(conn,paste0( query,"'",a,"'"))
The error I am getting is:
Error in .local(conn, statement, ...) :
could not run statement:
Any hints?

Related

Insert R dataframe into SQL (RODBC) - error table not found

I would like to drop my whole dataframe from R preferably using RODBC with sqlSave statement (not sqlQuery). Here is my sample code.
library(RODBC)
myconn <- odbcDriverConnect("some connection string")
mydf <- data.frame(col_1 = c(1,2,3), col_2 = c(2,3,4))
sqlSave(myconn, mydf, tablename = '[some_db].[some_schema].[my_table]', append = F, rownames = F, verbose=TRUE)
odbcClose(myconn)
After I execute it, I get back error message:
Error in sqlColumns(channel, tablename) :
‘my_table’: table not found on channel
When I check in SQL Server, an empty table is present.
If I run the same code again, I get error message:
Error in sqlSave(myconn, mydf, tablename = "[some_db].[some_schema].[my_table]", :
42S01 2714 [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]There is already an object named 'my_table' in the database.
[RODBC] ERROR: Could not SQLExecDirect 'CREATE TABLE [some_db].[some_schema].[my_table] ("col_1" float, "col_2" float)'
Any suggestions on how to troubleshoot?
UPDATE
In SSMS I can run the following commands successfully:
CREATE TABLE [some_db].[some_schema].[my_table] (
test int
);
drop table [some_db].[some_schema].[my_table]
Here are details of connection string:
Driver=ODBC Driver 17 for SQL Server; Server=someserveraddress; Uid=user_login; Pwd=some_password
To avoid the error, you could specify the database in the connection string:
Driver=ODBC Driver 17 for SQL Server; Server = someserveraddress; database = some_db; Uid = user_login; Pwd = some_password
and avoid using brackets:
sqlSave(myconn, mydf, tablename = 'some_schema.my_table', append = F, rownames = F, verbose=TRUE)

Inserting data from R to SQL Server

I am trying to insert data from R into SQL Server, using JDBC with Append Table.
dbAppendTable`(conn = conn,
name = " test",
value = data,
append = TRUE)
But I am getting this warning and error message:
Warning message in if (is.na(v)) {:
“the condition has length > 1 and only the first element will be used”
Error in .verify . JDBC .result(r, "Unable to retrieve JDBC result set for ", : Unable to retrieve JDBC result set for INSERT INTO " d b o .test "
(The value is not set for the parameter number 2.).
How can I resolve this or some other solution for inserting data with RJDBC?

Write to Snowflake VARIANT column from R

I am trying to load data to snowflake using the following code, but getting an error.
con <- DBI::dbConnect(
drv = odbc::odbc(),
driver = "SnowflakeDSIIDriver",
server = "<>",
authenticator = 'externalbrowser',
warehouse = "<>",
database = "<>",
UID = "<>",
role = "<>"
)
DBI::dbAppendTable(con, name = DBI::Id(schema = "<>", table = "<>"), value = tmp[1:2,])
tmp was downloaded from Snowflake, the same table using RStudio:
```{sql connection=con, output.var = 'tmp'}
select top 10 *
FROM <>
```
The error seems to be stemming from a VARIANT column where I store a JSON string.
Error in new_result(connection#ptr, statement, immediate) :
nanodbc/nanodbc.cpp:1374: 22000: SQL compilation error:
Expression type does not match column data type, expecting VARIANT but got VARCHAR(2) for column FEATURES
I had this once and it was an invalid JSON (missing brackets somewhere). Probably this helps.

Query a Postgresql table using variable in R

I have a few tablea in my postgresql database with names similar to: "114114.KP". I would like to dynamically call the table from R using RPostgres package.
I know that the name of the table makes it difficult to process. But is there a way to do it pls?
What I did so far, with i = "114114.KP" :
if(dbExistsTable(con5, i) == TRUE){
ExistingPortfolios <- dbSendQuery(con3, paste("SELECT * from \"", i,"\""))
ExistingPortfolios <- fetch(ExistingPortfolios)
}
But this returns an error (which is normal):
> ExistingPortfolios <- dbSendQuery(con3, paste("SELECT * from \"", i,"\""))
Error in postgresqlExecStatement(conn, statement, ...) :
RS-DBI driver: (connection with pending rows, close resultSet before continuing)

Insert R data.frame data into HIVE DB

I have a data.frame with 1000-2000 records. I am using RJDBC to insert into HIVE and followed below steps but I am getting an error
library(RJDBC)
drv <- JDBC(driverClass = "org.apache.hive.jdbc.HiveDriver", classPath = list.files("C:/R
Install/hive_lib",pattern="jar$",full.names=T),identifier.quote="'")
conn <- dbConnect(drv, "servername", "username", "pwd")
dbWriteTable(conn, name = tablename, value = data.frame, row.names = FALSE)
Error:
Error in .local(conn, statement, ...) :
execute JDBC update query failed in dbSendUpdate (Error while compiling statement: FAILED: ParseException line 1:334 mismatched input 'PRECISION' expecting ) near 'DOUBLE' in create table statement)
What is the best way to insert R data frame records into HIVE?

Resources