Delete values from db2 table if exist in schema in Python - python

i want to ask for a little help about my problem. I have sql query that get all the tables from some schema and put those tables in a list in Python. For example:
tablesList = ['TABLE1','TABLE2',...]
After i get this list of tables that i want i go one more time through each table in a for loop for example:
for t in range(len(tables)):
table = tables[t]
...
#here i want to check if this table exist in some db2 schema and if exist delete content
#of this table, otherwise go with next table check and don't delete content
Query for checking will be:
sql = """SELECT COUNT(*) FROM SYSIBM.SYSTABLES
WHERE TYPE = 'T'
AND CREATOR = 'MY_SCHEMA'
AND NAME = '{table}';""".format(table = table)
cursor.execute(sql)
rows_count = cursor.fetchone()
if rows_count is None:
pass
else:
delete...

Related

How can I handle errors inside of a for loop inside of a cx_Oracle connection?

here's a run down of what I'd like to do: I have a list of table names, and I want to run sql against an oracle database and pull back the table name and row count for every table in my table list. However, not every table name in my list of table names is necessarily actually in the database. This causes my code to throw a database error. What I would like to do, is whenever I come to a table name that is not in the database, I create a dataframe that contains the table name and instead of count(*), there's some text that says 'table not found', or something similar. At the end of the loop I'm concatenating all of the dataframes into one dataframe. The overall goal here is to validate that certain tables exist and that they have the expected row counts.
query_list=[]
df_List=[]
connstr= '%s/%s#%s' %(username, password, server)
conn = cx_Oracle.connect(connstr)
with conn:
query_list = ["SELECT '%s' as tbl, count(*) FROM %s." %(elm, database) +elm for elm in table_list]
df_List = [pd.read_sql(elm,conn) for elm in query_list]
df = pd.concat(df_List)
Consider try/except handling to return query output or table not found output:
def get_table_count(sql, conn, elm):
try:
return pd.read_sql(sql, conn)
except:
return pd.DataFrame({'tbl': elm, 'note': 'table not found'}, index = [0])
with conn:
sql = "SELECT '{t}' as tbl, count(*) as table_count FROM {d}.{t}"
df_List = [get_table_count(sql.format(t = elm, d = database), conn, elm) \
for elm in table_list]
df = pd.concat(df_List, ignore_index = True)
Get a list of all the Table Names which are in the DB, then create a loop to query each Table to get the row count.
Here is a SQL statement to get a list of all Tables in an Oracle DB:
SQL:
SELECT DISTINCT TABLE_NAME FROM ALL_TAB_COLUMNS ORDER BY TABLE_NAME ASC;
Python (to make list of tables you want row counts for and which exist in the DB):
list(set(tables_that_exist_in_DB) - (set(tables_that_exist_in_DB) - set(list_of_tables_you_want)))

"No such column" when checking for table column

I am creating a table to add data to a database but I am not sure where to create the column for 'emails'. My final aim for this is to be able to enter a username (email) and password and for it to be saved into a database but I am not sure how to do this. Here is my code currently:
import sqlite3
def save_to_database(my_stack, filename = 'stack_database.db'):
conn = sqlite3.connect(filename)
c = conn.cursor()
for row in c.execute('SELECT email FROM sqlite_master WHERE type="table"'):
if row != None:
c.execute("DROP TABLE emails")
c.execute("CREATE TABLE emails(email text,login_date text)")
...
The sqlite_master table does not have a column named email; the entire table structure is contained in the text in the sql column.
You could check for the table name itself (but note that if no row is found, no row is returned, not even an empty one, so it does not make sense to try to handle this with a for loop):
c.execute("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'emails'")
if not c.fetchall():
c.execute('DROP TABLE emails')
However, there is an easier method to ensure that a table is removed, regardless of its previous state:
c.execute('DROP TABLE IF EXISTS emails')

deleting data from multiple mysql tables using python

