3

I am very comfortable using R, but I've never used RSQLite or even SQLite, so I have a question on how to best use RSQLite to add data from a data frame in R to an SQLite table.

I understand that I can create tables and add data like so:

db <- dbConnect(SQLite(), dbname="Test.sqlite")

dbSendQuery(conn = db,  "CREATE TABLE School (SchID INTEGER,
        Location TEXT, Authority TEXT, SchSize TEXT)")

dbSendQuery(conn = db,
            "INSERT INTO School
         VALUES (1, 'urban', 'state', 'medium')")
dbSendQuery(conn = db,
            "INSERT INTO School
         VALUES (2, 'urban', 'independent', 'large')")
dbSendQuery(conn = db,
            "INSERT INTO School
         VALUES (3, 'rural', 'state', 'small')")

However, to do multiple INSERT statements like this, I'd need to parse all rows of the data frame using a for loop. Is it possible to use vectors and a single INSERT to populate the SQLite table with all values from a data frame?

1 Answer 1

7

You can use dbWriteTable to write the table in one go. Read the vignette to see more examples.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.