R语言连接操作mysql数据库读写代码
代码语言:r
所属分类:其他
代码描述:R语言连接操作mysql数据库读写代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
library(RMySQL)# 如果没安装就install.packages("RMySQL") #help(package="RMySQL") #查看说明文档 #创建数据库连接 con <- dbConnect(MySQL(), host="localhost", dbname="guest", user="guest", password="p6yhG8iDRMNxdJ9w") summary(con) #获取连接信息 dbGetInfo(con) #获取连接信息 dbListTables(con) #查看database下所有表格 dbRemoveTable(con, "guest") #删除名为test的表 #读数据库表 dbReadTable(con,"User")#中文出现乱码,这是因为字符编码格式不统一的问题 dbSendQuery(con,'SET NAMES utf8') #写数据库表 #fruits <-data.frame(id=1:5,name=c("苹果","香蕉","梨子","玉米","西瓜"),price=c(8.8,4.98,7.8,6,2.1),status=c("无","打折","无","售罄","批发")) #dbListTables(con) #dbWriteTable(con,"fruits",fruits) #dbListTables(con) #写数据表,覆盖追加 #testA <-data.frame(id=1:6,e=c("a","b","c","d","e","f"),c=c("我","的","世","界","变","得")) #testB <-data.frame(id=7:13,e=c("g","h","i","j","k","l","m"),c=c("奇","妙","跟","难","以","言","喻")) #直接写testA写入test表中 #dbWriteTable(con,"test",testA,row.names=F) #dbReadTable(con,"test") #追加写testB追加在test表后 #dbWriteTable(con,"test",testB,append=T,row.names=F) #dbRe.........完整代码请登录后点击上方下载按钮下载查看
网友评论0