```{r warning =FALSE, message = FALSE,echo=FALSE,output.var="info_query"}
library(RMySQL)
db
8306,dbname="database",user="uesrname",password="123456")
dbSendQuery(db,'SET NAMES gbk') # 防止乱码,如不行改成utf8
```
```{sql,connection=db,max.print = 20,warnig = FALSE}
select
user_name,
sum(shows),
sum(charge)
from table
where
stdate between 20170630 and 20170630
group by user_name limit 20
···
SQL
SQLThe SQL engine uses the DBI package to execute SQL queries, print their results, and optionally assign the results to a data frame. The SQL engine is available only in the most recent version of knitr (v1.14) which you can install as follows:
install.packages("knitr")##
To use the knitr SQL engine you first need to establish a DBI connection to a database (typically via the dbConnect function). You can make use of this connection in a SQL chunk via the connection option. For example:
```{r}
library(DBI)
db
```
```{sql, connection=db}
SELECT * FROM trials
```
By default SELECT queries will display the first 10 records of their results within the document.
Number of Records Displayed
The number of records displayed is controlled by the max.print option, which is turn derived from the global knitr option sql.max.print (i.e. opts_knit$set(sql.max.print = 10)). For example, the following code chunk displays the first 20 records:
```{sql, connection=db, max.print = 20}
SELECT * FROM trials
```
You can specify no limit on the records to be displayed via max.print = -1 or max.print = NA.
Table Captions
By default the knitr SQL engine includes a caption that indicates the total number of records displayed. You can override this caption using the tab.cap chunk option. For example:
```{sql, connection=db, tab.cap = "My Caption"}
SELECT * FROM trials
```
You can specify that you want no caption all via tab.cap = NA.
Assigning Results to a Data Frame
If you want to assign the results of the SQL query to an R data frame, you can do this using the output.var option, for example:
```{sql, connection=db, output.var="trials"}
SELECT * FROM trials
```
When the results of a SQL query are assigned to a data frame no records are printed within the document (if desired, you can manually print the data frame in a subsequent R chunk).
Using R Variables in Queries
If you need to bind the values of R variables into SQL queries, you can do so by prefacing R variable references with a ?. For example:
```{r}
subjects
```
```{sql, connection=db, output.var="trials"}
SELECT * FROM trials WHERE subjects >= ?subjects
```
Setting a Default Connection
如果有多个SQL块需要写,那么,设置一个通用接口是一个不错的选择,这样你就不必为每个分割的代码块进行相同设置而感到厌烦了。
```{r setup}
library(DBI)
db
knitr::opts_chunk$set(connection = "db")
```
以上设置完成后,就可以直接输入sq语句来直接查询数据,感觉省去的大量语句,非常的清爽。
```{sql}
SELECT * FROM trials
`` `