[英]how to add data to a table in sqlite 3 without going to a new line
I'm using sqlite to hold a series of data about stocks such as buy price, sell price, trend buy price etc. I add the prices and thats fine but when I add the trends it goes on to a new line, how do I add this in line with the other data?我正在使用 sqlite 来保存一系列关于股票的数据,例如买入价、卖出价、趋势买入价等。我添加了价格,那很好,但是当我添加趋势时,它会转到一个新行,我该怎么做添加这个与其他数据一致? I have tried using replace and update but it still adds on causing my other columns to return none which is causing problems with my code.我曾尝试使用替换和更新,但它仍然会导致我的其他列不返回任何内容,这会导致我的代码出现问题。
My code is as follows我的代码如下
def plot():
for a in stock:
count1 = 0
count2 = 1
index = 0
value = ["0"]
cursor.execute("""SELECT BuyPrice FROM """+a+"""""")
trend = []
rows = cursor.fetchall()
for row in rows:
print(row[0])
trend.append(float(row[0]))
index = index + 1
if index == len(web):
percentage = []
for i in range(len(web)-1):
change = trend[count2]-trend[count1]
print(trend[count2],trend[count1])
percent = (change/trend[count1])*100
print(percent)
percentage.append(percent)
count1 = count1 + 1
count2 = count2 + 1
for i in percentage:
print(i)
if i <= 0:
if i == 0:
value.append(0)
elif i <= -15:
value.append(-4)
elif i <= -10:
value.append(-3)
elif i <= -5:
value.append(-2)
elif i < 0:
value.append(-1)
else:
if i >= 15:
value.append(4)
elif i >= 10:
value.append(3)
elif i >= 5:
value.append(2)
elif i >= 0:
value.append(1)
for i in value:
t = str(i)
cursor.execute("""
REPLACE INTO """+ a +"""
(TrendBuy)
VALUES
("""+ t +""")
""")
this is what i mean if its a bit hard to understand这就是我的意思,如果它有点难以理解
SQLite's REPLACE INTO
needs a sort of "lookup column" to find a value and then replace the next corresponding value(s). SQLite 的REPLACE INTO
需要一种“查找列”来查找值,然后替换下一个相应的值。 If no matches exist, it will insert a new row.如果不存在匹配项,它将插入一个新行。 Consider incorporating BuySell in your action query.考虑在您的操作查询中加入BuySell 。 Also below formats SQL string and parameterizes query:同样在下面格式化 SQL 字符串并参数化查询:
cursor.execute("REPLACE INTO {} (BuySell, TrendBuy) VALUES (?, ?)".format(a), (row[0], t))
Alternatively, you could run an update query to avoid unintentional inserts and since no new BuySell values generate in looping logic.或者,您可以运行更新查询以避免无意插入,因为循环逻辑中不会生成新的BuySell值。
cursor.execute("UPDATE {} SET TrendBuy = ? WHERE BuySell = ?".format(a), (t, row[0]))
Aside - you seem to be storing each Stock in its own table.除了 - 您似乎将每个 Stock 存储在自己的表中。 For efficiency, scalability, and easier querying, consider one Stock table with a StockName or Symbol column.为了效率、可扩展性和更容易查询,考虑一个带有 StockName 或 Symbol 列的 Stock 表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.