简体   繁体   中英

Can I pass sqlite3 table column name as search parameter?

My first help request on Stack Overflow. Python version 3.6.4

I have an issue with my code. It is meant to to return a price quote based on parameters entered - such as country, shipment weight and in what zone is the city in given country.

The table looks like something this(excel picture). Screenshot Image Is it possible to narrow down which column to use. Such as if search parameters are estonia and 3 kgs it nicely returns all three columns of zone1, zone2 and zone3:

What country? :estonia
weight?: 70
('estonia', '75', '10,83', '12,25', '14,43')

But can I pass in an argument based on user input to narrow down which zone column to use to get the value?

For example if a city is in zone1, take only values from column zone1 based on other search parameters

My robust code is as follows:

import sqlite3
import pandas as pd

conn = sqlite3.connect("transportHinnakiri.db")
c = conn.cursor()

df = pd.read_csv("baltikum.csv")
df.to_sql("baltikum", conn, if_exists="append", index=False)

input_country = input("What country? :").lower()
input_weight = int(input("Weight?: "))

def weight_category(input_weight):
    if input_weight < 1:
        return 1
    elif 1 < input_weight <= 3:
        return 3
    elif 3 < input_weight <= 10:
        return 10
    elif 10 < input_weight <= 20:
        return 20
    elif 20 < input_weight <= 31.5:
        return 31.5
    elif 31.5 < input_weight <= 50:
        return 50
    elif 50 < input_weight <= 75:
        return 75
    elif 75 < input_weight <= 100:
        return 100

result = weight_category(input_weight)

def get_post():
    c.execute("SELECT * FROM baltikum WHERE country=? AND weight=?",(input_country, result))
    row = c.fetchone()
    return row

result_final = get_post()

print(result_final)`

I'm not sure if this is what you are looking for but here is how I would do it. First get the city from the user:

city = input("input City: ").lower()

Then I would create some data structure, say dictionary, that holds all the zones and their cities:

zones = {'zone1': ['boston', 'new york'], 'zone2': ['san fransisco', 'los 
angeles']}

Then you can have a function that takes in a city and returns a zone

def get_zone(city):
    for zone in zones.keys():
        if city in zones[zone]:
            return zone
    return "Error city not found"

Of course you would have to do something to normalize user input. Then you could create a command string and execute the command

def execute_command(zone, weight, country):
    cmd = "SELECT {zone} FROM baltikum WHERE country={country} AND weight={weight}".format(zone = zone, country = country, weight = weight)
    data = c.execute(cmd)
    return data

Then you only get data from selected zone. Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM