def main(argv): """Main entry function for CLI script, should be passed CLI args tuple, normally from sys.argv """ # Get CLI options try: opts, args = getopt.getopt( argv, "d:", [ "dsn=", ] ) except getopt.GetoptError: error("Unknown options", True, USAGE_MESSAGE) if not opts: usage(USAGE_MESSAGE) sys.exit(0) dsn = None # Parse CLI options for opt, arg in opts: if opt in ("-d", "--dsn"): dsn = arg if not dsn: error("Please provide a DSN", True, USAGE_MESSAGE) # Setup cursor conn = pyodbc.connect(dsn) u = ODBCUtility() u.cursor = u.system_cursor = conn.cursor() u.cmdloop()
def main(argv): """Main entry function for CLI script, should be passed CLI args tuple, normally from sys.argv """ # Get CLI options try: opts, args = getopt.getopt( argv, "f:", [ "file=", ] ) except getopt.GetoptError: error("Unknown options", True, USAGE_MESSAGE) if not opts: usage(USAGE_MESSAGE) sys.exit(0) filepath = None # Parse CLI options for opt, arg in opts: if opt in ("-f", "--file"): filepath = arg if not filepath: error("Please provide a DB filepath", True, USAGE_MESSAGE) # Setup cursor conn = sqlite3.connect(filepath) u = LiteUtility() u.cursor = u.system_cursor = conn.cursor() u.cmdloop()
def get_query(self, sql): """Helper method to get results for an SQL query, via the internal DBAPI cursor. """ try: data = self.cursor.execute(sql).fetchall() except Exception, e: data = tuple() error(e, False)
def connect(self, filepath): try: conn = sqlite3.connect(filepath) self.system_cursor = self.cursor = conn.cursor() except: error(e, False) return False else: return True
def connect(self, schema): self.dsn['schema'] = schema try: conn = pyodbc.connect(self.query, **self.dsn) self.cursor = self.system_cursor = conn.cursor() except: error(e, False) return False else: return True
def connect(self, schema): dsn = dict(self.dsn) parts = line.split(' ') if len(parts) > 1: dsn['DATABASE'] = parts[1] dsn['ProfileGuid'] = parts[0] else: dsn['DATABASE'] = line.strip() try: conn = pyodbc.connect(**dsn) self.cursor = conn.cursor() except: error(e, False) return False else: self.dsn = dsn return True
def main(argv=None): """Main entry function for CLI script, should be passed CLI args tuple, normally from sys.argv """ if argv is None: argv = sys.argv[1:] # Get CLI options try: opts, args = getopt.getopt( argv, "p:t:h:P:d:u:k:", [ "profile=", "template=", "host=", "port=", "systemdsn=", "username="******"password="******"Unknown options", True, USAGE_MESSAGE) profile = None template = None host = None port = None system_dsn = None username = None password = None dsn = {} # Parse CLI options for opt, arg in opts: if opt in ("-p", "--profile"): profile = arg elif opt in ("-t", "--template"): template = arg elif opt in ("-h", "--host"): host = arg elif opt in ("-P", "--port"): port = arg elif opt in ("-d", "--systemdsn"): system_dsn = arg elif opt in ("-u", "--username"): username = arg elif opt in ("-k", "--password"): password = arg if not profile: error("Must have a profile GUID, -p", True, USAGE_MESSAGE) if not template: error("Must have a template/schema, -t", True, USAGE_MESSAGE) if not host: error("Must have a host, -h", True, USAGE_MESSAGE) if not system_dsn: error("Must have a predefined system DSN, -d", True, USAGE_MESSAGE) dsn['DSN'] = system_dsn dsn['ProfileGuid'] = profile dsn['DATABASE'] = template dsn['SERVER'] = host if port: dsn['PORT'] = port else: dsn['PORT'] = '80' if password: dsn['Password'] = password if username: dsn['User ID'] = dsn['UID'] = user dsn['SSL'] = '0' dsn['AccountId'] = '1' # Setup cursor u = WTUtility() u.dsn = dict(dsn) conn = pyodbc.connect(**dsn) u.cursor = conn.cursor() del dsn['ProfileGuid'] dsn['Profile'] = 'WTSystem' dsn['DATABASE'] = 'WTSystem' conn = pyodbc.connect(**dsn) u.system_cursor = conn.cursor() u.cmdloop()