从表中选择是截断它

问题描述 投票:0回答:1

我编写的代码从表中提取数据并将其插入到字典中,然后截断表并在表中插入新值,但是当我运行代码时,它会立即截断表。

我试过删除截断代码,但它仍然截断表。

import ccxt
import mysql.connector

import os
import sys
import time
from collections import defaultdict
import json 
import requests
import logging
import telegram

cnx = mysql.connector.connect(user='root', password='',

host='localhost', database='coinbuys',

auth_plugin='mysql_native_password')

mycursor = cnx.cursor()

sql = """SELECT * FROM coinscores """
mycursor.execute(sql)
myresult = mycursor.fetchall()
for row in myresult:
coin_score_symbol = row[1]
coin_score_database_value = row[2]

print (coin_score_symbol)
print (coin_score_database_value)

#this prints nothing ^

sql = "INSERT INTO BUYWALLDATA3 (COINPAIR, price, size, volumethreshold) VALUES (%s, %s, %s, %s)"
            val = [
                (whichmarket[f], 
var_element_check[0], sum_array_check, volume_threshold)
                ]

            mycursor.executemany(sql, val)

            cnx.commit()

            print(mycursor.rowcount, "was inserted.")

for key in coins_with_buy_walls.keys():
sql = """SELECT * FROM BUYWALLDATA3 WHERE COINPAIR = '%s' """ % (key)
rows = 0
mycursor.execute(sql)
myresult = mycursor.fetchall()
for row in myresult:
    rows = rows + 1
    id_table = row[0]
    ticker = row[1]
    coin_price = row[2]
    size_order = row[3]
    volume_calculation = row[4]
    volume_calculation = row[4] + volume_calculation

#Here would be the truncate statement

for a in coin_score:
coin_final_score = coin_score[a]
sql = "INSERT INTO coinscores (symbol, score) VALUES (%s, %s)"              
val = [(a, coin_final_score)]
mycursor.executemany(sql, val)
cnx.commit()
print(mycursor.rowcount, "was inserted.")


# This is the code I used to create the coin scores table 
mycursor.execute("CREATE TABLE coinscores (id INT AUTO_INCREMENT PRIMARY KEY, symbol VARCHAR(255), score VARCHAR(255))")

我已经包含了所有的数据库语句,也许它们存在问题导致了这一点。我希望第一个数据库调用打印硬币分数中的所有值,然后擦除它并插入新数据。

python mysql mysql-connector
1个回答
0
投票

我在文件夹中有另一个文件,其中写有TRUNCATE行,并且由于某种原因我删除它时修复了问题。

© www.soinside.com 2019 - 2024. All rights reserved.