Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute more than one RSQLite statement at once or how to dump a whole file?

Tags:

sqlite

r

Working with RSQLite to build up a SQLite database I want to send more than one statement at a time - is this possible?

Why do these not work:

sql <- readLines("createtables.sql")
dbSendQuery(con,sql)

... and ...

sql <- paste(readLines("createtables.sql"),collapse="")
dbSendQuery(con,sql)

... and ...

sql <- paste(readLines("createtables.sql"),collapse="\n")
dbSendQuery(con,sql)

while these do:

sql <- "CREATE TABLE birthdays (
nameid INTEGER PRIMARY KEY AUTOINCREMENT ,
firstname VARCHAR(100) NOT NULL ,
lastname VARCHAR(100) NOT NULL ,
birthday DATE ) ; "
dbSendQuery(con,sql)

sql <- "/* table def: foodtypes */
CREATE TABLE foodtypes (
foodid INTEGER PRIMARY KEY AUTOINCREMENT ,
foodname VARCHAR(100) NOT NULL,
healthy INTEGER,
`kcal/100g` float );"
dbSendQuery(con,sql)

the content of createtables.sql is:

/* table def:  birthdays */
CREATE TABLE birthdays (
nameid INTEGER PRIMARY KEY AUTOINCREMENT ,
firstname VARCHAR(100) NOT NULL ,
lastname VARCHAR(100) NOT NULL ,
birthday DATE ) ;

/* table def: foodtypes */
CREATE TABLE foodtypes (
foodid INTEGER PRIMARY KEY AUTOINCREMENT ,
foodname VARCHAR(100) NOT NULL,
healthy INTEGER,
`kcal/100g` float );
like image 350
petermeissner Avatar asked Sep 20 '13 10:09

petermeissner


People also ask

How to execute multiple queries in SQLite using executescript () method?

The above code executes multiple SQLite statements at once. The first executescript () method in the code creates and inserts the data into a table in one instance. And then second executescript () method updates all the records in one instance. In this way, a set of many queries can be executed in SQLite using executescript ().

How to run multiple statements in SQL Developer?

The Trick: Run Statement Works With Multiple Statements! It says ‘run statement,’ but if you select more than one with your mouse and hit the button – it will run each and throw the results to 1 grid for each statement. If you mouse hover over the Query Result panel tab, SQL Developer will tell you the query used to populate that grid.

How to execute multiple queries in one row using Execute () method?

The basic execute () method allows us to only accept one query at a time, so when you need to execute several queries we need to arrange them like a script and pass that script to the executescript () method. executescript () can be able to execute series of several SQL/SQLite queries in a row.

How to execute many SQLite statements in Python?

- GeeksforGeeks How to Execute many SQLite Statements in Python? In SQLite using the executescript () method, we can execute multiple SQL statements/queries at once.


1 Answers

As their simply seems no way to convince RSQLite-functions to execute more than one statement at once I wrote two functions that solve this problem:

(1) sqlFromFile() reads in SQL-files and transforms the text so that each statement covers exactly one line.

(2) dbSendQueries() is analogous to dbSendQuery() provided by RSQLite but applies the query-function to each line (each element of the vector) of the provided text so that a whole bunh of statements can be run.

# read in sql-statements and preformat them                                        
sqlFromFile <- function(file){
    require(stringr)
    sql <- readLines(file)
    sql <- unlist(str_split(paste(sql,collapse=" "),";"))
    sql <- sql[grep("^ *$", sql, invert=T)]
    sql
}

# apply query function to each element
dbSendQueries <- function(con,sql){
    dummyfunction <- function(sql,con){
        dbSendQuery(con,sql)
    }
    lapply(sql, dummyfunction, con)
}

# solution for example in question
dbSendQueries( con, sqlFromFile("createtables.sql") )
like image 109
petermeissner Avatar answered Oct 16 '22 21:10

petermeissner