Search code examples
rsqldfrsqlite

Unknown error in sqldf package


What does this error mean?

Closing open result set, pending rowsError in result_create(conn@ptr, statement) : near "(": syntax error

This appears when i want to run the code

    ```{r}
    library(sqldf)
    first <- dbConnect(SQLite(), dbname= "DATA.sqlite")

    dbSendQuery(conn =  first,
                "CREATE TABLE COMPANY_MASTER
                ( 
    CompId INTEGER,
    CompName TEXT,
    Address TEXT,
    DirectorName TEXT,
    EmployeeNo INTEGER,
    PRIMARY KEY(CompName)
                )")
    dbSendQuery(conn = first, "INSERT INTO COMPANY_MASTER
                VALUES(1001,'Infosys','ABC1927','Dr.Sandeep',128)")
    dbSendQuery(conn = first, "INSERT INTO COMPANY_MASTER
                VALUES(1002,'Cognizant','ERT654','Michael',156)")
    dbSendQuery(conn = first, "INSERT INTO COMPANY_MASTER
                VALUES(1003,'Tata','PCD531','Sancheti',113)")
    *

dbSendQuery(conn =  first,
                "CREATE TABLE INCOME
                ( 
    CompName TEXT,
    In(2016) INTEGER,
    In(2017) INTEGER,
    In(2018) INTEGER,
    FOREIGN KEY(CompName)

*
                )")
    dbSendQuery(conn = first, "INSERT INTO INCOME
                VALUES('518361','528464','538646')")
    dbSendQuery(conn = first, "INSERT INTO INCOME
                VALUES('218434','205314','225815')")
    dbSendQuery(conn = first, "INSERT INTO INCOME
                VALUES('1038434','1184344','128434')")
    CompMAst <- dbGetQuery(conn = first, "SELECT * FROM COMPANY_MASTER")
    Income <- dbGetQuery(conn = first, "SELECT * FROM INCOME")
    ```

While giving the above error in the

dbSendQuery(conn =  first,
            "CREATE TABLE INCOME
            ( 
CompName TEXT,
In(2016) INTEGER,
In(2017) INTEGER,
In(2018) INTEGER,
FOREIGN KEY(CompName)
            )")

region of the code. What does this error mean? Is it some specific format error? Or have i missed some chunk that had to be mentioned before executing. Or is some other package involved Please help.


Solution

  • According to @SatZ and the error of you giving four columns and entering three values your ideal code should be

    ```{r}
    library(sqldf)
    first <- dbConnect(SQLite(), dbname= "DATA.sqlite")
    
    dbSendQuery(conn =  first,
                "CREATE TABLE COMPANY_MASTER
                ( 
    CompId INTEGER,
    CompName TEXT,
    Address TEXT,
    DirectorName TEXT,
    EmployeeNo INTEGER,
    PRIMARY KEY(CompName)
                )")
    dbSendQuery(conn = first, "INSERT INTO COMPANY_MASTER
                VALUES(1001,'Infosys','ABC1927','Dr.Sandeep',128)")
    dbSendQuery(conn = first, "INSERT INTO COMPANY_MASTER
                VALUES(1002,'Cognizant','ERT654','Michael',156)")
    dbSendQuery(conn = first, "INSERT INTO COMPANY_MASTER
                VALUES(1003,'Tata','PCD531','Sancheti',113)")
    dbSendQuery(conn =  first,
                "CREATE TABLE INCOME
                ( 
    CompName TEXT,
    IN_2016 TEXT,
    IN_2017 TEXT,
    IN_2018 TEXT)")
    dbSendQuery(conn = first, "INSERT INTO INCOME
                VALUES('Infosys','5183.61','5284.64','5386.46')")
    dbSendQuery(conn = first, "INSERT INTO INCOME
                VALUES('Cognizant','2184.34','2053.14','2258.15')")
    dbSendQuery(conn = first, "INSERT INTO INCOME
                VALUES('Tata','10384.34','11843.44','1284.34')")
    CompMAst <- dbGetQuery(conn = first, "SELECT * FROM COMPANY_MASTER")
    Income <- dbGetQuery(conn = first, "SELECT * FROM INCOME")
    ```