python - 从表中选择将截断它

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

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

最佳答案

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

关于python - 从表中选择将截断它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53979986/

相关文章:

mysql - 在两台不同的电脑上处理同一个数据库

mysql - 使用 mysql_secure_installation 的目的是什么?

c# - 如何在不实际安装连接器的情况下通过 C# 使用 mysql 连接器连接到 mysql

python - 在 python 3.2 中阅读希伯来语

python - Emacs 设置行内(行尾)注释的间距

python - 没有名为 tensorflow.python.platform 的模块

mysql - mysql中这个标记 ":="是什么意思?

mysql - 无法找到请求的 .Net Framework 数据提供程序 - 6.5.5.0 MySQL 文件 + GoDaddy

python - mysql-connector python 'IN' 运算符存储为列表

python - 无法将 Pandas 导入 IPython