如何使用python中的sqlalchemy在数据库连接字符串上识别模式?

发布于 2025-02-11 19:00:02 字数 1860 浏览 1 评论 0原文

我有使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你再特殊 2025-02-18 19:00:02

您可以通过更改执行代码

engine.execute('INSERT INTO [database].[schemaName].tableX (log_date, log_type, message, obj, total_time) VALUES (?, ?, ?, ?, ?)', listLog)

   engine.execute('INSERT INTO [schemaName].tableX (log_date, log_type, message, obj, total_time) VALUES (?, ?, ?, ?, ?)', listLog)

You can try by changing your execute code

engine.execute('INSERT INTO [database].[schemaName].tableX (log_date, log_type, message, obj, total_time) VALUES (?, ?, ?, ?, ?)', listLog)

OR

   engine.execute('INSERT INTO [schemaName].tableX (log_date, log_type, message, obj, total_time) VALUES (?, ?, ?, ?, ?)', listLog)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文