Insert in RMySQL from data frame

前端 未结 2 762
误落风尘
误落风尘 2020-12-30 13:32

Im trying to add data to MySQL table by using RMySQL. I only need to add one row at a time and it\'s not working. What I\'m trying to do is this.

dbGetQuery(         


        
相关标签:
2条回答
  • 2020-12-30 13:43

    You could try with:

    dbWriteTable(names, data[1,],append=True)
    

    as the DBI package details

    0 讨论(0)
  • 2020-12-30 13:54

    You can use paste to construct that actual query.

    dat <- matrix(1:4, 2, 2)
    query <- paste("INSERT INTO names VALUES(",data[1,1], ",", data[1,2], ")")
    query
    #[1] "INSERT INTO names VALUES( 1 , 3 )"
    dbGetQuery(con, query)
    
    # If there are a lot of columns this could be tedious...
    # So we could also use paste to add all the values at once.
    query <- paste("INSERT INTO names VALUES(", paste(data[1,], collapse = ", "), ")")
    query
    #[1] "INSERT INTO names VALUES( 1, 3 )"
    
    0 讨论(0)
提交回复
热议问题