dbGetQuery
returns a data frame. In the first case it has one row, so nrow(xid)
should be 1. In the second case it should return a data frame with zero rows, so nrow(xid)
should be zero. Just check the number of rows.
empty variable when running sqlite
-
23-03-2022 - |
سؤال
So i have a variable set to an sqlite query in R like so:
query<-paste("SELECT ID FROM DataTable WHERE Name = \'", X, "\'", sep="")
xid<-dbGetQuery(conn, query)
if X is in the database, xid is
1 obs. of 1 varaibles
but if X is not in the database, xid is
0 obs of 1 variables
I can't use exists()
, or Length()
or is.integer()
or is.null()
or anything else I can think of to differentiate between them since they both exist, are the same length and aren't null.
I'm sure it's something simple, but i'm new to this (obviously).
Thanks for the help.
المحلول 2
نصائح أخرى
You can use nrow
or dim
here a reproducible example:
library(RSQLite)
driver <- dbDriver("SQLite")
conn <- dbConnect(driver, dbname='DB_KEY')
mydf <- read.table(text ='ID NAme
1 2 OtherData1
2 2 OtherData2
3 2 OtherData3
4 2 OtherData4
5 2 OtherDat',head=T)
dbWriteTable(conn, "DataTable", mydf, append=TRUE)
dbDisconnect(conn)
X <- 'OtherData1'
query<-paste("SELECT type FROM Data1 WHERE name = \'", X , "\'", sep="")
conn <- dbConnect(driver, dbname='DB_KEY')
xid<-dbGetQuery(conn, query)
dim(xid)
[1] 1 1
dim(xid.notexist)
[1] 0 1
When the query returns no data , dim(xid.notexist)[1]
= nrow(xid.notexist)
are equal to 0.
لا تنتمي إلى StackOverflow