首先將從台北市政府的OpenData讀取到R
#安裝packages:jsonlite與curl install.packages("jsonlite", repos="http://cran.r-project.org") install.packages('curl') #匯入library:jsonlite library(jsonlite) #讀取UBike並轉回DataFream格式 dataUBike <- fromJSON("http://data.taipei/opendata/datalist/apiAccess?scope=resourceAquire&rid=ddb80380-f1b3-4f8e-8016-7ed9cba571d5")在資料庫建立TABLE
USE [OpenDataDB] GO DROP TABLE [dbo].[tabUbike] GO CREATE TABLE [dbo].[tabUbike]( [sno] [int] NULL, [sna] [varchar](30) NULL, [tot] [int] NULL, [sbi] [int] NULL, [sarea] [varchar](10) NULL, [mday] [varchar](20) NULL, [lat] [decimal](13, 10) NULL, [lng] [decimal](13, 10) NULL, [ar] [varchar](100) NULL, [sareaen] [varchar](20) NULL, [snaen] [varchar](100) NULL, [aren] [varchar](100) NULL, [bemp] [int] NULL, [act] [bit] NULL ) ON [PRIMARY] GO將讀取的資料寫入SQL SERVER
#將讀取的資料寫入SQL SERVER library(RODBC) conn <- odbcDriverConnect('driver={SQL Server};server=.;database=OpenDataDB;trusted_connection=true') df <- dataUBike$result$results[c("sno","sna","tot","sbi","sarea","mday","lat","lng","ar","sareaen","snaen","aren","bemp","act")] sqlSave(conn, df, tablename = "tabUbike", rownames=FALSE, append=TRUE) close(conn);查詢資料庫驗證資料庫寫入成功 另外也可以透過R從資料庫讀取資料。
#將讀取的資料寫入SQL SERVER conn <- odbcDriverConnect('driver={SQL Server};server=.;database=OpenDataDB;trusted_connection=true') data <- sqlQuery(conn, "SELECT * FROM tabUbike"); close(conn);
請問"在資料庫建立TABLE" 這一段程式是要放在SQLit的哪裡?(我是用DB browser for SQLit)
回覆刪除