python連線各種資料庫的類方法

2021-08-28 23:38:27 字數 3397 閱讀 1002

連線postgresql資料庫

import psycopg2

# 用來運算元據庫的類

class gpcommand(object):

# 類的初始化

def __init__(self):

self.hostname = '10.1.2.xx'

self.username = '***'

self.password = '***'

self.database = '***'

def connectgp(self):

try:

#鏈結資料庫

#讀取配置利用connect鏈結資料庫

self.connect = psycopg2.connect( host=self.hostname, user=self.username, password=self.password, dbname=self.database )

#建立乙個新的cursor

self.cursor = self.connect.cursor()

print("connect gp successful."+'\n' + '資料庫連線成功')

return ('con_successful')

except psycopg2.error:

error = 'failed to setup postgres environment.\n'.format(sys.exc_info())

print('connect gp error.'+'\n' + '資料庫連線失敗')

return 'con_error'+ error

def inserttrans(self,trans_name,url_name):

pass

連線mysql資料庫

import pymysql

class mysqldb:

def __init__(self):

self.hostname = '1x.1.2.xx'

self.port = 3306

self.username = 'xx'

self.password = 'xx'

self.database = '***x'

def connectmysql(self):

try:

condb = pymysql.connect(host=self.hostname, port=self.port, user=self.username, passwd=self.password, db=self.database)

print("鏈結成功")

return condb

except exception as e:

print("連線異常", e)

# except exception:

# info = sys.exc_info()

# print("連線異常", info[1])

def inserttable(self):

# 獲取資料庫連線

condb = self.connectmysql()

# 使用cursor() 方法建立乙個游標物件 cursor

cursor = condb.cursor()

try:

# 執行sql語句

sql = "select * from cn_customer.bank"

cursor.execute(sql)

# 提交到資料庫執行

condb.commit()

except exception: # 方法一:捕獲所有異常

# 如果發生異常,則回滾

info = sys.exc_info()

print("發生異常", info[1])

condb.rollback()

finally:

# 最終關閉資料庫連線

condb.close()

連線sqlserver

import pymssql 

class sqlserverdb:

def __init__(self):

self.hostname = '***.1.2.***x'

self.username = '******'

self.password = '***xx'

self.database = '***xx'

def connectmysql(self):

try:

condb = pymssql.connect(host=self.hostname, user=self.username, password=self.password, database=self.database)

return condb

except exception as e:

print("連線異常", e)

#獲取的table_columns名稱,長度等資訊

def load_columns(self,table_name):

# 獲取資料庫連線

condb = self.connectmysql()

# 使用cursor() 方法建立乙個游標物件 cursor

cursor = condb.cursor()

fields =

sql = "select c.column_name ,coalesce(a.ispriamarykey,'n') as ispriamarykey,case when c.is_nullable = 'no' then 'not null' else '' end as is_nullable,c.data_type,c.character_maximum_length\

from [information_schema].[columns] c\

left join (select name,'y' as ispriamarykey from syscolumns where id=object_id('%s') and colid in(select keyno from sysindexkeys where id=object_id('%s'))) a \

on c.column_name = a.name where table_name = '%s'" % (table_name,table_name,table_name)

cursor.execute(sql)

table_info = cursor.fetchall()

for res in table_info:

#print(res)

desc =

#print(fields)

self.postgres_create(fields)

各種資料庫連線

mysql string driver com.mysql.jdbc.driver 驅動程式 string url jdbc mysql localhost 3306 db name 連線的url,db name為資料庫名 string username username 使用者名稱 string ...

各種資料庫連線

mysql string driver org.gjt.mm.mysql.driver 驅動程式 string url jdbc mysql localhost 3306 db name 連線的url,db name為資料庫名 string username username 使用者名稱 strin...

各種資料庫連線

連線mysql class.forname org.gjt.mm.mysql.driver newinstance 或者class.forname com.mysql.jdbc.driver string url jdbc mysql localhost mydb?user soft passwor...