I want to delete some rows from almost 500 tables in mysql database using python.
I know that the query should be something like this:
DELETE FROM (table name)
[WHERE conditions] [ORDER BY ...] [LIMIT rows]
but I am not sure what should I use for the table name when I looping over the tables!
here is my loop
from mysql.connector import (connection)
# call databasecnx = connection.MySQLConnection(user='user', password='PW',host='127.0.0.1', database= 'database')
cursor = cnx.cursor()
cursor.execute("SHOW TABLES LIKE 'options_20%'")
table_names = [tables for (tables, ) in cursor]
for t in table_names:
cursor.execute("Delete table-refs WHERE Expiration = Datadate AND UnderlyingSymbol = 'SPY'")
cnx.commit()
I got an error:
You have an error in your SQL syntax; check the manual that...etc
Test with this.... You did'nt put from on your query statement.
for table in table_names:
cursor.execute("DELETE FROM"+ table +"WHERE Expiration = Datadate AND UnderlyingSymbol = 'SPY'")

Inserting data to table psycopg2 with no duplicates

I am new to psycopg2. I have to insert data into the table with no duplicates. So, first I created a temporary table where I dumped all the data. And then, I check and add the data to the actual table.
Here is the code till now:
for eachline in content:
pmid ,first_name, last_name,initial,article_title,journal,language = eachline.split("\t")
cur.execute ("INSERT INTO AUTHOR_PMID(pmid, Author_lastname, Author_firstname, Author_initial,Article_title)
SELECT DISTINCT (pmid, Author_lastname, Author_firstname, Author_initial,Article_title)
FROM AUTHOR_PMID WHERE NOT EXISTS (SELECT "X" FROM AUTHOR_pmid_temp
WHERE
AUTHOR_pmid_temp.pmid = AUTHOR_PMID.pmid
AND AUTHOR_pmid_temp.Author_lastname = AUTHOR_PMID.Author_lastname
AND AUTHOR_pmid_temp.Author_firstname = AUTHOR_PMID.Author_firstname
AND AUTHOR_pmid_temp.Author_initial = AUTHOR_PMID.Author_initial
AND AUTHOR_pmid_temp.Article_title = AUTHOR_PMID.Article_title);")
con.commit()
error: syntax error.
Where am i going wrong?
Try inserting query with triple quotes instead of single like below
for eachline in content:
pmid ,first_name, last_name,initial,article_title,journal,language = eachline.split("\t")
cur.execute ("""INSERT INTO AUTHOR_PMID(pmid, Author_lastname, Author_firstname, Author_initial,Article_title)
SELECT DISTINCT (pmid, Author_lastname, Author_firstname, Author_initial,Article_title)
FROM AUTHOR_PMID WHERE NOT EXISTS (SELECT "X" FROM AUTHOR_pmid_temp
WHERE
AUTHOR_pmid_temp.pmid = AUTHOR_PMID.pmid
AND AUTHOR_pmid_temp.Author_lastname = AUTHOR_PMID.Author_lastname
AND AUTHOR_pmid_temp.Author_firstname = AUTHOR_PMID.Author_firstname
AND AUTHOR_pmid_temp.Author_initial = AUTHOR_PMID.Author_initial
AND AUTHOR_pmid_temp.Article_title = AUTHOR_PMID.Article_title);""")
con.commit()
For more info, please check here !!!

Cannot copy one table to another?

I am using python to copy one table (dictionary) to another (origin_dictionary) in SQLite, and here is my code to this part:
def copyDictionaryToOrigin(self):
dropTableQueryStr = "DROP TABLE IF EXISTS origin_dictionary"
createTableQueryStr = "CREATE TABLE origin_dictionary (id INTEGER PRIMARY KEY AUTOINCREMENT, word TEXT, type TEXT)"
syncTableQueryStr = "INSERT INTO origin_dictionary (word, type) SELECT word, type FROM dictionary"
self.cur.execute(dropTableQueryStr)
self.cur.fetchone()
self.cur.execute(createTableQueryStr)
result = self.cur.fetchone()
self.cur.execute(syncTableQueryStr)
result = self.cur.fetchone()
With running this code, I can see a origin_dictionary table is created, but there is no data in the table. I could not find out the reason why the data didn't copy over to the new table. can someone please help me with this?
If you need to simply copy one table to another, why don't you use CREATE TABLE ... AS SELECT? Also, you need to commit() your statements.
Simply use code below, and it should work:
import sqlite3
conn = sqlite3.connect(example.db")
cur = conn.cursor()
cur.execute("DROP TABLE IF EXISTS origin_dictionary")
cur.execute("CREATE TABLE origin_dictionary AS SELECT * FROM dictionary")
conn.commit()
conn.close()

Categories