RODBC access sqlserver (reproduced)

Reprinted from: https://www.cnblogs.com/tgzhu/p/5776927.html

In my actual work, the data source is the relational database MS SqlServer on the one hand, and HBase on the other hand. This section mainly introduces the installation and configuration of accessing MS SqlServer through RODBC, see information ( https://msdn.microsoft.com/en-us/library/hh568454(v=sql.110).aspx ), please note: Download the msodbc There is a mandatory correspondence between the version and the unixODBC version, see: ( https://msdn.microsoft.com/en-us/library/hh568449(v=sql.110).aspx ), this article chooses to install msodbcsql -11.0.2270.0 , according to the official introduction, the corresponding unixodbc version is: unixODBC-2.3.0

content:

  • unixODBC installation
  • Msodbcsql installation
  • ODBC configuration
  • RODBC installation
  • RODBC call example

illustrate:


  • unixODBC provides Linux support for ODBC, but it is just an ODBC manager. To connect to an actual database, you need to provide an ODBC driver for this database
  • msodbcsql is a free ODBC driver for connecting to sqlServer under Linux ( Note: You must install unixODBC first, and then install sqlserverodbc )
  • RODBC is a package for R scripts to connect to ODBC

unixODBC installation :


  • Download the software installation package ( unixODBC-2.3.0.tar.gz ), download address: https://sourceforge.net/projects/unixodbc/files/unixODBC/2.3.0/
  • Command: yum remove unixODBC    (if other versions of unixODBC have been installed on the machine, they need to be removed before installation)
  • Command: tar xvzf unixODBC-2.3.0.tar.gz    (Upload the downloaded package to the linux host and extract it)
  • Command: cd unixODBC-2.3.0    (switch to the unzipped directory)
  • 命令:CPPFLAGS="-DSIZEOF_LONG_INT=8"
  • Command: export CPPFLAGS
  • 命令:./configure --prefix=/usr --libdir=/usr/lib64 --sysconfdir=/etc --enable-gui=no --enable-drivers=no --enable-iconv --with-iconv-char-enc=UTF8 --with-iconv-ucode-enc=UTF16LE
  • Command: make   (compile and install)
  • Command: make install

Msodbcsql installation :


  • Software installation package download ( msodbcsql-11.0.2270.0.tar.gz ), download address: https://www.microsoft.com/en-us/download/details.aspx?id=36437
  • Command: tar xvzf msodbcsql-11.0.2270.0.tar.gz   (the installation package is uploaded to the linux host and decompressed)
  • Command: cd msodbcsql-11.0.2270.0      (switch to the home directory after decompression)
  • Command: ./install.sh verify        (verifies whether the current environment meets the software installation requirements)
  • Command: ./install.sh install    (under the premise that the previous step is ok, execute the installation)
  • Command: odbcinst -q -d -n "ODBC Driver 11 for SQL Server"     (Test whether the installation is successful, if the following is ok)
    Description=Microsoft ODBC Driver 11 for SQL Server
    Driver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-11.0.so.2270.0
    Threading=1
    UsageCount=1
  •  

ODBC configuration :


  • After the installation is successful, the odbcinst.ini and odbc.ini files can be found in the /etc directory
  • odbcinst.ini indicates what types of drivers have been installed on this machine. After correct installation, the content is as follows
    [ODBC Driver 11 for SQL Server]
    Description=Microsoft ODBC Driver 11 for SQL Server
    Driver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-11.0.so.2270.0
    Threading=1
    UsageCount=1
  •  Command: vim /etc/odbc.ini     (configure MS sqlserver connection information), enter the following in the opened file

    [testsql]
    Driver=ODBC Driver 11 for SQL Server
    Server = database server name or IP
    Database = database name
  •  Note: Driver assignment must be consistent with the security in the odbcinst file

  • Command: isql testsql sa XXXX     (testsql: the set odbc connection name, followed by a space to enter the user name and password, the successful connection is OK, as shown in the figure)
  • Command:  quit    (exit)

RODBC installation:


  • Command: R     (to enter the R command line)
  • Execute: install.packages("RODBC")      (install RODBC)
  • After the installation is complete, execute the following script on the R command line, and the result is OK
    library(RODBC)
    pile <- odbcConnect("testsql",uid = "sa",pwd = "yourPassword")
    data <- sqlQuery(pile,"select top 5 code,name from codeItems")
    close(pile)
    data
  • The result is as follows, then ok

ODBC access interface


  • odbcConnect can open a connection, returning a handle for subsequent database access. Printing a connection will give some details of the ODBC connection, and calling  odbcGetInfo  will give some details of the client and server.
  • Details about the tables in a connection can be obtained with the function  sqlTables  .
  • The function  sqlSave  will copy the R data frame to a database table,
  • The function  sqlFetch  copies a table from a database to an R data frame
  • Querying through sqlQuery , the returned result is an R data frame.
  • sqlCopy passes a query to the database, and the returned results are stored in the database as a table. A better way to control is to call odbcQuery first, and then use sqlGetResults to get the results. The latter can be used to get finite rows at a time in a loop, as the function sqlFetchMore does.
  • The connection can be closed by calling the function  close  .

sqlSave function


  • copy code
    sqlSave(channel, dat, tablename = NULL, append = FALSE,
            rownames = TRUE, colnames = FALSE, verbose = FALSE,
            safer = TRUE, addPK = FALSE, typeInfo, varTypes,
            fast = TRUE, test = FALSE, nastring = NULL)
    
    sqlUpdate(channel, dat, tablename = NULL, index = NULL,
              verbose = FALSE, test = FALSE, nastring = NULL,
              fast = TRUE)
    copy code

     

  • append: Represents whether to append or not. The default is not to append. If a table already has data, you can use append to append new data. The same column is required. Generally, this is enough.

  • rownames: can be a logical value or a character type.
  • colnames: column names;
  • verbose: The default is FALSE, whether to send the statement to the R interface, if TRUE, then each uploaded data will appear in the command column.
  • addPK: Whether to specify rownames as the primary key.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325734604&siteId=291194637