def complete_stage_purge_process(self, target_cnxn, stage_table, process_cd):
self.logger.debug(datetime.now())
self.logger.debug('complete_stage_purge_process')
delete_dt = datetime.today() - timedelta(days=30)
delete_dt = str(delete_dt)
run_pk_sql = "select run_pk from " + schemaName.PROCESS.value + "." + tableName.RUN_LOG.value + " where " + ProcessRunlog.ETL_MODIFIED_DTM.value + " <= '" + delete_dt + "' and " + \
ProcessRunlog.PROCESS_PK.value + " = (select " + ProcessRunlog.PROCESS_PK.value + " from " + schemaName.PROCESS.value + "." + \
tableName.PROCESS.value + " where " + \
Process.PROCESS_CODE.value + " = '" + process_cd + "') "
delete_sql = "delete from " + schemaName.STAGE.value + "." + stage_table + " where run_pk in (" + run_pk_sql + ")"
print(delete_sql)
print(target_cnxn)
try:
trgt_cursor = target_cnxn.cursor()
trgt_cursor.execute(delete_sql)
self.logger.debug("deletes processed successfully ")
except:
self.logger.exception('Error in processing deletes')
raise
But when added commit after trgt_cursor.execute(delete_sql) then below error is thrown. Could someone please help on how to handle this
AttributeError: 'psycopg2.extensions.cursor' object has no attribute 'commit'
these are the error messages I am receiving from the terminal when I run add_user.py
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
After some reading online I am led to believe that these errors are thrown when I am missing a matching parenthesis or quotation mark?I have looked through the file several times but since it has been a few hours, I might just be looking straight over the error. If it is not a missing quotation mark or parenthesis, please explain what might be causing the issue so I can fix it. Thanks to all who help!
import csv
import os
filename = "/root/Downloads/linux_users.csv"
def create_username(aList):
if (len(aList) < 2):
print("INVALID RECORD")
else:
tokens = list(aList[2])
username = tokens[0].lower() + aList[1].lower()
return username
with open(filename) as f_handle:
csv_reader = csv.reader(f_handle)
next(csv_reader)
userList = []
groupList = []
recordCopy = []
rep = 0
tick = True
for record in csv_reader:
if (record[7] == ""):
record.remove(record[7])
for data in record:
if (data == ""):
print("ERROR: MISSING INFORMATION. USER ID: " + record[0] + " HAS NOT BEEN ADDED")
tick = False
if ((record[6] not in groupList) and (tick == True)):
groupList.append(record[6])
if (tick == True):
username = create_username(record)
if (username in userList):
username += str(rep)
rep += 1
userList.append(username)
recordCopy.append(record)
else:
tick = True
for group in groupList:
os.system("groupadd " + group)
print("GROUP: " + group + " HAS SUCCESSFULLY BEEN CREATED")
i = 0
for record in recordCopy:
if (record[5] == "ceo"):
os.system("adduser " + userList[i] + " -g " + record[6] +
" -d /home/" + record[5] + " -s /bin/csh -u " + record[0] + " -p password")
os.system("passwd -e " + userList[i])
else:
os.system("adduser " + userList[i] + " -g " + record[6] +
" -d /home/" + record[5] + " -s /bin/bash -u " + record[0] + " -p password")
os.system("passwd -e " + userList[i])
i += 1
print("USER: " + record[0] + " HAS SUCCESSFULLY BEEN ADDED")
I am trying to update if record exist and insert if no record is found. using below code
for index, row in df.iterrows():
cols = "],[".join([str(i) for i in df.columns.tolist()])
cols = "([" + cols + "])"
ucols = "] = ?,[".join([str(i) for i in df.columns.tolist()])
ucols = "[" + ucols + "] = ?"
c.execute("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;")
c.execute("BEGIN TRANSACTION;")
c.execute("UPDATE " + tblname + " SET" + ucols + " WHERE [TESTNUMBER]=" + str(row['TESTNUMBER']) + " AND [ROWNUM] =" + str(row['ROWNUM']) + ";", tuple(row))
sqlr = "IF ##ROWCOUNT = 0 " \
"BEGIN " \
"INSERT INTO " + tblname + cols +" VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?); " \
"END " \
"COMMIT TRANSACTION;"
c.execute(sqlr, tuple(row))
getting below error message
{ProgrammingError}('25000', u'[25000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 2, current count = 1. (266) (SQLExecDirectW)')
no sure what i am doing wrong. appreciate your help
I have to connect the sql database to python so that I can add new user data via python.
I have tried the int conversion which puts me in further trouble of null types dataset.
i have tried the bracket placement. It doesn't work.
import os
import datetime
import pyodbc
import sqlite3
file_open = open("filenames.txt","r")
path = 'C:\\Users\\Timble\\Desktop\\Face_recognition\\user-id_filenames\\'
flag_loc = 1
flag_proc = 0
flag_vis = 0
file_read_lines = file_open.readlines()
for line in file_read_lines:
for character in line:
if character == "_":
details = line.split("_")
now = datetime.datetime.now()
name = line
print("name:", name) #col-3
print("type of name:", type(name))
user_id = int(details[1])
print("user_id:", details[1]) #col-2
print("type of user_id:", type(user_id))
date = details[2]
print("date on which photo is taken:", details[2]) #col-4
print("type of data:",type(details[2]))
now = now.strftime("%Y-%m-%d %H:%M:%S")
print("Current date and time: ", now) #col-6
print("type of current date:", type(now))
path2 = path + details[1]
if os.path.exists(path2):
print(path2)
else:
os.makedirs(path2)
#break
date = str(date)
print("type of date", type(date))
user_id = str(user_id)
print("type of user_id", type(user_id))
name = str(name)
print("type of name",type(name))
now = str(now)
print("type of now", type(now))
flag_loc = str(flag_loc)
print("type loc flag", type(flag_loc))
flag_proc = str(flag_proc)
print("type proc flag", type(flag_proc))
flag_vis = str(flag_vis)
print("type vis flag", type(flag_vis))
conn = pyodbc.connect(
"DRIVER={SQl Server};"
"server=DESKTOP-3ORBD3I\MSSQL;"
"database=TimbleSecuritySystem;"
"uid=sa;"
"pwd=P#ssword")
cur = conn.cursor()
sqlInsertUser = "Insert Into retraining (date, user_id, image_name,location_flagged, processing_flagged, insert_date, visible)Values( "+ date + " , " + user_id + " , " + name + " , " + flag_loc + " , " + flag_proc + " , " + now + " , " + flag_vis + " )"
print(sqlInsertUser)
cur.execute(sqlInsertUser)
conn.commit()
break
file_open.close()
The actual results tell me that print(sqlInsertUser) prints all the right values.
I am expecting the execute command to work and sql data added there.
This line is the problem:
sqlInsertUser = "Insert Into retraining (date, user_id, image_name,location_flagged, processing_flagged, insert_date, visible)Values( "+ date + " , " + user_id + " , " + name + " , " + flag_loc + " , " + flag_proc + " , " + now + " , " + flag_vis + " )"
For example if name contains some invalid characters e.g. "[" or "]", then the execute call fails because the name string is not properly enclosed. (It should be enclosed in a pair of quote)
You can use the parameter substitution support in pyodbc e.g.
sqlInsertUser = "Insert Into retraining (date, user_id,
image_name, location_flagged, processing_flagged, insert_date,
visible) Values (?,?,?,?,?,?,?)"
then run
cur.execute(sqlInsertUser, date, user_id, name, flag_loc, flag_proc, now, flag_vis)
(My sample code above is untested. You might need to fix some syntax errors)
For more details about the syntax see https://www.python.org/dev/peps/pep-0249/#paramstyle or https://github.com/mkleehammer/pyodbc/wiki/Cursor
I have a Executable named learn after compiling my program vv.c in linux.I am using Tkinter (python-Tk) for making My GUI.But When running my executable code.It reached a error message "sh :1 : learn :not found " where -t -c -b are parametes passing to executable.
else:
if self.binaryFeature == 0:
cmd = "learn" + "-t " + self.type + " -c "\
+ self.C + " " + self.e2.get() + " " + self.e3.get()
else:
cmd = "learn" + "-t " + self.type + " -c "\
+ self.C + " -b 1 " + self.e2.get()\
+ " " + self.e3.get()
output_string = commands.getoutput(cmd)
self.text.insert(INSERT, output_string+"\n","CprogramOutput")
is it any error in commands for executing ?please help me ..thanks
You need to put a space before "-t":
cmd = "learn" + " -t " + self.type + " -c "\
+ self.C + " " + self.e2.get() + " " + self.e3.get()
currently the shell evaluates the command as learn-t rather than learn -t.