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 the 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 :


  • 安装成功后,在 /etc 目录下能找着 odbcinst.ini  和 odbc.ini 文件
  • odbcinst.ini 表示本机已经安装了哪些类型的驱动,正确安装后内容如下
    [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
  •  命令:vim /etc/odbc.ini    (配置MS sqlserver 连接信息),在打开的文件中输入如下内容

    [testsql]
    Driver=ODBC Driver 11 for SQL Server
    Server= 数据库服务器名或IP
    Database=数据库名
  •  注意:Driver 赋值必须与 odbcinst 文件中 安全一致

  • 命令:isql testsql sa XXXX    (testsql: 设置的odbc连接名,后面以空格输入用户名和密码,成功连接则OK,如图)
  • 命令: quit   (退出)

RODBC安装:


  • 命令:R    (进入R命令行)
  • 执行:install.packages("RODBC")     (安装RODBC)
  • 安装完成后,在R命令行下执行如下脚本,结果如下则OK
    library(RODBC)
    pile <- odbcConnect("testsql",uid = "sa",pwd = "yourPassword")
    data <- sqlQuery(pile,"select top 5 code,name from codeItems")
    close(pile)
    data
  • 结果如下,则ok

ODBC的访问接口


  • odbcConnect  可以打开一个连接,返回一个用于随后数据库访问的控制(handle)。 打印一个连接会给出ODBC连接的一些细节,而调用 odbcGetInfo 会给出客户端和服务器的一些细节信息。
  • 在一个连接中的表的细节信息可以通过函数 sqlTables 获得。
  • 函数 sqlSave 会把 R 数据框复制到一个数据库的表中,
  • 函数 sqlFetch 会把一个数据库中的表拷贝到 一个 R 的数据框中
  • 通过sqlQuery进行查询,返回的结果是 R 的数据框。
  • sqlCopy把一个 查询传给数据库,返回结果在数据库中以表的方式保存。 一种比较好的控制方式是首先调用 odbcQuery, 然后 用 sqlGetResults 取得结果。后者可用于一个循环中 每次获得有限行,就如函数 sqlFetchMore 的功能。
  • 连接可以通过调用函数 close 来关闭。

sqlSave函数


  • 复制代码
    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)
    复制代码

     

  • append:代表是否追加,默认不追加,如果一张已经有数据的表,就可以用append追加新的数据,需要同样的column,一般开个这个就行。

  • rownames:可以是逻辑值,也可以是字符型。
  • colnames:列名;
  • verbose:默认为FALSE,是否发送语句到R界面,如果TRUE,那么每条上传数据就会出现在命令栏目致之中。
  • addPK:是否将rownames指定为主键。

Guess you like

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