简体   繁体   English

如何存储SQL查询的结果并使用它?

[英]How to store results from SQL query and use it?

I am trying to see whether the type is either a the letter "T" or between number 1-6 for the specific data entry found with name and password. 我正在尝试查看使用名称和密码找到的特定数据条目的类型是字母“ T”还是数字1-6之间。

  sql = 'SELECT type FROM table name WHERE name = "{}" AND password = "{}"'.format(username, password)

and then in psedocode i need something like: 然后在psedocode中,我需要类似:

if type =< 5:
      int()
elif type = "T"
      string()

I am using python 2.7 我正在使用python 2.7

Here is a full script that will query the mysql DB, and use your above-mentioned logic to print the values. 这是一个完整的脚本,它将查询mysql DB,并使用上述逻辑来打印值。 I've included the python code as well as the sample database code for this test case. 我已经包含了该测试案例的python代码以及示例数据库代码。 Let me know if you have any questions. 如果您有任何疑问,请告诉我。

Python 蟒蛇

import pymysql

connection = pymysql.connect(user='username', passwd='password',
                                 host='localhost',
                                 database='database')

cursor = connection.cursor()

NAME = 'Person_A'
PASSWORD = 'Password_A'
query = ("SELECT * FROM My_TABLE WHERE NAME = '%(1)s' AND PASSWORD = '%(2)s';" % {"1" : NAME, "2" : PASSWORD})


cursor.execute(query)

for item in cursor:

    type = item[0]

    if type.isdigit():
        if int(type) <6:
            print('Type is a number less than 6')
        else:
            print('Type is a number but not less than 6')
    else:
        if type == 'T':
            print('Type is a string == T')
        else:
            print('Type is a string but not the letter T')

MYSQL MYSQL

CREATE TABLE MY_TABLE (TYPE VARCHAR(255), NAME VARCHAR(255), PASSWORD VARCHAR(255));

INSERT INTO MY_TABLE VALUES ('T','Person_A','Password_A'),('4','Person_A','Password_A'),('7','Person_B','Password_B'),('t','Person_C','Password_C');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 SQL 查询的结果从 Databricks 导出到 Azure Data Lake Store - How to Export Results of a SQL Query from Databricks to Azure Data Lake Store 如何在python中将列名称作为字典的sql查询结果存储在其中? - How to store results of sql query with column name as dictionary in python? Python-使用游标执行SQL查询,如何确定SQL是否有&gt; 0个结果? - Python - use cursor to perform SQL query, how do I find out if there were > 0 results from SQL? 如何从 SQL 查询的结果创建值的字典? - How do I create a dict of values from the results of an SQL query? 如何存储查询结果(使用 Python) - How To Store Query Results (Using Python) 如何从GUI传递SQL查询并获取结果并使用python在表中显示结果 - How to pass SQL query from GUI and get the results and show results in a table using python 在另一个sql查询中的python程序中使用来自sql查询的结果 - using results from a sql query in a python program in another sql query 根据SQL查询结果创建字典 - Create a dictionary from SQL query results 如何使用Airflow存储SQL查询结果并在if else条件中使用结果? - How to store the SQL query result using Airflow and use the result in if else condition? 如何按组限制SQL查询的结果 - How to limit the results of a SQL query by group
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM