How to update a SQL table using RJDBC in R - r

I already have a SQL table and I want to update the table on a daily basis. I want to overwrite the table everyday.
If I use dbWriteTable() command it is giving me an error "ORA-00955: name is already used by an existing object".
How can I get rid of this error?
Can I use dbSendUpdate() here? If yes,how?

Related

Update JSONB in Postgres using R

as the title states I am trying to update a jsonb field from inside R. I have several changes to apply. The original dataset is created by a third party application but needs to be corrected for several rows.
The following statement is working fine as a database statement:
UPDATE histories set meta=jsonb_set(meta,'{product}','"55-AB"') WHERE id = 17983;
Now, I need to update the "product" field for several different ids.
Assume the following dataframe as an example:
df<-data.frame(product=c("55-AB","567-C","UTG-98"),
id=c(17983,54388,20000))
Usually I would use sql_glue from the glue package but I run out of quotes generating the above queries dynamically.
sql_glue("UPDATE histories set meta=jsonb_set(meta,'{product}','"{`df$product`}"') WHERE id = {`df$id`};")
Error: unexpected '{' in "sql_glue("UPDATE histories set meta=jsonb_set(meta,'{pesticide}','"{"
I am running into problems with the quotation. Any idea how to get around this?

Whats the correct syntax for on conflict do update sqlyte?

I am trying to insert clients on a table(importing them from a csv), the email column needs to be unique(in the csv the a client can appear more than one time, the last instance has the correct info) and the ids are created with an autoincrementing value (this is why i cant use select or replace), im trying to use the syntax found in the sqlite guide(and in every question about ON CONFLICT DO UPDATE found here) but sqlite throws a
SQL logic error near "ON" :syntax error
the query goes like this(using VB)
sqlQuery = "INSERT INTO clients(name1,name2,address1,address2,plz,city,country,phoneNumber1,phoneNumber2,cellPhoneNumber,fax,email) VALUES (?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT(email) DO UPDATE SET name1=excluded.name1,name2=excluded.name2,address1=excluded.address1,address2=excluded.address2,plz=excluded.plz,city=excluded.city,country=excluded.country,phoneNumber1=excluded.phoneNumber1,phoneNumber2=excluded.phoneNumber2,cellPhoneNumber=excluded.cellPhoneNumber,fax=excluded.fax,email=excluded.email;

Unable to delete a MySQL InnoDB table

I am using MySQL 5.1.56
I have a DB with about 70 tables and I have an issue with a particular corrupted table e.g. Table_X
When I try to access the table
mysql> select * from Table_x;
ERROR 1105 (HY000): Failed to read from the .par file
I am unable to add partitions to the table.
When I try to drop the table I see the below errors.
mysql> drop table Table_X;
ERROR 1051 (42S02): Unknown table 'Table_X'
The create query gives the error: ERROR 1050 (42S01): Table 'Table_X' already exists.
In my DB files locations, I can see the corresponding Table_X.frm, Table_X.ibd and Table_X.par files. But in addition, I also see a file '#sql-Table_X.frm' in the location.
When I check the 'Tables' table in the information_Schema DB, the Engine value is NULL for this particular table, where as it should have been InnoDb.
The table seems to be corrupted somehow.
I tried the Flush-tables command,but that did not help as well. I am still unable to drop and recreate the table.
I do not wish to take a backup of this particulate table, but I need to preserve other tables of the database. Is there any way, I can just recreate this individual table without having to restore the entire Database.
You can try to repair the table with the following statement :
REPAIR TABLE Table_X;
Take care, such query can induce a loss of data, but since you are trying to DROP the table, I guess this is not a matter for you.
You could also try to use the FRM file as a source for your table with
REPAIR TABLE Table_X USE_FRM;
PS : Can you give us the result of the following query ?
CHECK TABLE Table_X;
References :
https://www.tutorialspoint.com/mysql/mysql_repair_table_statement.htm
https://www.tutorialspoint.com/mysql/mysql_check_table_statement.htm

ORA-00900 error in dbSendStatement() of 'RJDBC'

I am trying to upload data from R to database, I used package 'RJDBC' to connect R to Oracle. The connection is set up and I am able to load data from Oracle to R. When I tried to insert data into a table in Oracle using dbSendStatement():
sqlQuery_uploadResult<-function(A,B,C,D,E,F,G,H,I){
sprintf("INSERT INTO DEMAND_FCAST (A,B,C,D,E,F,G,H,I) VALUES (TO_DATE('%s','DD-MON-YY hh24:mi:ss'),%1.4f,%5.2f,%2.0f,%1.4f,%5.2f,%2.0f,%2.0f)",A,B,C,D,E,F,G,H,I)
}
dbSendStatement(conn,sqlQuery_uploadResult(A,B,C,D,E,F,G,H,I))
I got an error message:
Error in .verify.JDBC.result(md, "Unable to retrieve JDBC result set meta data for ", :
Unable to retrieve JDBC result set meta data for INSERT INTO DEMAND_FCAST (A,B,C,D,E,F,G,H,I) VALUES (TO_DATE('17-Oct-16 13:35:45','DD-MON-YY hh24:mi:ss'),0.1160,700,36,0.4037,965,35, 1) in dbSendQuery (ORA-00900: invalid SQL statement
)
However, I checked the table in Oracle, the record I tried to insert is in the table, is there anyone has any clue why does this error turn out and how to deal with this?
I was having the same issue. I tried dbExecute() and dbSendQuery() and got a similar error. A colleague suggested that I use dbSendUpdate() and it executed without error.

Having trouble specifying a schema name from MonetDB.R

I am trying to connect to a table that is not in the sys schema. The code below works if sys.tablea exists.
conn <- dbConnect(dbDriver("MonetDB"), "monetdb://localhost/demo")
frame <- monet.frame(conn,"tablea")
If I define tablea in a different schema, e.g. xyz.tablea, then I get the error message
Server says 'SELECT: no such table 'tablea'' [#NA]
The account used to connect has rights to the table.
In a related question, is it possible to use camel-case from MonetDB.R? When I change the table name to TableA, the server again responds with
Server says 'SELECT: no such table 'tablea'' [#NA]
where the table name is all lower-case.
Using tables in other schemata is not possible with the current constructor of monet.frame. However, you can work around the issue as follows:
frame <- monet.frame(conn,"select * from xyz.tablea")
This trick also works with CamelCased table names.
For the next version, I am planning to fix the issue.

Resources