Installing the RODBC package in 1.R
Install.packages ("RODBC")
2. Installing MySQL ODBC in Windows system Drive
Note distinguish between 32-bit and 64-bit versions;
Http://dev.mysql.com/downloads/connector/odbc
System configuration for 3.ODBC
Under Windows operating system: Control Panel, administrative Tools, data source (ODBC), double-click Add, select MySQL ODBC driver
Fill in: Data source name an entry you want to use the name, you name it, for example: Mysql_data;
Description A random fill, such as MyData
TCP/IP Server fills out the MySQL database with the address, such as: 127.0.0.1
User fills in your MySQL username
Password fill in your MySQL password
The database will then appear in your MySQL database, select a database.
4. Turn on the R environment test
#R代码 查询示例
library(RODBC)
channel<
-
odbcConnect(
"mysql_data"
, uid
=
"baidu_index"
, pwd
=
"******"
)
# 64 位ODBC,****替换为输入密码
sqlKeyword
= "SELECT * FROM BAIDU_INDEX WHERE col_keyword = ‘李易峰‘ AND col_date>=‘2015-01-01‘"
dataKeyword
=
sqlQuery(channel,sqlKeyword)
keywordList<
-
dataKeyword[[
"col_keyword"
]]
keywordList<
-
as.character(keywordList)
|
5. Rodbc other functions for working with MySQL database
# 函数名称 # 函数描述
odbcConnect(dsn, uid
=
"
", pwd="
")
# 建立并打开连接
sqlFetch(channel, sqltable)
# 从数据库读取数据表,并返回一个数据框对象
sqlQuery(channel, query)
# 向数据库提交一个查询,并返回结果
sqlSave(channel, mydf, tablename
= sqtable, append
= FALSE)
# 将一个数据框写入或更新(append=True)到数据库
sqlDrop(channel, sqtable)
# 从数据库删除一个表
sqlClear(channel, sqtable)
# 删除表中的内容
sqlTables(channel)
# 返回数据库中表的信息
sqlColumns(channel, sqtable)
# 返回数据库表sqtable列的信息
close(channel)
# 关闭连接
|
R language reading MySQL data sheet