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.
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")
```
The dbSendQuery() method returns a "DBIResult" object, it's the caller's responsibility to clear it with dbClearResult(). For queries that don't return a result set, use dbExecute() instead of dbSendQuery() to avoid the warning. (dbExecute() is the equivalent of dbGetQuery() for queries that don't return a result set.)
See the help to dbBind() for use cases of the longer form dbSendQuery() + dbFetch() + dbClearResult().
Related
I need to update multiple rows of a PLSQL table, something like this:
StringBuilder stringBuilder = new StringBuilder(
"UPDATE review_item SET LAST_MODIFIED_TIMESTAMP = systimestamp, ")
.append("target_urn = ?, ")
.append("assignee_urn = NULL WHERE item_id in (")
.append(StringUtils.join(itemIds, ","))
.append(")");
PreparedStatement ps = connection.prepareStatement(stringBuilder.toString());
OracleUtil.bindInput(ps, 1, getRandomUuid(), dbClauses.getQueuePostCleanState());
Issue is - I need all the rows to get updated with a different random guid, in this case it is getting updated with the same one. Any way to do this?
I've 2 tables in my postegreSQL database
CREATE TABLE touriste (
idclient BIGSERIAL PRIMARY KEY,
numclient INT,
nameclient VARCHAR(500),
codepost INT,
departement VARCHAR(500),
pays VARCHAR(100)
);
CREATE TABLE reservation (
idresa BIGSERIAL NOT NULL,
PRIMARY KEY(idresa),
dateresa DATE,
datearriv DATE,
datedep DATE,
idclient_cli BIGINT
REFERENCES touriste (idclient) MATCH FULL ON UPDATE CASCADE ON DELETE RESTRICT,
);
I tried to fullfill the database tables with my dataframe (which is already created in R) by using RPostgreSQL library. The probleme is that the column idclient_cli is empty.
Here is my R code :
dbWriteTable(con, "touriste",
value = dataAdb[, c(2:4, 17:19)], append = TRUE, row.names = FALSE)
# query the data from postgreSQL
df_postgres_tou <- dbGetQuery(con, "SELECT * from touriste")
View(df_postgres_tou)
dbWriteTable(con, "reservation",
value = dataAdb[, c(5:7, 14, 12, 13, 15:16, 20)], append = TRUE, row.names = FALSE)
# query the data from postgreSQL
df_postgres_resa <- dbGetQuery(con, "SELECT * from reservation")
View(df_postgres_resa)
My question is how can I match idclient value with the idclient_cli
Thanks in advance,
This is the program however when I run it says cannot find function dbSendQuery or dbCoonect or any function in the program.I am not sure is it due to me not installing the right packages
install.packages("RMySQL")
library(RMySQL)
mydb <- dbConnect(MySQL(), dbname='W1547901_0', user='',password='',host='elephant.ecs.westminster.ac.uk')
dbSendQuery(mydb, "drop table if exists books")
# creating tables in bookstore:
dbSendQuery(mydb, "
CREATE TABLE books (
book_id INT,
title VARCHAR(50),
publisher VARCHAR(50));")
dbListTables(mydb)
dbSendQuery(mydb, "INSERT INTO books
(book_id, title, publisher)
VALUES(1, 'R and MySQL', 'Addison Wesley');")
try1 = fetch(dbSendQuery(mydb, "SELECT book_id, title, publisher
FROM books
WHERE book_id = 1 ;"))
I am having a transaction that needs to run on sqlite. The Transaction includes several queries. If built separately (line by line) it works but it requires the call of dbSendQuery n-times. It slows down the code quite a bit.
The ideal code should look like this, but multiline query fails:
library("RSQLite")
con <- dbConnect(RSQLite::SQLite(), dbname="test.db")
dbSendQuery(con, paste("CREATE TABLE Variables (Name TEXT PRIMARY KEY, RealValue REAL, IntegerValue INTEGER, BlobValue BLOB, TextValue TEXT)"))
dbSendQuery(con, paste("INSERT INTO Variables (Name) VALUES ('newid');"))
dbSendQuery(con, paste("BEGIN IMMEDIATE TRANSACTION;",
"UPDATE Variables SET TextValue = '0' WHERE Name = 'newid';",
"UPDATE Variables SET RealValue = 0 WHERE Name = 'newid';",
"UPDATE Variables SET IntegerValue = 0 WHERE Name = 'newid';",
"COMMIT;",sep=" "))
a <- dbFetch(dbSendQuery(con, paste("SELECT * FROM _Variables;")))
dbSendQuery(con, paste("COMMIT;"))
Consider the RSQLite methods: dbBegin() and dbCommit() (w/ dbRollback for any errors) to handle transactions. Even consider combining all UPDATE statements into one:
library("RSQLite")
con <- dbConnect(RSQLite::SQLite(), dbname="test.db")
dbSendStatement(con, "CREATE TABLE [Variables] ([Name] TEXT PRIMARY KEY, [RealValue] REAL, [IntegerValue] INTEGER, [BlobValue] BLOB, [TextValue] TEXT)")
dbSendStatement(con, "INSERT INTO [Variables] ([Name]) VALUES ('newid')")
dbBegin(con)
tryCatch({
dbSendStatement(con, "UPDATE [Variables] SET TextValue = '0', RealValue = 0, IntegerValue = 0 WHERE [Name] = 'newid'")
}, error=function(e) dbRollback(con))
dbCommit(con)
a <- dbGetQuery(con, "SELECT * FROM [Variables]")
You can even combine UPDATE statement into one query:
Using SQLite 3.6.21, I would like to update a column in a table.
The goal is to "squeeze out" NULLs from a column if there is only 1 unique real value in that column. If LastName contained "Smith", "Johnson", and a NULL, then do nothing.
For example:
create table foo (FirstName char(20), LastName char(20));
insert into foo values ('Joe', 'Smith');
insert into foo values ('Susan', NULL);
insert into foo values ('Shirley', 'Smith');
insert into foo values ('Kevin', NULL);
Since there is only one last name, I want to replace the NULLs with Smith. I have tried this without success. It ends up replacing the whole column with NULLs.
UPDATE foo
SET LastName =
( CASE
WHEN ((select count(distinct LastName) from foo) = 1) THEN (SELECT distinct LastName from foo)
ELSE LastName
END
);
EDIT:
I'm executing this in Python using the following code:
import sqlite3 as lite
'''
con = lite.connect('test.db')
names = (
('Joe', 'Smith'),
('Susan', None),
('Shirley', 'Smith'),
('Kevin', None),
)
squeezecmd = "UPDATE foo SET LastName = (CASE WHEN ((select count(distinct LastName) from foo) = 1) THEN (SELECT distinct LastName from foo) ELSE LastName END)"
with con:
cur = con.cursor()
cur.execute("CREATE TABLE foo(FirstName TEXT, LastName TEXT)")
cur.executemany("INSERT INTO foo VALUES(?, ?)", names)
cur.execute(squeezecmd)
cur.execute("SELECT * FROM foo")
rows = cur.fetchall()
for row in rows:
print row
Python reorders the "SELECT distinct LastName from foo" so that the NULL is the first value. SQL provides "Smith" as the first value. To ignore the NULL I changed that line to
...THEN (SELECT distinct LastName from foo where LastName is NOT NULL)
EDIT:
Copy from SQL console:
sqlite>
sqlite> SELECT distinct LastName from foo;
Smith
sqlite>
Copy from Python:
with con:
cur = con.cursor()
cur.execute("SELECT distinct LastName from foo")
answer = cur.fetchall()
print answer
Results in
[(None,), (u'Smith',)]