Eric Berger
2017-Aug-29 09:21 UTC
[R] DBI::dbWriteTable syntax error apparently from quotes
I have been successfully using RODBC for a long time (years) to connect to MS SQL Server from R. This week I wanted to try using odbc but I am seeing some problems which may be related to how I set up my driver and/or connection. The dbWriteTable manual page gives as an example command: dbWriteTable( pDB$con, "mtcars", mtcars[1:5,]) When I try this I get the following error Error: <SQL> 'CREATE TABLE "mtcars" ( "row_names" varchar(255), "mpg" FLOAT, "cyl" FLOAT, "disp" FLOAT, "hp" FLOAT, "drat" FLOAT, "wt" FLOAT, "qsec" FLOAT, "vs" FLOAT, "am" FLOAT, "gear" FLOAT, "carb" FLOAT ) ' nanodbc/nanodbc.cpp:1587: 42000: [FreeTDS][SQL Server]Incorrect syntax near 'mtcars'. I believe that the complaint is related to the double quotes around "mtcars" (and presumably the other double quotes in the command). I tried searching to see if others have had this problem but I could not find anything. Thanks [[alternative HTML version deleted]]
Jeff Newmiller
2017-Aug-29 13:43 UTC
[R] DBI::dbWriteTable syntax error apparently from quotes
Double quotes are not legal SQL syntax. Use single quotes. -- Sent from my phone. Please excuse my brevity. On August 29, 2017 2:21:44 AM PDT, Eric Berger <ericjberger at gmail.com> wrote:>I have been successfully using RODBC for a long time (years) to connect >to >MS SQL Server from R. >This week I wanted to try using odbc but I am seeing some problems >which >may be related to how I set up my driver and/or connection. >The dbWriteTable manual page gives as an example command: > >dbWriteTable( pDB$con, "mtcars", mtcars[1:5,]) > >When I try this I get the following error > >Error: <SQL> 'CREATE TABLE "mtcars" ( > "row_names" varchar(255), > "mpg" FLOAT, > "cyl" FLOAT, > "disp" FLOAT, > "hp" FLOAT, > "drat" FLOAT, > "wt" FLOAT, > "qsec" FLOAT, > "vs" FLOAT, > "am" FLOAT, > "gear" FLOAT, > "carb" FLOAT >) >' > nanodbc/nanodbc.cpp:1587: 42000: [FreeTDS][SQL Server]Incorrect syntax >near 'mtcars'. > >I believe that the complaint is related to the double quotes around >"mtcars" (and presumably the other double quotes in the command). >I tried searching to see if others have had this problem but I could >not >find anything. > >Thanks > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
Eric Berger
2017-Aug-29 14:34 UTC
[R] DBI::dbWriteTable syntax error apparently from quotes
I did not put the double quotes in the SQL statement. The SQL statement was created automatically by the routine dbWriteTable. The full code (minus the details of creating the DBIConnection object) would be like this, as in the man page for dbWriteTable library(DBI) library(odbc) conn <- [connect to database] DBI::dbWriteTable(conn,"mtcars",mtcars[1:5,]) The above code produces the error message in the OP On Tue, Aug 29, 2017 at 4:43 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:> Double quotes are not legal SQL syntax. Use single quotes. > -- > Sent from my phone. Please excuse my brevity. > > On August 29, 2017 2:21:44 AM PDT, Eric Berger <ericjberger at gmail.com> > wrote: > >I have been successfully using RODBC for a long time (years) to connect > >to > >MS SQL Server from R. > >This week I wanted to try using odbc but I am seeing some problems > >which > >may be related to how I set up my driver and/or connection. > >The dbWriteTable manual page gives as an example command: > > > >dbWriteTable( pDB$con, "mtcars", mtcars[1:5,]) > > > >When I try this I get the following error > > > >Error: <SQL> 'CREATE TABLE "mtcars" ( > > "row_names" varchar(255), > > "mpg" FLOAT, > > "cyl" FLOAT, > > "disp" FLOAT, > > "hp" FLOAT, > > "drat" FLOAT, > > "wt" FLOAT, > > "qsec" FLOAT, > > "vs" FLOAT, > > "am" FLOAT, > > "gear" FLOAT, > > "carb" FLOAT > >) > >' > > nanodbc/nanodbc.cpp:1587: 42000: [FreeTDS][SQL Server]Incorrect syntax > >near 'mtcars'. > > > >I believe that the complaint is related to the double quotes around > >"mtcars" (and presumably the other double quotes in the command). > >I tried searching to see if others have had this problem but I could > >not > >find anything. > > > >Thanks > > > > [[alternative HTML version deleted]] > > > >______________________________________________ > >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >https://stat.ethz.ch/mailman/listinfo/r-help > >PLEASE do read the posting guide > >http://www.R-project.org/posting-guide.html > >and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Apparently Analagous Threads
- DBI::dbWriteTable syntax error apparently from quotes
- Problema de INSERT en Windows SQL
- Bug in by() function which works for some FUN argument and does not work for others
- Bug in by() function which works for some FUN argument and does not work for others
- Bug in by() function which works for some FUN argument and does not work for others