简体   繁体   中英

How to connect to a remote sqlite database in r

I am not familiar SQLite. So I wonder if I can fetch SQLite data from a remote server?

I know I can do this quite easily with MySQL data,

# Load package.
library("RMySQL")

# MySQL connection.
DB <- dbConnect(MySQL(), user="root", password="xxx", host="127.0.0.1", db="xxxx")

# Select data from a table.
data = dbGetQuery(DB, "SELECT * FROM article")

But for SQLite, it seems that it requires a path,

# Load package.
library("RSQLite")

# Connect to the sqlite file.
DB <- dbConnect(SQLite(), dbname = "C:/SQLite/xxxx.sqlite")

How can I know and obtain the path of a remote server then? Can I do this below?

http://mywebsite-name.com/sqlite/xxxx.sqlite

I don't think you can (though I hope somebody will help me if I'm wrong, or perhaps you could raise an issue on the package github site ).

As I started to comment, download it first and operate on it as a local file. There are several ways to download files like this, but the most direct is likely with download.file .

If you want to download it and keep it around, set dbfile to be something in a known location (perhaps the current working directory. However, if keeping it around is not critical and/or you want to generalize this and not have various sqlite files sitting around, you can download them to a temporary location:

dbfile <- tempfile(fileext=".sqlite") # not created yet, just a string

To download, simply:

download.file("http://mywebsite-name.com/xxxx.sqlite", dbfile)

And then SQL away ...

library(RSQLite)
DB <- dbConnect(SQLite(), dbname=dbfile)
## ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM