Python 和 SQLquery 的新手。我正在尝试在 mssql 中查询现有表。第一个任务是显示表中的内容,我还想做的另一个任务是执行exists() 语句以检查表中的数据是否确实存在。到目前为止,这些是我的代码片段:
def dbhandler():
params = urllib.parse.quote_plus(db_string)
engine = db.create_engine("mssql+pyodbc:///?odbc_connect={}".format(params))
connection = engine.connect()
metadata = db.MetaData()
odfs_tester_history_files = db.Table('odfs_tester_history_files', metadata, autoload=True, autoload_with=engine)
odfs_tester_history = db.Table('odfs_tester_history', metadata, autoload=True, autoload_with=engine)
tables_dict = {'odfs_tester_history_files': {'engine': engine, 'connection': connection, 'table': odfs_tester_history_files}, 'odfs_tester_history': {'engine': engine, 'connection': connection, 'table': odfs_tester_history}}
#return {'engine': engine, 'connection': connection, 'table': odfs_tester_history_files}
return tables_dict
db_instance = dbhandler()
odfs_tabletest_dict = db_instance['odfs_tester_history']
foo_col = db.sql.column('CSV_FILENAME')
sql = db.select(odfs_tabletest_dict['table']).where(odfs_tabletest_dict['odfs_tester_history'].c.foo_col == '06_16_2020_FMGN519.csv')
df = pd.read_sql(sql, odfs_tabletest_dict['connection'])
print(df)
Run Code Online (Sandbox Code Playgroud)
但是,当我运行代码时,出现以下错误:sqlalchemy.exc.ArgumentError:select() 的列参数必须是 Python …
看一下这个:
#include <iostream> //input outut like cout, cin
#include <string> //strings
#include <cstdlib> //includes random num generators and basic c++ functions
#include <limits> //functions for min and max for data types
#include <vector>
#include <numeric> //sequences of values
#include <cmath> //math functions
#include <sstream> //string stream
#include <ctime> //time
#include <algorithm> //includes sort
#include <fstream> //alllows ofstream and ifstream for some reason? unicode vs ansi?
#include "Shape.h"
#include "Circle.h"
#include <functional> //allows you to use function poitner? <function>
#define PI …Run Code Online (Sandbox Code Playgroud) 嗨,我想知道是否可以在一个列表条件中运行两个不同的枚举条件:
mlist = ['a', 'boy 808', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'a', 'boy 808', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
loc = [(a,b), a for a, b for b, zip(x in enumerate(mlist), y in enumerate (mlist)) if '808' in x, if 'd' in y]
print(loc)
Run Code Online (Sandbox Code Playgroud)
这可能吗?我尝试了上面的方法,但出现了无效的语法错误:
File "c:\Users\sys_nsgprobeingestio\Documents\dozie\odfs\ctests.py", line 118
loc = [(a,b), a for a, b for b, zip(x in enumerate(mlist), y in enumerate (mlist)) if '808' in x, if 'd' in …Run Code Online (Sandbox Code Playgroud) 嗨,我有一个函数可以解析文件中的前 60 行,如果有完全空白的行,它应该提醒用户。然而,这些可以发生在这 60 行中的任何地方,所以我希望脚本解析整个 60 行,主要是因为我需要来自几行的数据来报告错误。我们可能想知道这些错误将来发生在哪里。我是这样写的:
def header_data(data):
dictionary = {}
datalen = len(data)
itrcntr = 0
try:
for line in data:
itrcntr += 1
if line.isspace():
raise Exception('File has badly formatted header data line(s)')
else:
linesplit = line.rstrip().split(":")
if len(linesplit) > 1:
dictionary[linesplit[0]] = linesplit[1].strip()
return dictionary
except Exception as e:
errmsg = str(e)
if itrcntr == datalen:
return (dictionary, errmsg)
else:
pass
Run Code Online (Sandbox Code Playgroud)
使用这个函数,我希望如果它看到 itrcntr 不等于 datalen,它会通过并返回到 try 块并移动到下一行。但这不会发生。相反,它跳出函数并在函数调用者的下一行继续。我怎样才能让它继续循环直到它到达循环的末尾,然后它将返回字典和错误消息?或者这不能用 try catch 异常处理程序来完成?