SQLite3在python

时间:2018-08-17 17:14:10

标签: python sql sqlite

目前在为以下示例获取正确的SQL命令时遇到问题。

title  || stock        || ids         ||
----------------------------------------
item 1 || low-stock    || 883864-1096 ||
item 1 || out-of-stock || 883864-1097 ||
item 1 || out-of-stock || 883864-1098 ||
item 2 || out-of-stock || 883864-1456 ||

理想的是当我刷新API json时,如果在新请求中stock列中的值更改为该id,我希望将数据库更新为该新值,那么正确的SQL命令是什么?我尝试了以下操作: (用于刷新api和解析值的功能)

stocklevels = stocklevels
title= title
ids = ids
z = (title,)
c.execute('SELECT (title) FROM products WHERE title=?', z)
olderstock = old_stock = c.fetchone()

到目前为止,标题在哪里?命令抓住标题,但是,此后我迷路了。 感谢所有帮助


def loadjson():
   r = sess.get(url)
   data = r.json()
   x = data['skus']
   title = data['name']
   for l in x:
      stocklevels = l['stockLevel']
    ids = l['id']
    postit(stocklevels,title,ids)

def postit(stocklevels,title,ids):
  stocklevels = stocklevels
  title= title
  ids = ids
  conn = sqlite3.connect('ports.db')
  c = conn.cursor()
  c.execute("""CREATE TABLE IF NOT EXISTS products(title TEXT,stock TEXT,ids INTERGER UNIQUE)""")
  try:
     c.execute("""INSERT INTO products (title,stock,ids) VALUES (?,?,?)""", (title,stocklevels,ids,))
  except:
      try:
       # SQL command to select rows with same title.
       # If Stocklevels != Stock  AKA if new stocklevels parsed on r  doesnt  == stock column
       # c.execute Update stocklevel
       # else pass


    except Exception as e:
        print e

0 个答案:

没有答案