Guided Project: Designing and Creating a Database in R

While creating a table in mlb.db, i’m getting an error. Can someone help me to solve this;

conn <- dbConnect(SQLite(),“mlb.db”)
dbWriteTable(conn, name=“game_log”, value = “game_log.csv”, header=TRUE)

Error in connection_import_file([email protected], name, value, sep, eol, skip) :
RS_sqlite_import: game_log.csv line 354 expected 161 columns of data but found 164

Hi @ranjith89poovanna,

I would be happy to help you with it. Please send me the R script file :slightly_smiling_face:.


Hi Sahil,
Sorry for the late response. Have attached my R script.


MLBAnalytics.R (633 Bytes)

When I was completing this project, I ran into the same issue. It seemed to happen when I tried to call the csv file directly into dbWriteTable(). Instead, I read in the csv file to memory, then used dbWriteTable() and it seemed to clear out the error.

game_log <- read_csv(‘game_log.csv’,
col_types = cols(.default = ‘c’,
v_league = ‘c’, h_league = ‘c’,
completion = ‘c’, protest = ‘c’,
v_first_catcher_interference = ‘c’,
h_first_catcher_interference = ‘c’,
2b_umpire_id = ‘c’, 2b_umpire_name = ‘c’,
3b_umpire_id = ‘c’, 3b_umpire_name = ‘c’,
lf_umpire_id = ‘c’, lf_umpire_name = ‘c’,
rf_umpire_id = ‘c’, rf_umpire_name = ‘c’,
winning_rbi_batter_id = ‘c’,
winning_rbi_batter_id_name = ‘c’))

conn <- dbConnect(SQLite(), ‘mlb.db’)

dbWriteTable(conn = conn, name = ‘game_log’, value = game_log, row.names = FALSE, header = TRUE)