代码几乎已经完成,但我在数据库时遇到了问题,并且当它为0时有余额
脚本只是连接到使用RPC调用的硬币钱包 它发送一个电话,然后查看余额并记录在数据库中,当有一个存款时,它发送另一个电话并注册新的余额
ERROR
[ERROR] Exception in command 'balance', (1048, "Column 'balance' cannot be null")
数据库代码
import pymysql.cursors
import warnings
from utils import parsing, output
config = parsing.parse_json('config.json')["mysql"]
host = config["db_host"]
try:
port = int(config["db_port"])
except KeyError:
port = 3306
db_user = config["db_user"]
db_pass = config["db_pass"]
db = config["db"]
connection = pymysql.connect(
host=host,
port=port,
user=db_user,
password=db_pass,
db=db)
cursor = connection.cursor(pymysql.cursors.DictCursor)
#cursor.execute("DROP DATABASE IF EXISTS {};".format(database))
#cursor.execute("CREATE DATABASE IF NOT EXISTS {};".format(database))
#conn.commit()
#cursor.execute("USE {};".format(database))
def run():
with warnings.catch_warnings():
warnings.simplefilter('ignore')
cursor.execute("""CREATE TABLE IF NOT EXISTS users (
snowflake_pk BIGINT UNSIGNED NOT NULL,
username VARCHAR(37) NOT NULL,
balance DECIMAL(20, 8) NOT NULL,
PRIMARY KEY (snowflake_pk)
)""")
cursor.execute("""CREATE TABLE IF NOT EXISTS deposit (
snowflake_fk BIGINT UNSIGNED NOT NULL,
address_from VARCHAR(34) NOT NULL,
address_to VARCHAR(34) NOT NULL,
amount DECIMAL(20, 8) NOT NULL,
FOREIGN KEY (snowflake_fk) REFERENCES users(snowflake_pk)
)""")
cursor.execute("""CREATE TABLE IF NOT EXISTS withdrawal (
snowflake_fk BIGINT UNSIGNED NOT NULL,
address_from VARCHAR(34) NOT NULL,
address_to VARCHAR(34) NOT NULL,
amount DECIMAL(20, 8) NOT NULL,
FOREIGN KEY (snowflake_fk) REFERENCES users(snowflake_pk)
)""")
cursor.execute("""CREATE TABLE IF NOT EXISTS tip (
snowflake_from_fk BIGINT UNSIGNED NOT NULL,
snowflake_to_fk BIGINT UNSIGNED NOT NULL,
ammount DECIMAL(20, 8) NOT NULL,
FOREIGN KEY (snowflake_from_fk) REFERENCES users(snowflake_pk),
FOREIGN KEY (snowflake_to_fk) REFERENCES users(snowflake_pk)
)""")
cursor.execute("""CREATE TABLE IF NOT EXISTS server (
server_id VARCHAR(18) NOT NULL,
enable_soak TINYINT(1) NOT NULL,
PRIMARY KEY (server_id)
)""")
cursor.execute("""CREATE TABLE IF NOT EXISTS channel (
channel_id VARCHAR(18) NOT NULL,
server_id VARCHAR(18) NOT NULL,
enabled TINYINT(1) NOT NULL,
FOREIGN KEY (server_id) REFERENCES server(server_id),
PRIMARY KEY (channel_id)
)""")
BALANCE代码
import discord
from discord.ext import commands
from utils import rpc_module, mysql_module
#result_set = database response with parameters from query
#db_bal = nomenclature for result_set["balance"]
#snowflake = snowflake from message context, identical to user in database
#wallet_bal = nomenclature for wallet reponse
rpc = rpc_module.Rpc()
mysql = mysql_module.Mysql()
class Balance:
def __init__(self, bot):
self.bot = bot
async def do_embed(self, name, db_bal):
# Simple embed function for displaying username and balance
embed = discord.Embed(colour=0xff0000)
embed.add_field(name="User", value=name.mention)
embed.add_field(name="Balance", value="{:.8f}
PHR".format(round(float(db_bal), 8)))
try:
await self.bot.say(embed=embed)
except discord.HTTPException:
await self.bot.say("I need the `Embed links` permission to send
this")
def __check_for_new_balance(self, user):
balance = rpc.getbalance(user.id)
mysql.set_balance(user, balance)
@commands.command(pass_context=True)
async def balance(self, ctx):
"""Display your balance"""
# Set important variables
snowflake = ctx.message.author.id
name = ctx.message.author.name
self.__check_for_new_balance(ctx.message.author)
# Check if user exists in db
result_set = mysql.check_for_user(name, snowflake)
# Execute and return SQL Query
result_set = mysql.get_user(snowflake)
await self.do_embed(ctx.message.author, result_set['balance'])
def setup(bot):
bot.add_cog(Balance(bot))