Python Forum
Connect to SQL and Select using Two Functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Connect to SQL and Select using Two Functions
#5
I'm trying to create the Class object, but I'm unsure how to pass the arguments compared to my function based code?

import pyodbc
import csv
import logging
import sys

server = ''
database = ''
username = ''
password = ''
table = ''

class SQLConnection(object):

    def __init__(self, driver, host, dbname, uname, pswd):
            connectstring = 'DRIVER=%s;SERVER=%S;PORT=1433;DATABASE=%s;UID=%s;PWD=%s;' % (driver, host, dbname, uname, pswd)
            self.connection = pyodbc.connect(connectstring)

    def getcursor(self):
        return self.connection.cursor()

    def select_from_database(self, tablename):
        cursor = self.getcursor()
        cursor.execute("SELECT * FROM %s " % tablename)
        row = cursor.fetchone()
        while row:
            print("%s %s" % (row[0], row[1]))
            row = cursor.fetchone()
        return row
Reply


Messages In This Thread
RE: Connect to SQL and Select using Two Functions - by phillyfa - Apr-09-2020, 06:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  SQL select join operation in python(Select.. join) pradeepkumarbe 1 2,273 Feb-14-2019, 08:34 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020