RODBC sqlSave no quoting some strings in insert query - sql

I'm trying to use the sqlSave function to insert the rows of a data.frame into a table in a Microsoft Access database. First of all, if I don't specify fast = FALSE I get a fatal error and R and RStudio crash completely with no error messages given. Changing fast to FALSE fixes this, but I get the error
unable to append to table ‘archiverapp_fcsfiles’
sqlSave conn matched archiverapp_fcsfiles TRUE TRUE FALSE TRUE FALSE
By setting verbose = TRUE, I can see what it is trying to do:
Query: INSERT INTO "archiverapp_fcsfiles" ( "experiment",
"fcsfilename",
"celltype",
"donor",
"time",
"readout",
"treat1",
"treat1conc",
"treat2",
"treat2conc",
"treat3",
"treat3conc" )
VALUES ( 2, 'Specimen_001_F10_F10_JUN s63_60 minutes.fcs', 'Donor 1',
'THP1', TNFa, '50', '60', NULL, NULL, NULL, NULL, JUN s63 )
sqlwrite returned
42000 -3100 [Microsoft][ODBC Microsoft Access Driver]
Syntax error (missing operator) in query expression 'JUN s63'.
[RODBC] ERROR: Could not SQLExecDirect
'INSERT INTO "archiverapp_fcsfiles" ( "experiment", "fcsfilename", "celltype",
"donor", "time", "readout", "treat1", "treat1conc", "treat2", "treat2conc",
"treat3", "treat3conc" )
VALUES ( 2, 'Specimen_001_F10_F10_JUN s63_60 minutes.fcs', 'Donor 1',
'THP1', TNFa, '50', '60', NULL, NULL, NULL, NULL, JUN s63 )'
As you can see, it is not adding quotes around the strings 'TNFa' and 'JUN s63'.
I'm not having problems like this with any of my other queries, but then again I'm not using fast = FALSE with them so it's probably using a different method.
Any idea on how to avoid this?

Related

Hive regexp_replace Unable to execute method public

Getting the following error when running the hive query in an airflow job. However the query itself works in hive when executing in the interactive environment.
The data is the 'scans' field in virus total https://developers.virustotal.com/v2.0/reference/url-report and it got stored as json string when saving to the table
Here is my query on extracting the scans data from the table and parsing the json string and split each av engine into a separate row of itself
SELECT
av_engine, output_map['detected'], output_map['result']
FROM (
SELECT
av_engine,
str_to_map(regexp_replace(engine_result, '\\}', ''),',', ':') AS output_map
FROM (
SELECT
str_to_map(regexp_replace(regexp_replace(scans, '\"', ''), '\\{',''),'\\},', ':') AS key_val_map
FROM virus_total_scan_results
) AS S
LATERAL VIEW EXPLODE(key_val_map) temp AS av_engine, engine_result
)
And it error out with the following error message:
Caused by: org.apache.hadoop.hive.ql.metadata.HiveException: Unable to execute method public
org.apache.hadoop.io.Text org.apache.hadoop.hive.ql.udf.UDFRegExpReplace.evaluate(
org.apache.hadoop.io.Text,
org.apache.hadoop.io.Text,
org.apache.hadoop.io.Text
) with arguments
{
Bkav: {detected: false, version: 1.3.0.9899, result: null, update: 20210218},
DrWeb: {detected: false, version: 7.0.49.9080, result: null, update: 20210218},
McAfee: {detected: false, version: 6.0.6.653, result: null, update: 20210218},
...
Qihoo-360: {detected: false, version: 1.0.0.1120, result: null, update: 20210218}
},
{,}:Illegal repetition
Any idea on what the issue could be?

Vague syntax error on an SQL query in Maria-DB

INSERT INTO "order" SET `user_id` = 5, `week_id` = 1, `box_settings_id` = '1', `delivery_day` = 'Thursday', `delivery_time` = 'Morning 6am - 9am', `notes` = '', `recipes` = '70, 71, 72, 74';
What is wrong with my SQL query? I'm getting this error in PHPMyAdmin:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '"order" SET `user_id` = 5, `week_id` = 1, `box_settings_id` = '1', `delivery_day' at line 1
While the SQL debugger isn't showing any error.
Issue likely is due to use of double quotes which MySQL and MariaDB diverge from the ANSI SQL standard. Instead of double quotes (synonymous to single quotes for string literals), MariaDB and MySQL would use backticks. With that said, you can adjust SQL mode to non-default ANSI or ANSI_QUOTES to support double quotes for identifiers:
ANSI_QUOTES
Changes " to be treated as `, the identifier quote character. This may break old MariaDB applications which assume that " is used as a string quote character.
Therefore, simply use backticks on order table like you do for columns. Generally, any identifier can use backticks.
INSERT INTO `order`
SET `user_id` = 5,
`week_id` = 1,
`box_settings_id` = '1',
`delivery_day` = 'Thursday',
`delivery_time` = 'Morning 6am - 9am',
`notes` = '',
`recipes` = '70, 71, 72, 74';

