dbSendquery giving format error - sqldf

I need to create a table and insert some values into it uisng r
library(sqldf)
Company_Master <- dbConnect(SQLite(), dbname="Company Master.sqlite" )
dbSendQuery(conn = Company_Master,
"CREATE TABLE Company Master(
Comp_name varchar
Address varchar)
")
Till here no error is given.But after this when i want to insert some value into the table
dbSendQuery(conn = Company_Master, "INSERT INTO COMPANY MASTER
VALUES('INFOSYS','abcLALA')")
I get error like
Error in result_create(conn#ptr, statement) : near "Master": syntax error
I found the format on https://www.r-bloggers.com/r-and-sqlite-part-1/ and don't seem to get any other format except if using dbCEATETABLE. What is wrong in this format? Please help

Related

ADF: Pass dynamic Where Clause as a string with quotes

I have a lookup that retrieves a few records from a SQL Server table containing server, database, schema, table name and a whole where clause. These values are passed to a copy data (within a ForEach) In the copy data i have tried to use two different Dynamic query statement, but I seem to get an error. And can't figure out where I'm going wrong.
Values in table:
SRC_SERVERNAME
SRC_DATABASE
SRC_SCHEMANAME
SRC_TABLENAME
SRC_WHERE_DATE_CLAUSE
SQ01
NAV
dbo
Company$Sales Invoice Header
where [Posting Date] >= '2021-01-01'
Source setup:
Error for statement 1:
A database operation failed with the following error: 'Incorrect syntax near '.'.'
Incorrect syntax near '.'., SqlErrorNumber=102,Class=15,State=1,
Error for statement 2:
A database operation failed with the following error: 'Incorrect syntax near '.'.'
Incorrect syntax near '.'., SqlErrorNumber=102,Class=15,State=1,
Statement 1 (query):
SELECT *
FROM #{item().SRC_SERVERNAME}.#{item().SRC_DATABASENAME}.#{item().SRC_SCHEMANAME}.#{item().SRC_TABLENAME},' ',#{item().SRC_WHERE_DATE_CLAUSE}
Statement 2 (dynamic query with concat):
#concat('select * from ',item().SRC_SERVERNAME,'.',item().SRC_DATABASENAME,'.',item().SRC_SCHEMANAME,'.',item().SRC_TABLENAME,' ',item().SRC_WHERE_DATE_CLAUSE)
There is a syntax error in your query.
In the 4-part naming of SQL database, server name/database name/schema name/table name should be separated by ‘.’.
When you have space or other special characters in the name of server/database/schema/table, they should be embedded inside square braces [].
#concat('select * from [',item().SRC_SERVERNAME, '].[',item().SRC_DATABASENAME,'].[',item().SRC_SCHEMANAME,'].[',item().SRC_TABLENAME, '] ',item().SRC_WHERE_DATE_CLAUSE)

syntax error at or near "I" while trying to insert rows in postgresql via python script

Trying to insert a query into a postgres table via python code.
Code line:
query = f"INSERT INTO Phone (data, result, reason) VALUES ('{json.dumps(data)}', {val}, '{json.dumps(reason)}') RETURNING id;"
here, data and reason are columns of type json and result is a boolcolumn.
Query on printing the variable:
INSERT INTO Phone (data, result, reason) VALUES ('{'ICl': False, 'Poster': True, 'Lock': True, 'Model': 'ABC'}', True, '{}') RETURNING id;
Error:
syntax error at or near "I"
LINE 1: I
^
The same query on copy pasting into Table plus directly gives me no error and the row gets inserted.
Where is the error? Can't seem to understand why the query is giving me such error that too only in Python code. Please help.
You need to let psycopg2 handle the quoting for you.
Please try this:
query = """INSERT INTO Phone (data, result, reason)
VALUES (%s,%s,%s) RETURNING id;"""
cursor.execute(query, (json.dumps(data), val, json.dumps(reason),))

Condition IF In Power Query's Advanced Editor

I have a field named field, and I would like to see if it is null, but I get an error in the query, my code is this:
let
Condition= Excel.CurrentWorkbook(){[Name="test_table"]}[Content],
field= Condition{0}[fieldColumn],
query1="select * from students",
if field <> null then query1=query1 & " where id = '"& field &"',
exec= Oracle.Database("TESTING",[Query=query1])
in
exec
but I get an error in the condition, do you identify the mistake?
I got Expression.SyntaxError: Token Identifier expected.
You need to assign the if line to a variable. Each M line needs to start with an assignment:
let
Condition= Excel.CurrentWorkbook(){[Name="test_table"]}[Content],
field= Condition{0}[fieldColumn],
query1="select * from students",
query2 = if field <> null then query1 & " some stuff" else " some other stuff",
exec= Oracle.Database("TESTING",[Query=query2])
in
exec
In query2 you can build the select statement. I simplified it, because you also have conflicts with the double quotes.
I think you're looking for:
if Not IsNull(field) then ....
Some data types you may have to check using IsEmpty() or 'field is Not Nothing' too. Depending on the datatype and what you are using.
To troubleshoot, it's best to try to set a breakpoint and locate where the error is happening and watch the variable to prevent against that specific value.
To meet this requirement, I would build a fresh Query using the PQ UI to select the students table/view from Oracle, and then use the UI to Filter the [id] column on any value.
Then in the advanced editor I would edit the generated FilteredRows line using code from your Condition + field steps, e.g.
FilteredRows = Table.SelectRows(TESTING_students, each [id] = Excel.CurrentWorkbook(){[Name="test_table"]}{0}[fieldColumn])
This is a minor change from a generated script, rather than trying to write the whole thing from scratch.

Using SELECT .. AS in a Python Sqlite query

In SQLite3, I have a "Accounts" table. I can run any of these queries:
SELECT Last_Bal AS Balance FROM Accounts;
SELECT Last_Bal AS 'Balance' FROM Accounts;
SELECT Last_Bal as "Balance" FROM Accounts;
All three queries run fine, and display the word "Balance" as the column heading.
However, when I run this code in Python 3:
c.Execute( 'SELECT Last_Bal AS Balance FROM Accounts' )
I get the error message: "sqlite3.OperationalError: near "Balance": syntax error". If I try putting single quotes or double quotes around the word Balance, as in this:
c.Execute( 'SELECT Last_Bal AS "Balance" FROM Accounts' )
I get the error message: "_tkinter.TclError: Invalid column index Balance"
I've tried swapping the single quotes for double quotes, and even triple quotes, but nothing seems to work.
SOLVED: Stupid User Error - I was using two different SELECT statements trying to build a TreeView. Thanks for your help.

Converting double to numeric - Sybase

I have a column with type double precision and I'd like copy values from this column to another column with type numeric(19,2).
When I do select CONVERT( numeric(19,2), PriceNettoTmp) from Table it works but when I do update Table set PriceNetto = CONVERT( numeric(19,2), PriceNettoTmp) it doesn't.
Error message for update when executing in Squirrel SQL:
Data Truncation error occured on a write of column -1Data was -1
bytes long and -1 bytes were transferred.
Error message for update when executing in Sybase Central:
Could not execute statement. Arithmetic overflow during explicit
conversion of FLOAT NULL value '10,449999999999999.0' to a NUMERIC
field . Sybase error code=247 Severity Level=16, State=1, Transaction
State=0 Line 1
Anybody knows what can be wrong?
EDIT
I found a solution which works: update Table set PriceNetto = CONVERT( numeric(19,2), str_replace(str(PriceNettoTmp,19,2),',','.'))
Although I still don't understand why select was working and update not. And is there any simpler solution than my?
Solution (as stated by OP)
update Table set PriceNetto = CONVERT( numeric(19,2), str_replace(str(PriceNettoTmp,19,2),',','.'))

Resources