Why does DB2 return -1 when deleting all rows in a table? - db2

The question says it all. Using the Python API as an example:
import ibm_db
#setup stuff
conn = ibm_connect(DATABASE, user, password)
stmt = ibm_db.exec_immediate(conn, 'DELETE FROM sometable')
print(ibm_db.num_rows(stmt)) # prints -1
Why doesn't it print the actual count of rows deleted?

This is not an answer, really, just showing that the function does work and that SQLCODEs cause python exceptions. The fact that num_rows() does not work for you may indicate that the functionality may not be supported by the database you're connected to. You may want to describe your environment in detail: the DB2 server version and platform, whether it's a local or remote database, the DB2 client version (if different from the server) etc.
>>> import ibm_db
>>> conn = ibm_db.connect('TEST',user,password)
>>> stmt = ibm_db.exec_immediate(conn, 'create table t (f int)')
>>> print ibm_db.num_rows(stmt) # DDL statement - num_rows not applicable
-1
>>> stmt = ibm_db.exec_immediate(conn,'delete from t')
>>> print ibm_db.num_rows(stmt) # 0 rows deleted
0
>>> stmt = ibm_db.exec_immediate(conn,'delete from x') # nonexistent table - exception
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception: [IBM][CLI Driver][DB2/LINUXX8664] SQL0204N "USER.X" is an undefined name. SQLSTATE=42704 SQLCODE=-204
>>> stmt = ibm_db.exec_immediate(conn,'insert into t(f) values (1),(2),(3)')
>>> print ibm_db.num_rows(stmt) # 3 rows inserted
3
>>> stmt = ibm_db.exec_immediate(conn,'delete from t')
>>> print ibm_db.num_rows(stmt) # 3 rows deleted
3
>>> ibm_db.close(conn)
True
>>>

A negative SQL Code in DB2 means an error:
SQL codes

Related

I am unable to COPY my CSV to postgres using psycopg2/copy_expert

edit:
In postgresql.conf, the log_statement is set to:
#log_statement = 'none' # none, ddl, mod, all
My objective is to COPY a .cvs file containing ~300k records to Postgres.
I am running the script below and nothing happens; no error or warning but still the csv is not uploaded.
Any thoughts?
import psycopg2
# Try to connect
try:
conn = psycopg2.connect(database="<db>", user="<user>", password="<pwd>", host="<host>", port="<port>")
print("Database Connected....")
except:
print("Unable to Connect....")
cur = conn.cursor()
try:
sqlstr = "COPY \"HISTORICALS\".\"HISTORICAL_DAILY_MASTER\" FROM STDIN DELIMITER ',' CSV"
with open('/Users/kevin/Dropbox/Stonks/HISTORICALS/dump.csv') as f:
cur.copy_expert(sqlstr, f)
conn.commit()
print("COPY pass")
except:
print("Unable to COPY...")
# Close communication with the database
cur.close()
conn.close()
This is what my .csv looks like
Thanks!
Kevin
I suggest you to load in first time your df with pandas
import pandas as pd
import psycopg2
conn = psycopg2.connect(database="<db>", user="<user>", password="<pwd>", host="<host>", port="<port>")
cur = conn.cursor()
df = pd.read_csv('data.csv')
cur.copy_from(df, schema , null='', sep=',/;', columns=(df.columns))
For the part columns=(df.columns) I forgot if they want turple or list but should work with a conversion and you should read this
Pandas dataframe to PostgreSQL table using psycopg2 without SQLAlchemy? who could help you

psycopg2.connect() fails when connecting with database

I am trying to access a database via postgresql2 with my jupyter notebook but I receive the following error message.
OperationalError: could not create SSL context: no such file
import pandas as pd
import psycopg2
#Connect to postgres
conn_string = "host='xx' sslmode='require' \
dbname='dbname' port='xx' user='xx' \
password='xx'"
#Create rework dataset
conn = psycopg2.connect(conn_string)
postgreSQL_select_Query = u'SELECT * FROM "xx"."yy"'
conn.set_client_encoding('UNICODE')
cursor = conn.cursor()
cursor.execute(postgreSQL_select_Query)
colnames = [desc[0] for desc in cursor.description]
df_imp = cursor.fetchall()
df = pd.DataFrame(data=df_imp, columns=colnames)
Expected result is the access to the database and generation of dataframe.
Actual result is OperationalError: could not create SSL context: no such file by step conn = psycopg2.connect(conn_string)
---------------------------------------------------------------------------
OperationalError Traceback (most recent call last)
<ipython-input-2-932b2fb01c9f> in <module>
5
6 #Create rework dataset
----> 7 conn = psycopg2.connect(conn_string)
8 postgreSQL_select_Query = u'SELECT * FROM "xx"."xx"'
9 conn.set_client_encoding('UNICODE')
~\AppData\Local\Continuum\anaconda3\lib\site-packages\psycopg2\__init__.py in connect(dsn, connection_factory, cursor_factory, **kwargs)
128
129 dsn = _ext.make_dsn(dsn, **kwargs)
--> 130 conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
131 if cursor_factory is not None:
132 conn.cursor_factory = cursor_factory
OperationalError: could not create SSL context: No such process
After trying several solutions, the problem was the version of psycopg2 library.
conda update does not install the latest version of the library. However, pip does it and then my code works again!