Strange error with ADO SQL string versus DoCmd.Execute

I'm trying to pass the following SQL string with ADO:
UPDATE ((JLE_Parcelas INNER JOIN ST_JLE_Municipios ON JLE_Parcelas.Cod_Parcelas_Municipio = ST_JLE_Municipios.Id_Municipios_Municipio) INNER JOIN ST_JLE_Provincias ON ST_JLE_Municipios.Cod_Municipios_Provincia = ST_JLE_Provincias.Id_Provincias_Provincia) INNER JOIN ST_JLE_Paises ON ST_JLE_Provincias.Cod_Provincias_Pais = ST_JLE_Paises.Id_Paises_Pais SET Parcelas_Alta_FechaFinal= NULL, Parcelas_Alta_FechaInicial= NULL, Parcelas_SIGPACNumPoligono= 20, Parcelas_SIGPACSuperficie= 79.9244, Parcelas_Observaciones='DDDDDEE', Parcelas_SIGPACNumParcela= 1, Parcelas_Contrato_FechaFinal= NULL, Cod_Parcelas_TipoProteccion= 1, Cod_Parcelas_TipoTenencia= 1, Cod_Parcelas_TipoRiego= 2, Cod_Parcelas_Agricultor= 4, Cod_Parcelas_Municipio= 195 WHERE Id_Parcelas_Parcela=7
but ADO returns Syntax error -2147217900 (systax error near '('.)
Notice I'm working with SQL Server database.
If I send the same string to Currentdb.execute, error dissapears and register is correctly saved, so I have two questions:
Can you tell me where is error in ADO?. I tried to find it, but it's impossible for me
is it better to use ado versus DoCmd, or vice versa?
Thanks in advance

saved data frame is not shown correctly in sql server

I have data frame named distTest which have columns with UTF-8 format. I want to save the distTest as table in my sql database. My code is as follows;
library(RODBC)
load("distTest.RData")
Sys.setlocale("LC_CTYPE", "persian")
dbhandle <- odbcDriverConnect('driver={SQL Server};server=****;database=TestDB;
trusted_connection=true',DBMSencoding="UTF-8" )
Encoding(distTest$regsub)<-"UTF-8"
Encoding(distTest$subgroup)<-"UTF-8"
sqlSave(dbhandle,distTest,
tablename = "DistBars", verbose = T, rownames = FALSE, append = TRUE)
I considered DBMSencoding for my connection and encodings Encoding(distTest$regsub)<-"UTF-8"
Encoding(distTest$subgroup)<-"UTF-8"
for my columns. However, when I save it to sql the columns are not shown in correct format, and they are like this;
When I set fast in sqlSave function to FALSE, I got this error;
Error in sqlSave(dbhandle, Distbars, tablename = "DistBars", verbose =
T, : 22001 8152 [Microsoft][ODBC SQL Server Driver][SQL
Server]String or binary data would be truncated. 01000 3621
[Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been
terminated. [RODBC] ERROR: Could not SQLExecDirect 'INSERT INTO
"DistBars" ( "regsub", "week", "S", "A", "F", "labeled_cluster",
"subgroup", "windows" ) VALUES ( 'ظâ€', 5, 4, 2, 3, 'cl1', 'ط­ظ…ظ„
ط²ط¨ط§ظ„ظ‡', 1 )'
I also tried NVARCHAR(MAX) for utf-8 column in the design of table with fast=false the error gone, but the same error with format.
By the way, a part of data is exported as RData in here.
I want to know why the data format is not shown correctly in sql server 2016?
UPDATE
I am fully assured that there is something wrong with RODBC package.
I tried inserting to table by
sqlQuery(channel = dbhandle,"insert into DistBars
values(N'7من',NULL,NULL,NULL,NULL,NULL,NULL,NULL)")
as a test, and the format is still wrong. Unfortunately, adding CharSet=utf8; to connection string does not either work.
I had the same issue in my code and I managed to fix it eliminating rows_at_time = 1 from my connection configuration.

Active Record query causing SQLException near "," syntax error

I'm trying to display all such rows of a table STUDENTS which have a value of an attribute, such as COURSE IN a set of given values e.g. 'MS', 'PhD'.
I get the values in the students_controller.rb file using params. I tried to run an Active Record query using where to do the job:
#all_courses = ['MS', 'PhD', 'BA', 'MSc']
#students = Student.where("course IN :courses" , {:courses => params.has_key?(:courses) ? params[:courses].keys : #all_courses})
But I get the following error:
SQLite3::SQLException: near ",": syntax error: SELECT "students".* FROM "students" WHERE (course IN 'MS', 'PhD', 'BA', 'MSc')
I think the error might be due to the absence of ; at the end of the SQL query generated by Active Record, but I cannot do anything to get that semicolon at the end.
You need to use parentheses: "course IN (:courses)"