I am trying to transfer data from one table to another using Python based on date range.
import anprint
import os
import sys
import re
import datetime
from mydb import cursor
try:
cam_name = sys.argv[1]
rec_date = sys.argv[2]
except:
print "Usage: %s cam_name date " % sys.argv[0]
sys.exit(1)
dbObj = anprint.ExcelDb()
# Get a list of the plates...
tablename = "customer_1.%s_anpr_vega" % cam_name
sql = """SELECT plate, datetime, id FROM %s WHERE DATE(datetime)="%s" """ % (tablename, rec_date)
cursor.execute(sql)
retval = cursor.fetchall()
for values in retval:
print values
(vrm, vrm_datetime, record_id) = values
dbObj.reconcile_plate(cam_name, vrm, vrm_datetime, record_id)
1) It bad paractice:
sql = """SELECT plate, datetime, id FROM %s WHERE DATE(datetime)="%s" """ % (tablename, rec_date)
cursor.execute(sql)
Do below:
sql = """SELECT plate, datetime, id FROM %s WHERE DATE(datetime)=?""" % tablename
cursor.execute(sql, [rec_date])
2) It's simple and not good solution to use sys.argv[n]
for get command line arguments like cam_name = sys.argv[1]
. Good way is to use argparse
for assign command line arguments to script.
3) It's good practice use between
operator in sql like this:
sql = "SELECT plate, datetime, id FROM " +tablename+ " WHERE datetime between ? and ?"
cursor.execute(sql, [start_date, end_date])
Furthermore read it for understanding parameters style SQL query.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.