Formatting SQL query

I am trying to make a query for my PostgreSQL database.
I think the format of my query is wrong and I can't seem to get it to work, I have posted my code below:
query = cur.execute('SELECT "KINASE_NAME" FROM public."Phosphosite_table"
WHERE "GENE_NAME" LIKE %(genename)s AND "RESIDUE" LIKE %(location)s')
The aim is to take the kinase name is the gene name and location match.
my error message appears as the following:
ProgrammingError Traceback (most recent call last)
<ipython-input-33-9eae43b913d6> in <module>()
35 cur = connection.cursor()
36
---> 37 query = cur.execute('SELECT "KINASE_NAME" FROM public."Phosphosite_table" WHERE "GENE_NAME" LIKE%(genename)s AND "RESIDUE" LIKE %(location)s')
Thanks!
Connor
Don't use string operations to build SQL queries. Use the proper %s syntax.
genname = "foo"
location = "bar"
cur.execute("SELECT ... LIKE %s and ... LIKE %s", (genname, location))
No quotes around the values must be used. The quoting will be done by the DB API library.

psycopg2 does not insert unicode data

I have a script that takes data from one database and according to tables' names and fields copies it into another database.
The issue is about unicode data, I need to insert some words in Russian but every time psycopg2 writes it as if it were default string.
import psycopg2
import psycopg2.extensions
conn_two = psycopg2.connect(user="postgres", password="password", host = "localhost", port= "5432", dbname = "base2")
cur_2 = conn_two.cursor()
sql = 'INSERT INTO {} ({}) VALUES {};'.format('"tb_names"', '"num", "name", "district"', (23, 'Рынок', 'Волжский'))
cur_2.execute(sql)
conn_two.commit()
Here is how the result looks like in pgAdmin4:
I also tried to set exteinsions and insert data in unicode, but in this case I have an error
import psycopg2
import psycopg2.extensions
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
conn_two = psycopg2.connect(user="postgres", password="password", host = "localhost", port= "5432", dbname = "base2")
conn_two.set_client_encoding("utf-8")
conn_two.set_client_encoding('UNICODE')
cur_2 = conn_two.cursor()
sql = 'INSERT INTO {} ({}) VALUES {};'.format('"tb_names"', '"num", "name", "district"', (23, u'Рынок', u'Волжский'))
cur_2.execute(sql)
conn_two.commit()
Traceback (most recent call last):
File "D:\_Scripts\pgadmin.py", line <>, in <module>
cur_2.execute(sql)
psycopg2.ProgrammingError: ОШИБКА: тип "u" не существует # - says that type "u" does not exist
LINE 1: ...ing_ex" ("num", "name", "district") VALUES (23, u'\u0420\u...
^
What should be done here?
Don't prepare your string with the values baked in (using string formatting or concatenation).
Instead, use %s in the SQL string as placeholders, then pass your values to the .execute method as an argument.
If you're on Python 2.x, non-ASCII strings should be passed as Unicode types.
E.g.
Python 2.x
sql = 'INSERT INTO "tb_names" ("num", "name", "district") VALUES (%s, %s, %s);'
cur_2.execute(sql, (23, u'Рынок', u'Волжский'))
Python 3.x
sql = 'INSERT INTO "tb_names" ("num", "name", "district") VALUES (%s, %s, %s);'
cur_2.execute(sql, (23, 'Рынок', 'Волжский'))

Python psycopg2 - using .format() with a dbname inside a string

I'm using psycopg2 to query a database that starts with a number + ".district", so my code goes like:
number = 2345
cur = conn.cursor()
myquery = """ SELECT *
FROM {0}.districts
;""".format(number)
cur.execute("""{0};""".format(query))
data = cur.fetchall()
conn.close()
And i keep receiving the following psycopg2 error..
psycopg2.ProgrammingError: syntax error at or near "2345."
LINE 1: SELECT * FROM 2345.districts...
Thought it was the type of data the problem, maybe int(number) or str(number)..but no, same error appears.
¿ What am i doing wrong ?
The way you are trying to use to pass parameters is not supported. Please read the docs.