0

I have a .sql file that I am trying to read into SQL database using R package RSQLite. To familiarize myself, first, I looked at R dataset mtcars:

mydb <- dbConnect(RSQLite::SQLite(), "")
dbWriteTable(mydb, "mtcars", mtcars)
str(mydb)

The structure is as follows:

Formal class 'SQLiteConnection' [package "RSQLite"] with 5 slots
  ..@ Id                 :<externalptr> 
  ..@ dbname             : chr ""
  ..@ loadable.extensions: logi TRUE
  ..@ flags              : int 6
  ..@ vfs                : chr ""

Next, I simply read five lines from the database:

dbGetQuery(mydb, 'SELECT * FROM mtcars LIMIT 5')

          row_names  mpg cyl disp  hp drat    wt  qsec vs am gear carb
1         Mazda RX4 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
2     Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
3        Datsun 710 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
4    Hornet 4 Drive 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
5 Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

Now, I can see the column names, and maybe I am only interested in hp>200, so I do:

dbGetQuery(mydb, 'SELECT * FROM mtcars WHERE hp > 200')
            row_names  mpg cyl disp  hp drat    wt  qsec vs am gear carb
1          Duster 360 14.3   8  360 245 3.21 3.570 15.84  0  0    3    4
2  Cadillac Fleetwood 10.4   8  472 205 2.93 5.250 17.98  0  0    3    4
3 Lincoln Continental 10.4   8  460 215 3.00 5.424 17.82  0  0    3    4
4   Chrysler Imperial 14.7   8  440 230 3.23 5.345 17.42  0  0    3    4
5          Camaro Z28 13.3   8  350 245 3.73 3.840 15.41  0  0    3    4
6      Ford Pantera L 15.8   8  351 264 4.22 3.170 14.50  0  1    5    4
7       Maserati Bora 15.0   8  301 335 3.54 3.570 14.60  0  1    5    8

Now, I am actually trying to look at my data of interest (not mtcars anymore) called myData.sql. I was told that this file is generic SQL, and that I could load into whatever database I preferred, and that it came from PostgreSQL.

So, I try this, which outputs the same structure as before:

mydb <- dbConnect(RSQLite::SQLite(), "myData.sql")
str(mydb)
Formal class 'SQLiteConnection' [package "RSQLite"] with 5 slots
  ..@ Id                 :<externalptr> 
  ..@ dbname             : chr "myData.sql"
  ..@ loadable.extensions: logi TRUE
  ..@ flags              : int 6
  ..@ vfs                : chr ""

Now, I would like to see what is stored in myData.sql, hoping to see a data table with columns (like for mtcars). However, I get errors when I try:

dbGetQuery(mydb, 'SELECT * LIMIT 5')
Error in sqliteSendQuery(con, statement, bind.data) : 
  error in statement: no tables specified

and

dbGetQuery(mydb, 'SELECT * FROM mydb LIMIT 5')
Error in sqliteSendQuery(con, statement, bind.data) : 
  error in statement: no such table: mydb

I am very unsure how to begin to explore the information in myData.sql. Any advice is greatly appreciated!

1 Answers1

0

First things first, mydata.sql is not an SQLite database, it's a text file containing SQL statements that will create and populate your database.

I suggest you download an SQLite data browser such as this one, which is multiplatform. Using this tool, you can create an actual SQLite database. For instance, if you use sqlitebrowser, go to File > Import > Database from SQL file. This will create the database which you can then connect to from within R.

If you don't like the data browser I suggested, there are plenty other ones that you can try, they all do more or less the same thing. Good luck!

Edit

To do everything from R, here's a suggestion using RSQLite:

library(RSQLite)
con <- dbConnect(SQLite(), "my_db.sqlite")

If the sql file has everything it needs to create the tables, you can use RSQLite's dbSendQuery function:

fileName <- 'mydata.sql'
sql.query <- readChar(fileName, file.info(fileName)$size)
dbSendQuery(con, sql.query)
dbListTables(con) # to see a list of all created tables

If you run into problems at the dbSendQuery step, that's a case where the data browser tools come in particularly handy, as the error messages you'll get from the R interpreter might not give you all the necessary details to understand what's going wrong.

Dominic Comtois
  • 10,230
  • 1
  • 39
  • 61
  • Thank you for explaining my misinformation. I am wondering why you suggest using the SQLite data browser (and other data browsers) over the RSQLite package. Is it because my .sql file cannot be populated with the RSQLite package? Thanks. –  Mar 12 '15 at 12:30
  • You can do everything from R. But I find it more convenient to do that kind of background work in suck a tool. I can update my answer to show how to do it from R. – Dominic Comtois Mar 12 '15 at 12:31
  • I would be grateful if you could do that! If it is not too difficult from R; I do wonder how it would work. Also, I did your instructions, but got an error "Error importing data: Error in statement #1: near "SET" syntax error. Aborting execution." I could not find much about this on Google to troubleshoot. Thanks again... –  Mar 12 '15 at 12:43
  • There might be compatibility issues at play. SQL is not totally standard across the different DBMS. You might need to adapt the sql syntax to be able to run it with SQLite, unfortunately. If you ran into this problem from the data browser tool, you'll get blocked by the R-way as well. – Dominic Comtois Mar 12 '15 at 12:46
  • Just saw that for multi-line strings, RSQLite seems to struggle -- not sure but it might be when non-unix linebreaks are present. See this post for more info: http://stackoverflow.com/questions/18914283/how-to-execute-more-than-one-rsqlite-statement-at-once-or-how-to-dump-a-whole-fi – Dominic Comtois Mar 12 '15 at 13:04
  • Oh, I see. Thanks for the link. I am a little unclear what the dbSendQueries() function does that the user wrote on StackOverflow. I see that it calls dbGetQuery (http://www.inside-r.org/packages/cran/DBI/docs/dbSendQuery). Does this mean that if I used the script you linked to on StackOverflow, then I would also need to use the function fetch to get the records I need? I also notice that the script does not save anything as a variable. So, I would need to redo this each time I wanted to extract records? It has been running ~40 min now. I wonder if that is normal? The .sql is 41MB. –  Mar 12 '15 at 13:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72850/discussion-between-dominic-comtois-and-user2808302). – Dominic Comtois Mar 12 '15 at 14:59
  • Thanks for your help. I think I originally missed your kind offer to chat. A lot of time has passed, but I never quite figured this out, and am tackling it again. I would appreciate any additional help, if at all possible!... –  May 21 '15 at 01:30