从表中选择被截断

时间:2018-12-30 17:47:31

标签: python mysql mysql-connector

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

我尝试删除截断代码,但是它仍然会截断表。

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))")

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

1 个答案:

答案 0 :(得分:0)

文件夹中有另一个文件,其中写入了TRUNCATE行,由于某种原因,当我删除该文件时,它可以解决问题。