0

How to these use R libraries to write and save a 'some_content' string into a field of an existing SQL table object?

I want to insert a 'some_content' string into the field barplot inside the blog_blogpost object in SQL. How to proceed?

library(DBI)
library(RSQLite)
library(tidyverse)

con <- DBI::dbConnect(RSQLite::SQLite(), dbname = 'db.sqlite3')

DBI::dbListTables(con)

 [1] "account_emailaddress"          "account_emailconfirmation"     "auth_group"                    "auth_group_permissions"       
 [5] "auth_permission"               "auth_user"                     "auth_user_groups"              "auth_user_user_permissions"   
 [9] "blog_blogpost"                 "django_admin_log"              "django_content_type"           "django_migrations"            
[13] "django_session"                "django_site"                   "searches_searchquery"          "socialaccount_socialaccount"  
[17] "socialaccount_socialapp"       "socialaccount_socialapp_sites" "socialaccount_socialtoken"     "sqlite_sequence"   


DBI::dbListFields(con, "blog_blogpost")

 [1] "id"              "content"         "title"           "slug"            "user_id"         "timestamp"       "updated"        
 [8] "publish_date"    "image"           "barplot"         "summ_bert"       "article"         "maltego_csv"     "rainette_explor"
[15] "video_url"       "pdfs"            "wordcloud"  
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • `DBI::dbWriteTable(con, "blog_blogpost", data.frame(barplot="some_content"))`, but I feel like there is so much not being said here. – r2evans Sep 19 '22 at 10:06
  • Can you please expand on what you're trying to do, perhaps with some context? My first comment answers your question of how to save a string into an existing table, but ... (1) a new row, or replacing the value in an existing row? (2) if existing, replace that column in all rows or just some rows based on a condition? (3) One could also use raw SQL, as in `DBI::dbExecute(con, "update blog_blogpost set barplot='some_content'")` for all rows, and `"... set barplot='some_content' where id=7"` for a specific row. Please expand, it's unclear/unfocused at the moment. Thanks! – r2evans Sep 20 '22 at 12:43
  • Thanks for the attention. What I've intended to do is to write to an existing field inside an sql table. I have no intent to create a new column, but i have to insert data on an empty field. Turns out I've found an workaround. As you may notice, the present tables were create by the Django framework in python3, and in the end I've found that the easiest way to fill those values was thru python itself. This R script only complements the python workflow. Later I'll try this approach you suggested to share here. – Joao Henrique da Silva Sep 21 '22 at 21:29

0 Answers0