简体   繁体   English

如何在不换行的情况下向sqlite 3中的表添加数据

[英]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.

相关问题 (Python,Windows)如何在不进入高级设置的情况下将新文件夹添加到 PATH? 从命令行? - (Python, Windows) How to add a new folder to PATH without going to advanced settings? From command line? 如何在不重新运行程序的情况下在 tkinter 表上显示新添加的数据? - How to display new add data on tkinter table without rerun the program? 如何在一行中输入列表的所有元素而不必在Python中使用新行 - How to enter all the elements of a list in a single line without going on to new line in Python 在reportlab中为表数据的变量内的值添加新行 - Add new line to the value inside a variable for table data in reportlab 添加新模型后。 如何在不丢失数据的情况下进行 Django 表迁移 - After Add New model.How to Django Table Migrate without losing data 数据逐个字母而不是逐字进入 SQLite3 表 - Data going into SQLite3 Table letter by letter not word by word 如何使用新数据添加更新表 - How to add update a table with new data 如何在 Python 中的 print() 中的 ',' 后添加没有空格的新行 - How to add the new line without space after ',' in print() in Python 如何在没有\\ n的情况下使用python在文本文件中添加新行 - how to add a new line in a text file using python without \n 如何在 python3 中不使用 \n 添加新行? - How to Add new line without using \n in python3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM