Python - pyodbc,向数据库添加唯一值
Python - pyodbc, adding unique values to a database
如标题所示,我正在尝试解析数据并插入到 postgresql 数据库中。
以下 2 个函数是我用来完成此任务的。如您所见,第一个函数接受输入并将其转换为列表列表。
for 循环遍历每个列表并将它们分配给 object。
您可以看到从 for 循环中调用“insertToDatabase()”并将 object 作为字符串向下传递。
连接到数据库,我首先 运行 查询数据库中的 return 所有 post_id,并存储到列表中。然后我尝试做“如果 id 不在结果中”,继续插入。
但是这不起作用,每次程序 运行s 时都会添加我的条目,从而使多个条目相同。我尝试了类似的尝试,试图仅获取最后发布的条目,按时间戳降序排列,并执行“if id != last_posted”,但这也不起作用。
必须有更好的方法来做到这一点。我在这里做错了什么?如果数据库中已经存在某个项目(例如“7229511362”)的'id',我想将其re-inserting跳过到数据库中并继续循环以检查所有结果。
代码:
def initialParse(results):
rList = [list(r.values()) for r in results]
print(rList)
for l in rList:
r_id = str(l[0])
r_name = str(l[2])
r_url = str(l[3])
r_datetime = str(l[4])
r_updated = str(l[5])
r_price = str(l[6])
r_where = str(l[7])
insertToDatabase(r_id, r_name, r_url, r_datetime, r_updated, r_price, r_where)
def insertToDatabase(id, name, url, date, updated, price, where):
global last_insert
cnxn = connectDb()
cursor = cnxn.cursor()
cursor.execute('select post_id from listings order by tstmp desc')
results = cursor.fetchall()
print(results)
try:
if id not in results:
Logger.writeAndPrintLine('Adding ' + id + ' to database...', 0)
cursor.execute("insert into listings (post_id, timestamp, url, subject, price, location, tstmp) values ("+id+", '"+date+"', '"+url+"', '"+name+"', '"+price+"', '"+where+"', (current_timestamp));")
print('inserted')
cursor.commit()
time.sleep(1)
except:
pass
cursor.close()
disconnectDb(cnxn)
转换为list of lists后的输入数据示例:
[['7230609794', None, '2004 Nissan Sentra sedan automatic runs excellent', 'https://monterey.craigslist.org/cto/d/salinas-2004-nissan-sentra-sedan/7230609794.html', '2020-11-13 17:35', '2020-11-13 17:35', ',850', 'Salinas', True, None, False], ['7230559009', None, '2006 mini cooper', 'https://monterey.craigslist.org/cto/d/king-city-2006-mini-cooper/7230559009.html', '2020-11-13 15:38', '2020-11-13 15:38', ',000', 'King city', True, None, False]]
什么的例子 cursor.fetchall() returns:
[('7229511362', ), ('7229470879', ), ('7229511362', ), ('7229697890', ), ('7229839309', ), ('7229957054', ), ('7230191646', ), ('7230491972', ), ('7230558061', ), ('7230559009', ), ('7230609794', ), ('7229470879', ), ('7229511362', ), ('7229697890', ), ('7229839309', ), ('7229957054', ), ('7230191646', ), ('7230491972', ), ('7230558061', ), ('7230559009', ), ('7230609794', ), ('7229470879', ), ('7229511362', ), ('7229697890', ), ('7229839309', ), ('7229957054', ), ('7230191646', ), ('7230491972', ), ('7230558061', ), ('7230559009', ), ('7230609794', ), ('7229470879', ), ('7229697890', ), ('7229839309', ), ('7229957054', ), ('7230191646', ), ('7230491972', ), ('7230558061', ), ('7230559009', ), ('7230609794', ), ('7229470879', ), ('7229697890', ), ('7229839309', ), ('7229957054', ), ('7230191646', ), ('7230491972', ), ('7230558061', ), ('7230559009', ), ('7230609794', )]
.fetchall()
returns pyodbc.Row
个对象的列表。如果您想使用 in
来测试是否返回了特定的 id 值,那么您首先需要将该 Row
对象列表转换为标量值列表:
crsr = cnxn.cursor()
rows = crsr.execute("""\
SELECT 'foo' AS col1
UNION ALL
SELECT 'bar' AS col1
""").fetchall()
print(rows) # [('foo', ), ('bar', )]
print("foo" in rows) # False
ids = [row[0] for row in rows]
print(ids) # ['foo', 'bar']
print("foo" in ids) # True
如标题所示,我正在尝试解析数据并插入到 postgresql 数据库中。
以下 2 个函数是我用来完成此任务的。如您所见,第一个函数接受输入并将其转换为列表列表。
for 循环遍历每个列表并将它们分配给 object。 您可以看到从 for 循环中调用“insertToDatabase()”并将 object 作为字符串向下传递。
连接到数据库,我首先 运行 查询数据库中的 return 所有 post_id,并存储到列表中。然后我尝试做“如果 id 不在结果中”,继续插入。
但是这不起作用,每次程序 运行s 时都会添加我的条目,从而使多个条目相同。我尝试了类似的尝试,试图仅获取最后发布的条目,按时间戳降序排列,并执行“if id != last_posted”,但这也不起作用。
必须有更好的方法来做到这一点。我在这里做错了什么?如果数据库中已经存在某个项目(例如“7229511362”)的'id',我想将其re-inserting跳过到数据库中并继续循环以检查所有结果。
代码:
def initialParse(results):
rList = [list(r.values()) for r in results]
print(rList)
for l in rList:
r_id = str(l[0])
r_name = str(l[2])
r_url = str(l[3])
r_datetime = str(l[4])
r_updated = str(l[5])
r_price = str(l[6])
r_where = str(l[7])
insertToDatabase(r_id, r_name, r_url, r_datetime, r_updated, r_price, r_where)
def insertToDatabase(id, name, url, date, updated, price, where):
global last_insert
cnxn = connectDb()
cursor = cnxn.cursor()
cursor.execute('select post_id from listings order by tstmp desc')
results = cursor.fetchall()
print(results)
try:
if id not in results:
Logger.writeAndPrintLine('Adding ' + id + ' to database...', 0)
cursor.execute("insert into listings (post_id, timestamp, url, subject, price, location, tstmp) values ("+id+", '"+date+"', '"+url+"', '"+name+"', '"+price+"', '"+where+"', (current_timestamp));")
print('inserted')
cursor.commit()
time.sleep(1)
except:
pass
cursor.close()
disconnectDb(cnxn)
转换为list of lists后的输入数据示例:
[['7230609794', None, '2004 Nissan Sentra sedan automatic runs excellent', 'https://monterey.craigslist.org/cto/d/salinas-2004-nissan-sentra-sedan/7230609794.html', '2020-11-13 17:35', '2020-11-13 17:35', ',850', 'Salinas', True, None, False], ['7230559009', None, '2006 mini cooper', 'https://monterey.craigslist.org/cto/d/king-city-2006-mini-cooper/7230559009.html', '2020-11-13 15:38', '2020-11-13 15:38', ',000', 'King city', True, None, False]]
什么的例子 cursor.fetchall() returns:
[('7229511362', ), ('7229470879', ), ('7229511362', ), ('7229697890', ), ('7229839309', ), ('7229957054', ), ('7230191646', ), ('7230491972', ), ('7230558061', ), ('7230559009', ), ('7230609794', ), ('7229470879', ), ('7229511362', ), ('7229697890', ), ('7229839309', ), ('7229957054', ), ('7230191646', ), ('7230491972', ), ('7230558061', ), ('7230559009', ), ('7230609794', ), ('7229470879', ), ('7229511362', ), ('7229697890', ), ('7229839309', ), ('7229957054', ), ('7230191646', ), ('7230491972', ), ('7230558061', ), ('7230559009', ), ('7230609794', ), ('7229470879', ), ('7229697890', ), ('7229839309', ), ('7229957054', ), ('7230191646', ), ('7230491972', ), ('7230558061', ), ('7230559009', ), ('7230609794', ), ('7229470879', ), ('7229697890', ), ('7229839309', ), ('7229957054', ), ('7230191646', ), ('7230491972', ), ('7230558061', ), ('7230559009', ), ('7230609794', )]
.fetchall()
returns pyodbc.Row
个对象的列表。如果您想使用 in
来测试是否返回了特定的 id 值,那么您首先需要将该 Row
对象列表转换为标量值列表:
crsr = cnxn.cursor()
rows = crsr.execute("""\
SELECT 'foo' AS col1
UNION ALL
SELECT 'bar' AS col1
""").fetchall()
print(rows) # [('foo', ), ('bar', )]
print("foo" in rows) # False
ids = [row[0] for row in rows]
print(ids) # ['foo', 'bar']
print("foo" in ids) # True