如何使用python中的sqlalchemy在数据库连接字符串上识别模式?
我有使用SQLalchemy连接到数据库的代码。 我只有一个模式(DBO),所以这很好。 现在我必须进行模式。我一直在尝试调整此代码以识别连接上的模式,但是我没有能够使其正常工作。谁能帮忙?
from datetime import datetime
from sqlalchemy import create_engine
import urllib
import time
import logging
server = "xxx"
username = "xxx"
password = "xxx"
database = "db_name"
driver = "{ODBC Driver 17 for SQL Server}"
#to measure total elapsed time of the script
start_time = round(time.time(),2)
#datetime manipulation to send to table
run_time_start = datetime.now().isoformat(" ", "seconds")
#Logging Information Configuration
logging.basicConfig(filename='ControlX.log', level=logging.DEBUG,
format='%(asctime)s++%(levelname)s++%(message)s')
#to measure db connection time
start_time_db = round(time.time(),2)
#Connection object for connection to sql database to send files
print( "Connecting to database..." )
params = urllib.parse.quote_plus(
'Driver=%s;' % driver +
'Server=tcp:%s,1433;' % server +
'Database=%s;' % database +
'Uid=%s;' % username +
'Pwd={%s};' % password +
'Encrypt=yes;' +
'TrustServerCertificate=no;' +
'Connection Timeout=60;')
conn_str = 'mssql+pyodbc:///?odbc_connect=' + params
engine = create_engine(conn_str,echo=False, fast_executemany=True)
logging.info("Database Connection++Database++{}".format(round(time.time()-start_time_db, 2)))
print("Database connection succeeded")
listLog=[]
#log information treatment
with open("ControlX.log",'r') as data_file:
for line in data_file:
line_list = line.rstrip('\n').split("++")
line_list[0] = line_list[0][:-4]
listLog.append(line_list)
for item in listLog:
item[4] = float(item[4])
#insert logging information about DATABASE CONNECTION ELAPSED TIME in database
engine.execute('INSERT INTO tableX (log_date, log_type, message, obj, total_time) VALUES (?, ?, ?, ?, ?)', listLog)
I have this code that connects to a database using SQLAlchemy.
I only had one schema (dbo), so this worked perfectly fine.
Now I have to schemas. I've been trying to adapt this piece of code to identify the schema on the connection, but I haven't been capable of making it work. Can anyone help?
from datetime import datetime
from sqlalchemy import create_engine
import urllib
import time
import logging
server = "xxx"
username = "xxx"
password = "xxx"
database = "db_name"
driver = "{ODBC Driver 17 for SQL Server}"
#to measure total elapsed time of the script
start_time = round(time.time(),2)
#datetime manipulation to send to table
run_time_start = datetime.now().isoformat(" ", "seconds")
#Logging Information Configuration
logging.basicConfig(filename='ControlX.log', level=logging.DEBUG,
format='%(asctime)s++%(levelname)s++%(message)s')
#to measure db connection time
start_time_db = round(time.time(),2)
#Connection object for connection to sql database to send files
print( "Connecting to database..." )
params = urllib.parse.quote_plus(
'Driver=%s;' % driver +
'Server=tcp:%s,1433;' % server +
'Database=%s;' % database +
'Uid=%s;' % username +
'Pwd={%s};' % password +
'Encrypt=yes;' +
'TrustServerCertificate=no;' +
'Connection Timeout=60;')
conn_str = 'mssql+pyodbc:///?odbc_connect=' + params
engine = create_engine(conn_str,echo=False, fast_executemany=True)
logging.info("Database Connection++Database++{}".format(round(time.time()-start_time_db, 2)))
print("Database connection succeeded")
listLog=[]
#log information treatment
with open("ControlX.log",'r') as data_file:
for line in data_file:
line_list = line.rstrip('\n').split("++")
line_list[0] = line_list[0][:-4]
listLog.append(line_list)
for item in listLog:
item[4] = float(item[4])
#insert logging information about DATABASE CONNECTION ELAPSED TIME in database
engine.execute('INSERT INTO tableX (log_date, log_type, message, obj, total_time) VALUES (?, ?, ?, ?, ?)', listLog)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过更改执行代码
或
You can try by changing your execute code
OR