pyodbc (technically pypyodbc) on SQL SERVER (windows) where I have a SELECT statement that has a join and two WHERE conditions. Each parameterized WHERE condition works when in a query by itself:
bhfc_cursor.execute("""\
select inc_auth.staff_id, inc_auth.rank, inc_auth.assignment, inc_auth.dttm from INC_AUTH
INNER JOIN INC_MAIN ON inc_auth.inci_id = inc_main.inci_id
where inc_main.inci_id = (?) """ ,this_incid)
mutual_aid_fdid = bhfc_cursor.fetchone()
bhfc_cursor.execute("""\
select inc_auth.staff_id, inc_auth.rank, inc_auth.assignment, inc_auth.dttm from INC_AUTH
INNER JOIN INC_MAIN ON inc_auth.inci_id = inc_main.inci_id
where inc_auth.auth_type = (?) """,auth_type)
mutual_aid_fdid = bhfc_cursor.fetchone()
But if I try to make it into a compound where with AND, it bombs:
bhfc_cursor.execute("""\
select inc_auth.staff_id, inc_auth.rank, inc_auth.assignment, inc_auth.dttm from INC_AUTH
INNER JOIN INC_MAIN ON inc_auth.inci_id = inc_main.inci_id
where inc_auth.auth_type = (?) and inc_main.inci_id = (?) """,auth_type, this_incid)
mutual_aid_fdid = bhfc_cursor.fetchone()
error:
File "C:\Users\bucky\OneDrive\Python_Installs\PycharmProjects\ReadSQLWireNFIRS\ReadSQQLWriteNFIRS.py", line 228, in <module>
bhfc_cursor.execute("""\
File "C:\Users\bucky\OneDrive\Python_Installs\PycharmProjects\ReadSQLWireNFIRS\Lib\site-packages\pypyodbc.py", line 1617, in execute
check_success(self, ret)
File "C:\Users\bucky\OneDrive\Python_Installs\PycharmProjects\ReadSQLWireNFIRS\Lib\site-packages\pypyodbc.py", line 1007, in check_success
ctrl_err(SQL_HANDLE_STMT, ODBC_obj.stmt_h, ret, ODBC_obj.ansi)
File "C:\Users\bucky\OneDrive\Python_Installs\PycharmProjects\ReadSQLWireNFIRS\Lib\site-packages\pypyodbc.py", line 987, in ctrl_err
raise DatabaseError(state,err_text)
pypyodbc.DatabaseError: ('07002', '[07002] [Microsoft][ODBC SQL Server Driver]COUNT field incorrect or syntax error')
I know one of the princple points in asking a question is "what have you tried"? Well, I've tried a couple dozen things. Making it into a different passing format with %s instead of (?). Making the two variables into a list. Or tuple. I've been plugging at this for three hours now and I've tried everything I can think of and everything I've found while searching. As you can probably infer, this is sort of greek to me- I see many answers where people much smarter than me know the mechanics of where things get interpreted- in the driver or the DB or wherever- so I'm not really sure what the difference is in the "?", "(?)", the %s, %d, %b, %f formats, but I can find anything that works.
The type of inci_id is <class 'pypyodbc.TupleRow..Row'> while auth_type is <class 'list'>. Thanks.
p.s. this is an internal ETL type program that will never be exposed to the outside world, so SQL injection concerns are minimal, although I know I should get into the habit of writing code that prevents them regardless...
EDIT: while looking at this again, I realized the 2nd table isn't even necessary. The table that has the columns I want also has the other INCI_ID column. But even that one fails with the same "count field incorrect.." error.
bhfc_cursor.execute("""\
select inc_auth.staff_id, inc_auth.rank, inc_auth.assignment, inc_auth.dttm from INC_AUTH
where inc_auth.auth_type = (?) and inc_auth.inci_id = (?) """,auth_type, this_incid)
mutual_aid_fdid = bhfc_cursor.fetchone()```
ANOTHER EDIT: I replaced one of the variables with a constant and the query works. Although it was somewhat obvious, this proves that the problem is with the variable substitution and not the query itself:
bhfc_cursor.execute("""\
select inc_auth.staff_id, inc_auth.rank, inc_auth.assignment, inc_auth.dttm from INC_AUTH
where (inc_auth.auth_type = 'OC' ) AND (inc_auth.inci_id = (?) ) """ , this_incid)
oic_details = bhfc_cursor.fetchone()
And here's some seemingly relevant stuff from an ODBC trace. I really don't know what much of this means however.
ReadSQQLWriteNF fb68-77d0 ENTER SQLPrepareW
HSTMT 0x000001CEFFC70470
WCHAR * 0x000001CEFF370570 [ 164] " select inc_auth.staff_id, inc_auth.rank, inc_auth.assignment, inc_auth.dttm from INC_AUTH\ a where (inc_auth.auth_type = (?) ) AND (inc_auth.inci_id = (?) ) "
SDWORD 164
ReadSQQLWriteNF fb68-77d0 EXIT SQLPrepareW with return code 0 (SQL_SUCCESS)
HSTMT 0x000001CEFFC70470
WCHAR * 0x000001CEFF370570 [ 164] " select inc_auth.staff_id, inc_auth.rank, inc_auth.assignment, inc_auth.dttm from INC_AUTH\ a where (inc_auth.auth_type = (?) ) AND (inc_auth.inci_id = (?) ) "
SDWORD 164
ReadSQQLWriteNF fb68-77d0 ENTER SQLNumParams
HSTMT 0x000001CEFFC70470
SWORD * 0x000001CEFF3B3C98
ReadSQQLWriteNF fb68-77d0 EXIT SQLNumParams with return code 0 (SQL_SUCCESS)
HSTMT 0x000001CEFFC70470
SWORD * 0x000001CEFF3B3C98 (2)
ReadSQQLWriteNF fb68-77d0 ENTER SQLDescribeParam
HSTMT 0x000001CEFFC70470
UWORD 1
SWORD * 0x000001CEFF3B3398
SQLULEN * 0x000001CEFF3B3918
SWORD * 0x000001CEFF3B3518
SWORD * 0x000001CEFF3B3718
ReadSQQLWriteNF fb68-77d0 EXIT SQLDescribeParam with return code 0 (SQL_SUCCESS)
HSTMT 0x000001CEFFC70470
UWORD 1
SWORD * 0x000001CEFF3B3398 (-9)
SQLULEN * 0x000001CEFF3B3918 (255)
SWORD * 0x000001CEFF3B3518 (0)
SWORD * 0x000001CEFF3B3718 (1)
ReadSQQLWriteNF fb68-77d0 ENTER SQLDescribeParam
HSTMT 0x000001CEFFC70470
UWORD 2
SWORD * 0x000001CEFF3B3798
SQLULEN * 0x000001CEFF3B3398
SWORD * 0x000001CEFF3B3918
SWORD * 0x000001CEFF3B3518
ReadSQQLWriteNF fb68-77d0 EXIT SQLDescribeParam with return code -1 (SQL_ERROR)
HSTMT 0x000001CEFFC70470
UWORD 2
SWORD * 0x000001CEFF3B3798
SQLULEN * 0x000001CEFF3B3398
SWORD * 0x000001CEFF3B3918
SWORD * 0x000001CEFF3B3518
DIAG [07009] [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index (0)
ReadSQQLWriteNF fb68-77d0 ENTER SQLGetDiagRecW
SQLSMALLINT 3 <SQL_HANDLE_STMT>
SQLHANDLE 0x000001CEFFC70470
SQLSMALLINT 1
SQLWCHAR * 0x000001CEFF31A070
SQLINTEGER * 0x000001CEFF3B2818
SQLWCHAR * 0x000001CEFF2704E0
SQLSMALLINT 1024
SQLSMALLINT * 0x000001CEFF3B2618
ReadSQQLWriteNF fb68-77d0 EXIT SQLGetDiagRecW with return code 0 (SQL_SUCCESS)
SQLSMALLINT 3 <SQL_HANDLE_STMT>
SQLHANDLE 0x000001CEFFC70470
SQLSMALLINT 1
SQLWCHAR * 0x000001CEFF31A070 [ 5] "07009"
SQLINTEGER * 0x000001CEFF3B2818 (0)
SQLWCHAR * 0x000001CEFF2704E0 [ 59] "[Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index"
SQLSMALLINT 1024
SQLSMALLINT * 0x000001CEFF3B2618 (59)
Related
I have a query similar like the one below where I need to use multiple string of characters to replace in a single query.
I am not too sure how to make sure a and b are passed into the %s in the query.
I am getting the error 'unused arguments (b)'
a
[1] "A10000001" "C10000002" "10000003"
[4] "10000004" "D10000005" "10000006"
b
[1] "A100000011" "B100000021" "100000031"
[4] "10000004" "10000005" "10000006"
expr1 <- sprintf("select
FSAS.a_id,
FSAS.grade,
FSAS.score,
FSAS.placement,
FSAS.start,
FSAS.completion
FROM db.Fact AS FSAS
LEFT JOIN
(SELECT FB.end , FB.ids, FB.place FROM FB where FB.ids in (%s))
ON FB.end = FSAS.start
LEFT JOIN
(SELECT FAB.a, FAB.ids FROM FAB WHERE FAB.ids in (%s))
ON FAB.a = FB.place", toString(sQuote(a, b, q = FALSE), collapse=", "))
sqlQuery(con, expr1)
Any advice please?
Thanks
sQuote only takes one vector as an argument. You can't give it a and b at the same time as separate arguments. And collapse is an argument for paste, not for toString. And sprintf will want separate arguments for each %s. I think you want
qry = "select
FSAS.a_id,
FSAS.grade,
FSAS.score,
FSAS.placement,
FSAS.start,
FSAS.completion
FROM db.Fact AS FSAS
LEFT JOIN
(SELECT FB.end , FB.ids, FB.place FROM FB where FB.ids in (%s))
ON FB.end = FSAS.start
LEFT JOIN
(SELECT FAB.a, FAB.ids FROM FAB WHERE FAB.ids in (%s))
ON FAB.a = FB.place"
expr1 = sprintf(
qry,
toString(sQuote(a, q = FALSE)),
toString(sQuote(b, q = FALSE))
)
I am having difficulty. I need to get the desired result through mysql query in R .
my attempt.
load libraries which i use.
library(RMySQL)
library(data.table)
library(dplyr)
library(Rcpp)
library(zoo)
library(gsubfn)
library(proto)
library(RSQLite)
library(DBI)
library(RMariaDB)
library(dbx)
then i try perform connect
getDf <- function (connect, sql)
{
str(paste("EXECUTE: ", sql))
query <- dbSendQuery(connect, sql)
df <- dbFetch(query, n = -1)
dbClearResult(query)
str(paste("ROW FETCHED:", nrow(df)))
df
}
then
db_user <- 'k'
db_password <- 'F'
db_name <- 'yyy'
db_table <- 'mytable'
db_host <- 'xxx' # for local access
db_port <- 3306
readDB <- dbConnect(RMariaDB::MariaDB(), user = db_user, pass = db_password, dbname = db_name, host = db_host, port = db_port)
and here i try get prepared data via query mysql
df333 <- getDf(readDB, paste("UPDATE incoming_aggregation_google ia
JOIN data_aggregation_google_median dm ON
(ia.agency_id = dm.agency_id)
AND (ia.search_category_id = dm.search_category_id)
AND (ia.offer_category_id = dm.offer_category_id)
AND (ia.flight_codes = dm.flight_codes)
AND (ia.search_type_category_id = dm.search_type_category_id)
SET ia.prediction_diff_percent_base_price = dm.median_diff_percent_base_price + (0.4 * RAND() - 0.2);
UPDATE incoming_aggregation_google_general ia
JOIN data_aggregation_google_general_median dm ON
(ia.agency_id = dm.agency_id)
AND (ia.offer_category_id = dm.offer_category_id)
AND (ia.search_type_category_id = dm.search_type_category_id)
AND (ia. service_discount_category_id = dm. service_discount_category_id)
SET ia.prediction_diff_percent_base_price = dm.median_diff_percent_base_price + (0.4 * RAND() - 0.2);
UPDATE incoming_aggregation_google_general da
JOIN data_aggregation_google_general_median i ON
(
i.agency_id=da.agency_id
AND i.offer_category_id=da.offer_category_id
AND i.service_discount_category_id=da.service_discount_category_id
AND i.search_type_category_id=da.search_type_category_id
)
set da.prediction_diff_percent_base_price = i.median_diff_percent_base_price;", db_table))
but the error
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE incoming_aggregation_google_general ia
JOIN data_aggregation_google_gener' at line 9 [1064]
Please help me understand what I did wrong and how to fix it? If what I want to do is not can be done, is there a way to translate the essence of this query into logic R.
Thanks for your any valuable help
I have 2 tables A and B. I'm taking the name value of each row of table A in the array var1 and then put values of array var1 in WHERE condition of a SELECT query of table B and print the result of SELECT query of table B.
Code:
my $sql2 = 'select name,value from A';
my $sth2 = $dbh1 ->prepare($sql2);
$sth2 ->execute();
while (my #row = $sth2 ->fetchrow_array)
{
print join(',', #row), "\n\n";
push(#var1, $row[0]);
}
foreach(#var1 ) {
print "$_\n";
}
print "\n";
my $sth3 = $dbh ->prepare("select name, value from B where name in (".join(", ",#var1).")"); - line 99
$sth3 ->execute(#var1);
while (my #row = $sth3 ->fetchrow_array)
{
print join(", ", #row), "\n";
}
But getting error:
DBD::Oracle::db prepare failed: ORA-00904: "5": invalid identifier
(DBD ERROR: error possibly near <> indicator at char 143 in 'select
name,value from B where name in(1,2,3,4,<>5)') [for Statement "select
name,value from B where name in(1,2,3,4,5)"] at hello.pl line 99.
Can't call method "execute" on an undefined value at hello.pl line
102.
The query you are using is incorrect
select name,value from A where in (1,2,3,4,5)
Add a column name between Where and In
select name,value from A where *column_you_checking* in (1,2,3,4,5)
my $sth3 = $dbh->prepare(select name,value from B where *column_you_checking* in(".join(",",#var1).")); - line 99
Little description of task:
Need to get total of base if we will selling coin_quantity of coin to the market.
By default, this example means, that market is coin+"_"+base. No flip case.
For simplification, there is only one case(action): selling / ask.
python:
def get_depth_price( db_cursor, coin_quantity, coin, market ):
sql_command = "SELECT * FROM '" + market + "' WHERE type=1"
db_cursor.execute( sql_command )
bids = sorted([ i for i in db_cursor.fetchall() ], \
key = lambda k: k[3], reverse = True )
tmp_quantity = float( coin_quantity )
tmp_total = 0
for bid in bids:
if tmp_quantity > bid[4]:
tmp_total += bid[4] * bid[3]
tmp_quantity -= bid[4]
else:
tmp_total += tmp_quantity * bid[3]
tmp_quantity = 0
break
return tmp_total
Where market data looks like:
# echo "SELECT * from 'DOGE_BTC' WHERE type=1
ORDER BY price DESC;" | sqlite3 big.db
6f827564d88ddd0d99a9f976ac384a3f|0|1|5.0e-07|374365.08
1f696fea1270c07d9d4217e47ad40d3c|0|1|4.9e-07|1337443.42857
b9bee0a3bc2d4b241383062f06569b54|0|1|4.8e-07|465618.375
716cb29e0f5fe4742de73302e5b88250|0|1|4.7e-07|197560.659574
3189ed55c60530014892c6a3fce673e8|0|1|4.6e-07|115757.521739
cf19858241fb25de9095160b1704ef44|0|1|4.5e-07|237807.133333
f53642c0e7d5074daaa2b324e82483c5|0|1|4.4e-07|16112.6818182
ee8fb3f5255fb0ef8c157becb6a8c539|0|1|4.3e-07|22581.0697674
### (0)id ----^ (1)self---^ ^ ^-(3)price ^
### (2)type(ask=0/bid=1)-------+ (4)quantity--+
Methinks, that python script - is very very slow thing against sql.
However, I can't create similar on the sql language.
How to replace full function of get_depth_price by sql-query?
How to create on sql similar to if tmp_quantity > bid[4] / then / else ?
I'm been staring at my code for over an hour and I still can't figure out whats wrong with my query.
I'm getting the error message:
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near ")"
LINE 38: WHERE (abs(ax2 - ?) / ? < ?)
query = %Q{
SELECT
avg(ax2),
avg(ay2),
avg(az2)
FROM (
SELECT
avg(t1) as t2,
avg(n1) as n2,
avg(ax1) as ax2,
avg(ay1) as ay2,
avg(az1) as az2
FROM (
SELECT
avg(t) as t1,
avg(n) as n1,
avg(ax) as ax1,
avg(ay) as ay1,
avg(az) as az1
FROM obds
WHERE uid = #{uid} AND (ev = 6011 or ev = 6012)
GROUP BY
round(t,3),
round(n,3)
)
AS derivedTable1
GROUP BY round(t1 + 0.0005,3), round(n1 + 0.0005,3)
)
AS derivedTable2
WHERE (abs(ax2 - ?) / ? < ?) #<<<<< Line with error
}
binds = [ uid, x_average, x_average, THRESHOLD_FOR_RESTING ]
result = Ozd.find_by_sql query, binds
The answer is that the query has to be apart of the bind array.
binds = [ query, uid, x_average, x_average, THRESHOLD_FOR_RESTING ]
results = Ozd.find_by_sql binds
try this WHERE (abs((ax2 - ?) / ? < ?)) instead