Rstudio 01 连接MySQL

> install.packages("RMySQL")
also installing the dependency ‘DBI’

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/DBI_0.6-1.zip'
Content type 'application/zip' length 744728 bytes (727 KB)
downloaded 727 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/RMySQL_0.10.11.zip'
Content type 'application/zip' length 2296782 bytes (2.2 MB)
downloaded 2.2 MB

package ‘DBI’ successfully unpacked and MD5 sums checked
package ‘RMySQL’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\fangping\AppData\Local\Temp\Rtmp4OKUkD\downloaded_packages
> library(RMySQL)  
载入需要的程辑包:DBI
> help(package=”RMySQL”) #查看RMySQL的说明文档,里面有RMySQL所有可用的方法  
Error: unexpected input in "help(package=?
> #创建数据库连接  
> con <- dbConnect(MySQL(),host="localhost",dbname="test2",user="root",password="xinwei")  
> #获取连接信息,查看database下所有表,以及删除testname表  
> summary(con)  
<MySQLConnection:0,0>
  User:   root 
  Host:   localhost 
  Dbname: test2 
  Connection type: localhost via TCP/IP 

Results:
> dbGetInfo(con)  
$host
[1] "localhost"

$user
[1] "root"

$dbname
[1] "test2"

$conType
[1] "localhost via TCP/IP"

$serverVersion
[1] "5.6.36"

$protocolVersion
[1] 10

$threadId
[1] 3

$rsId
list()

> dbListTables(con)  
[1] "data_category_url"  "demo"               "tb_common_biz"     
[4] "tb_data_createtime" "tb_data_privilege"  "test_event"        
[7] "xw_sequence"       
> 


    #读数据库表(show tables);
       dbListTables(con)
    #读数据库表
    > dbReadTable(con,"xixi")
    #写数据库表  
    fruits <-data.frame(id=1:3,create_time=c("2017-05-03 16:11:40","2017-05-03 16:11:41","2017-05-03 16:11:42"),privilege_person=c(0,1,2),person_or_role=c(1,2,3),is_creator=c(1,2,3),data_id=c(20170512001,20170512002,2017051200))  
    dbListTables(con)  
    dbWriteTable(con,"fruits",fruits)  
    dbListTables(con)


> dbReadTable(con,"xixi")
  id         create_time privilege_person person_or_role is_creator     data_id
1  1 2017-05-03 16:11:40                0              1          1 20170512001
2  2 2017-05-03 16:11:41                1              2          2 20170512002
3  3 2017-05-03 16:11:42                2              3          3  2017051200
> dbListTables(con) 
[1] "data_category_url"  "demo"               "fruits"             "tb_common_biz"     
[5] "tb_data_createtime" "tb_data_privilege"  "test_event"         "xixi"              
[9] "xw_sequence"       

 

posted @ 2017-05-12 15:17  Alamps  阅读(432)  评论(0编辑  收藏  举报