AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-26782974

user26782974's questions

Martin Hope
NucyLoodle
Asked: 2024-09-05 03:30:05 +0800 CST

如何向具有 AUTO_INCREMENT 的表中插入数据?

  • 8

我正在尝试在新用户注册时将其插入到 MySQL 中的表中。我在 PythonAnywhere 上使用 FlaskApp。

这是我的疑问:

INSERT INTO user_profile (email, user_name, first_foo) VALUES (%s, %s, 0);

这是从我的 flask_app 代码运行的:

def connect_db(query, params):

    db_connection= MySQLdb.connect("<username>.mysql.eu.pythonanywhere-services.com","<username>","<password","<db_name>", cursorclass=MySQLdb.cursors.DictCursor)

    cursor=db_connection.cursor()
    cursor.execute(query, params)
    result = cursor.fetchone()

    return result

connect_db(query, (email, username,))

这是我的表结构:

+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| user_id      | int         | NO   | PRI | NULL    | auto_increment |
| email        | varchar(50) | YES  | UNI | NULL    |                |
| user_name    | varchar(15) | YES  | UNI | NULL    |                |
| first_foo    | tinyint(1)  | YES  |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+

不幸的是,我不断收到:

MySQLdb._exceptions.OperationalError: (1048, "Column 'user_id' cannot be null")

我尝试了几个查询,包括:

INSERT INTO user_profile (user_id, email, user_name, first_foo) VALUES (NULL, %s, %s, 0);
INSERT INTO user_profile (user_id, email, user_name, first_foo) VALUES (DEFAULT, %s, %s, 0);
INSERT INTO user_profile (user_id, email, user_name, first_foo) VALUES (0, %s, %s, 0);

但都返回相同的错误。

如果我在 Python Anywhere 上的 MySQL 控制台中运行第一个查询,则查询成功。

在此先感谢您的帮助。

python
  • 2 个回答
  • 47 Views
Martin Hope
NucyLoodle
Asked: 2024-08-31 00:20:33 +0800 CST

访问 pokeapi - 花费很长时间

  • 8

我想要从前 150 个生命值低于特定值的口袋妖怪中获取名称列表。

以下是我目前得到的信息:

def get_pokemon_with_similar_hp(max_hp):
    pokemon_names = []
    poke_data = []
    for i in range(1, 151):
        api_url = f"https://pokeapi.co/api/v2/pokemon/{i}"
        pokemon_response = requests.get(api_url)
        pokemon_data = pokemon_response.json()
        poke_data.append(pokemon_data)
    
    for j in range(0, 150):
        if poke_data[j]['stats'][0]['base_stat'] < max_hp:
                pokemon_names.append(poke_data[j]['name'])
    return pokemon_names   

这有效,并给了我想要的数据,但目前需要 8.49 秒来处理并发送到我的前端。有没有办法改进我的代码来加快速度?非常感谢 :)

python
  • 1 个回答
  • 26 Views
Martin Hope
user26782974
Asked: 2024-08-13 03:13:36 +0800 CST

Flask 和 Fetch() - 继续加载新的空白 html 而不是更新当前页面

  • 5

我正在尝试使用 Python 和 Flask 编写基于文本的 Pokemon 游戏。我希望用户能够通过单击网页上的按钮来选择他们的 Pokemon,这个 user_pokemon_name 变量将被发送到后端,然后使用有关 Pokemon 的各种统计数据更新网页。然后游戏将继续,并显示新按钮和新消息。

我面临的问题是,一旦用户单击按钮,就会在相同的 URL 处加载一个新的 HTML 页面,并在正文中包含响应。

这是我的 app.py 代码:

from flask import Flask, render_template, request

app = Flask(__name__ ,
            static_url_path='',
            static_folder='../frontend/static',
            template_folder='../frontend/templates')

@app.route("/battle")
def battle_page():
    return render_template('battle.html')

@app.route("/battle", methods=['POST'])
def get_user_choice():
    user_pokemon_name = request.form['userPokemonChoice']
    print(user_pokemon_name)
    user_choice = f"You have chosen {user_pokemon_name.capitalize()}."
    return user_choice

这是我的 battle.html 代码:

<body>
        <section id= "userPokemonChoice">
            <p>Now, which Pokemon do you want? The three available Pokemon are Bulbasaur, Charmander, and Squirtle.</p>
            <form class = "userinput" method ="POST" action="/battle" id = "form"> 
                <button type = "submit" name = "userPokemonChoice" value="bulbasaur" id="choosePokemon" onclick="getUserChoice()">Bulbasaur</button>
                <button type = "submit" name = "userPokemonChoice" value="charmander" id="choosePokemon" onclick="getUserChoice()">Charmander</button>
                <button type = "submit" name = "userPokemonChoice" value="squirtle" id="choosePokemon" onclick="getUserChoice()">Squirtle</button>    
            </form>
        </section>

        <section id="stats"></section>

这是我的 battle.js 代码

// const form = document.getElementById('form')
// form.addEventListener('submit', (e) => {
//     e.preventDefault()
//     
// })


function getUserChoice() {
    const url = 'http://127.0.0.1:5001/battle'
    fetch(url)
    .then(response => response)  
    .then(data => {
        
        console.log(data.text);
        document.getElementById("stats").innerHTML = data
    })    
}

当我按原样运行代码时,显示以下屏幕:

带有响应的空白 HTML 页面

如果我取消注释表单事件监听器,我会收到一个 [object Response],我希望看到响应: object Response

任何帮助都将非常感激--我一直为此而烦恼!

flask
  • 1 个回答
  • 28 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve