sql - new column each row results from a query to mysql database in R -
i've got simple dataframe 3 columns. 1 of columns contains database name. first need check if data exists, , if not, insert it. otherwise nothing.
sample data frame:
clientid;region;database 135;europe;europedb 2567;asia;asiadb 23;america;americadb so created function apply dataframe way:
library(rmysql) check_if_exist <- function(df){ con <- dbconnect(mysql(), user="myuser", password="mypass", dbname=df$database, host="myhost") query <- paste0("select count(*) table client_id='", df$clientid,"' , region='", df$region ,"'") rs <- dbsendquery(con, query) rs } function call:
df$new_column <- lapply(df, check_if_exist) but doesn't work.
this working example of asking, if understood correctly. don't have database, print query verification, , fetch random number result.
note doing lapply(df,...), looping on columns of database, , not rows want.
df = read.table(text="clientid;region;database 135;europe;europedb 2567;asia;asiadb 23;america;americadb",header=t,sep=";") check_if_exist <- function(df){ query = paste0("select count(*) table client_id='", df$clientid,"' , region='", df$region ,"'") print(query) rs <- runif(1,0,1) return(rs) } df$new_column <- sapply(split(df,seq(1,nrow(df))),check_if_exist) hope helps.
Comments
Post a Comment