Python Mysql在select完成后获取变量值

时间:2018-02-08 21:00:08

标签: python mysql python-2.7 mysql-python

所以我想在python中完成select之后做一个IF语句。很难解释,更好地提供一个例子:

<select id="edit-timezone--2" name="timezone" class="form-select">
    <option value="Africa/Abidjan">Africa/Abidjan: Thursday, February 8, 2018 - 20:45 +0000</option>
    <option value="Africa/Accra">Africa/Accra: Thursday, February 8, 2018 - 20:45 +0000</option>
    <option value="Africa/Addis_Ababa">Africa/Addis Ababa: Thursday, February 8, 2018 - 23:45 +0300</option>
    <option value="Africa/Freetown">Africa/Freetown: Thursday, February 8, 2018 - 20:45 +0000</option>
    <option value="Africa/Gaborone">Africa/Gaborone: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Africa/Harare">Africa/Harare: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Africa/Johannesburg">Africa/Johannesburg: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Africa/Juba">Africa/Juba: Thursday, February 8, 2018 - 23:45 +0300</option>
    <option value="Africa/Kampala">Africa/Kampala: Thursday, February 8, 2018 - 23:45 +0300</option>
    <option value="Africa/Khartoum">Africa/Khartoum: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Africa/Kigali">Africa/Kigali: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Africa/Kinshasa">Africa/Kinshasa: Thursday, February 8, 2018 - 21:45 +0100</option>
    <option value="Africa/Lagos">Africa/Lagos: Thursday, February 8, 2018 - 21:45 +0100</option>
    <option value="Africa/Libreville">Africa/Libreville: Thursday, February 8, 2018 - 21:45 +0100</option>
    <option value="Africa/Lome">Africa/Lome: Thursday, February 8, 2018 - 20:45 +0000</option>
    <option value="Africa/Luanda">Africa/Luanda: Thursday, February 8, 2018 - 21:45 +0100</option>
    <option value="Africa/Lubumbashi">Africa/Lubumbashi: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Africa/Lusaka">Africa/Lusaka: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Africa/Malabo">Africa/Malabo: Thursday, February 8, 2018 - 21:45 +0100</option>
    <option value="Africa/Maputo">Africa/Maputo: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Africa/Maseru">Africa/Maseru: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Africa/Mbabane">Africa/Mbabane: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Africa/Mogadishu">Africa/Mogadishu: Thursday, February 8, 2018 - 23:45 +0300</option>
    <option value="Africa/Monrovia">Africa/Monrovia: Thursday, February 8, 2018 - 20:45 +0000</option>
    <option value="Africa/Nairobi">Africa/Nairobi: Thursday, February 8, 2018 - 23:45 +0300</option>
    <option value="America/Barbados">America/Barbados: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/Belem">America/Belem: Thursday, February 8, 2018 - 17:45 -0300</option>
    <option value="America/Belize">America/Belize: Thursday, February 8, 2018 - 14:45 -0600</option>
    <option value="America/Blanc-Sablon">America/Blanc-Sablon: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/Boa_Vista">America/Boa Vista: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/Bogota">America/Bogota: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Boise">America/Boise: Thursday, February 8, 2018 - 13:45 -0700</option>
    <option value="America/Cambridge_Bay">America/Cambridge Bay: Thursday, February 8, 2018 - 13:45 -0700</option>
    <option value="America/Campo_Grande">America/Campo Grande: Thursday, February 8, 2018 - 17:45 -0300</option>
    <option value="America/Cancun">America/Cancun: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Caracas">America/Caracas: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/Cayenne">America/Cayenne: Thursday, February 8, 2018 - 17:45 -0300</option>
    <option value="America/Cayman">America/Cayman: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Chicago">America/Chicago: Thursday, February 8, 2018 - 14:45 -0600</option>
    <option value="America/Chihuahua">America/Chihuahua: Thursday, February 8, 2018 - 13:45 -0700</option>
    <option value="America/Costa_Rica">America/Costa Rica: Thursday, February 8, 2018 - 14:45 -0600</option>
    <option value="America/Creston">America/Creston: Thursday, February 8, 2018 - 13:45 -0700</option>
    <option value="America/Cuiaba">America/Cuiaba: Thursday, February 8, 2018 - 17:45 -0300</option>
    <option value="America/Curacao">America/Curacao: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/Danmarkshavn">America/Danmarkshavn: Thursday, February 8, 2018 - 20:45 +0000</option>
    <option value="America/Dawson_Creek">America/Dawson Creek: Thursday, February 8, 2018 - 13:45 -0700</option>
    <option value="America/Dawson">America/Dawson: Thursday, February 8, 2018 - 12:45 -0800</option>
    <option value="America/Denver">America/Denver: Thursday, February 8, 2018 - 13:45 -0700</option>
    <option value="America/Detroit">America/Detroit: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Dominica">America/Dominica: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/Edmonton">America/Edmonton: Thursday, February 8, 2018 - 13:45 -0700</option>
    <option value="America/Eirunepe">America/Eirunepe: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/El_Salvador">America/El Salvador: Thursday, February 8, 2018 - 14:45 -0600</option>
    <option value="America/Fort_Nelson">America/Fort Nelson: Thursday, February 8, 2018 - 13:45 -0700</option>
    <option value="America/Fortaleza">America/Fortaleza: Thursday, February 8, 2018 - 17:45 -0300</option>
    <option value="America/Glace_Bay">America/Glace Bay: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/Godthab">America/Godthab: Thursday, February 8, 2018 - 17:45 -0300</option>
    <option value="America/Goose_Bay">America/Goose Bay: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/Grand_Turk">America/Grand Turk: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/Grenada">America/Grenada: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/Guadeloupe">America/Guadeloupe: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/Guatemala">America/Guatemala: Thursday, February 8, 2018 - 14:45 -0600</option>
    <option value="America/Guayaquil">America/Guayaquil: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Guyana">America/Guyana: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/Halifax">America/Halifax: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/Havana">America/Havana: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Hermosillo">America/Hermosillo: Thursday, February 8, 2018 - 13:45 -0700</option>
    <option value="America/Indiana/Indianapolis">America/Indiana/Indianapolis: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Indiana/Knox">America/Indiana/Knox: Thursday, February 8, 2018 - 14:45 -0600</option>
    <option value="America/Indiana/Marengo">America/Indiana/Marengo: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Indiana/Petersburg">America/Indiana/Petersburg: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Indiana/Tell_City">America/Indiana/Tell City: Thursday, February 8, 2018 - 14:45 -0600</option>
    <option value="America/Indiana/Vevay">America/Indiana/Vevay: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Indiana/Vincennes">America/Indiana/Vincennes: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Indiana/Winamac">America/Indiana/Winamac: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Inuvik">America/Inuvik: Thursday, February 8, 2018 - 13:45 -0700</option>
    <option value="America/Iqaluit">America/Iqaluit: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Jamaica">America/Jamaica: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Juneau">America/Juneau: Thursday, February 8, 2018 - 11:45 -0900</option>
    <option value="America/Kentucky/Louisville">America/Kentucky/Louisville: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Kentucky/Monticello">America/Kentucky/Monticello: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Kralendijk">America/Kralendijk: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/La_Paz">America/La Paz: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/Nassau">America/Nassau: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/New_York">America/New York: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Nipigon">America/Nipigon: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Nome">America/Nome: Thursday, February 8, 2018 - 11:45 -0900</option>
    <option value="America/Noronha">America/Noronha: Thursday, February 8, 2018 - 18:45 -0200</option>
    <option value="America/Punta_Arenas">America/Punta Arenas: Thursday, February 8, 2018 - 17:45 -0300</option>
    <option value="America/Rainy_River">America/Rainy River: Thursday, February 8, 2018 - 14:45 -0600</option>
    <option value="America/Rankin_Inlet">America/Rankin Inlet: Thursday, February 8, 2018 - 14:45 -0600</option>
    <option value="America/Recife">America/Recife: Thursday, February 8, 2018 - 17:45 -0300</option>
    <option value="America/Regina">America/Regina: Thursday, February 8, 2018 - 14:45 -0600</option>
    <option value="America/Resolute">America/Resolute: Thursday, February 8, 2018 - 14:45 -0600</option>
    <option value="America/Rio_Branco">America/Rio Branco: Thursday, February 8, 2018 - 15:45 -0500</option>
    <option value="America/Santarem">America/Santarem: Thursday, February 8, 2018 - 17:45 -0300</option>
    <option value="America/Santiago">America/Santiago: Thursday, February 8, 2018 - 17:45 -0300</option>
    <option value="America/Santo_Domingo">America/Santo Domingo: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/Sao_Paulo">America/Sao Paulo: Thursday, February 8, 2018 - 18:45 -0200</option>
    <option value="America/Scoresbysund">America/Scoresbysund: Thursday, February 8, 2018 - 19:45 -0100</option>
    <option value="America/Sitka">America/Sitka: Thursday, February 8, 2018 - 11:45 -0900</option>
    <option value="America/St_Barthelemy">America/St Barthelemy: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/St_Johns">America/St Johns: Thursday, February 8, 2018 - 17:15 -0330</option>
    <option value="America/St_Kitts">America/St Kitts: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/St_Lucia">America/St Lucia: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/St_Thomas">America/St Thomas: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/St_Vincent">America/St Vincent: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="America/Swift_Current">America/Swift Current: Thursday, February 8, 2018 - 14:45 -0600</option>
    <option value="America/Tegucigalpa">America/Tegucigalpa: Thursday, February 8, 2018 - 14:45 -0600</option>
    <option value="America/Thule">America/Thule: Thursday, February 8, 2018 - 16:45 -0400</option>
    <option value="Antarctica/Vostok">Antarctica/Vostok: Friday, February 9, 2018 - 02:45 +0600</option>
    <option value="Arctic/Longyearbyen">Arctic/Longyearbyen: Thursday, February 8, 2018 - 21:45 +0100</option>
    <option value="Asia/Aden">Asia/Aden: Thursday, February 8, 2018 - 23:45 +0300</option>
    <option value="Asia/Almaty">Asia/Almaty: Friday, February 9, 2018 - 02:45 +0600</option>
    <option value="Asia/Amman">Asia/Amman: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Asia/Anadyr">Asia/Anadyr: Friday, February 9, 2018 - 08:45 +1200</option>
    <option value="Asia/Irkutsk">Asia/Irkutsk: Friday, February 9, 2018 - 04:45 +0800</option>
    <option value="Asia/Jakarta">Asia/Jakarta: Friday, February 9, 2018 - 03:45 +0700</option>
    <option value="Asia/Jayapura">Asia/Jayapura: Friday, February 9, 2018 - 05:45 +0900</option>
    <option value="Asia/Jerusalem">Asia/Jerusalem: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Asia/Kabul">Asia/Kabul: Friday, February 9, 2018 - 01:15 +0430</option>
    <option value="Asia/Kamchatka">Asia/Kamchatka: Friday, February 9, 2018 - 08:45 +1200</option>
    <option value="Asia/Karachi">Asia/Karachi: Friday, February 9, 2018 - 01:45 +0500</option>
    <option value="Asia/Kathmandu">Asia/Kathmandu: Friday, February 9, 2018 - 02:30 +0545</option>
    <option value="Asia/Khandyga">Asia/Khandyga: Friday, February 9, 2018 - 05:45 +0900</option>
    <option value="Asia/Sakhalin">Asia/Sakhalin: Friday, February 9, 2018 - 07:45 +1100</option>
    <option value="Asia/Samarkand">Asia/Samarkand: Friday, February 9, 2018 - 01:45 +0500</option>
    <option value="Asia/Seoul">Asia/Seoul: Friday, February 9, 2018 - 05:45 +0900</option>
    <option value="Asia/Shanghai">Asia/Shanghai: Friday, February 9, 2018 - 04:45 +0800</option>
    <option value="Asia/Singapore">Asia/Singapore: Friday, February 9, 2018 - 04:45 +0800</option>
    <option value="Asia/Srednekolymsk">Asia/Srednekolymsk: Friday, February 9, 2018 - 07:45 +1100</option>
    <option value="Asia/Taipei">Asia/Taipei: Friday, February 9, 2018 - 04:45 +0800</option>
    <option value="Asia/Tashkent">Asia/Tashkent: Friday, February 9, 2018 - 01:45 +0500</option>
    <option value="Asia/Tbilisi">Asia/Tbilisi: Friday, February 9, 2018 - 00:45 +0400</option>
    <option value="Asia/Tehran">Asia/Tehran: Friday, February 9, 2018 - 00:15 +0330</option>
    <option value="Asia/Thimphu">Asia/Thimphu: Friday, February 9, 2018 - 02:45 +0600</option>
    <option value="Asia/Tokyo">Asia/Tokyo: Friday, February 9, 2018 - 05:45 +0900</option>
    <option value="Asia/Tomsk">Asia/Tomsk: Friday, February 9, 2018 - 03:45 +0700</option>
    <option value="Australia/Eucla">Australia/Eucla: Friday, February 9, 2018 - 05:30 +0845</option>
    <option value="Australia/Hobart">Australia/Hobart: Friday, February 9, 2018 - 07:45 +1100</option>
    <option value="Australia/Lindeman">Australia/Lindeman: Friday, February 9, 2018 - 06:45 +1000</option>
    <option value="Australia/Lord_Howe">Australia/Lord Howe: Friday, February 9, 2018 - 07:45 +1100</option>
    <option value="Australia/Melbourne">Australia/Melbourne: Friday, February 9, 2018 - 07:45 +1100</option>
    <option value="Australia/Perth">Australia/Perth: Friday, February 9, 2018 - 04:45 +0800</option>
    <option value="Australia/Sydney">Australia/Sydney: Friday, February 9, 2018 - 07:45 +1100</option>
    <option value="Europe/Chisinau">Europe/Chisinau: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Europe/Copenhagen">Europe/Copenhagen: Thursday, February 8, 2018 - 21:45 +0100</option>
    <option value="Europe/Dublin">Europe/Dublin: Thursday, February 8, 2018 - 20:45 +0000</option>
    <option value="Europe/Gibraltar">Europe/Gibraltar: Thursday, February 8, 2018 - 21:45 +0100</option>
    <option value="Europe/Guernsey">Europe/Guernsey: Thursday, February 8, 2018 - 20:45 +0000</option>
    <option value="Europe/Helsinki">Europe/Helsinki: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Europe/Isle_of_Man">Europe/Isle of Man: Thursday, February 8, 2018 - 20:45 +0000</option>
    <option value="Europe/Istanbul">Europe/Istanbul: Thursday, February 8, 2018 - 23:45 +0300</option>
    <option value="Europe/Jersey">Europe/Jersey: Thursday, February 8, 2018 - 20:45 +0000</option>
    <option value="Europe/Kaliningrad">Europe/Kaliningrad: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Europe/Kiev">Europe/Kiev: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Europe/Kirov">Europe/Kirov: Thursday, February 8, 2018 - 23:45 +0300</option>
    <option value="Europe/Lisbon">Europe/Lisbon: Thursday, February 8, 2018 - 20:45 +0000</option>
    <option value="Europe/Ljubljana">Europe/Ljubljana: Thursday, February 8, 2018 - 21:45 +0100</option>
    <option value="Europe/Tirane">Europe/Tirane: Thursday, February 8, 2018 - 21:45 +0100</option>
    <option value="Europe/Ulyanovsk">Europe/Ulyanovsk: Friday, February 9, 2018 - 00:45 +0400</option>
    <option value="Europe/Uzhgorod">Europe/Uzhgorod: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Europe/Vaduz">Europe/Vaduz: Thursday, February 8, 2018 - 21:45 +0100</option>
    <option value="Europe/Vatican">Europe/Vatican: Thursday, February 8, 2018 - 21:45 +0100</option>
    <option value="Europe/Vienna">Europe/Vienna: Thursday, February 8, 2018 - 21:45 +0100</option>
    <option value="Europe/Vilnius">Europe/Vilnius: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Europe/Volgograd">Europe/Volgograd: Thursday, February 8, 2018 - 23:45 +0300</option>
    <option value="Europe/Warsaw">Europe/Warsaw: Thursday, February 8, 2018 - 21:45 +0100</option>
    <option value="Europe/Zagreb">Europe/Zagreb: Thursday, February 8, 2018 - 21:45 +0100</option>
    <option value="Europe/Zaporozhye">Europe/Zaporozhye: Thursday, February 8, 2018 - 22:45 +0200</option>
    <option value="Europe/Zurich">Europe/Zurich: Thursday, February 8, 2018 - 21:45 +0100</option>
    <option value="Indian/Antananarivo">Indian/Antananarivo: Thursday, February 8, 2018 - 23:45 +0300</option>
    <option value="Indian/Chagos">Indian/Chagos: Friday, February 9, 2018 - 02:45 +0600</option>
    <option value="Indian/Christmas">Indian/Christmas: Friday, February 9, 2018 - 03:45 +0700</option>
    <option value="Indian/Cocos">Indian/Cocos: Friday, February 9, 2018 - 03:15 +0630</option>
    <option value="Indian/Comoro">Indian/Comoro: Thursday, February 8, 2018 - 23:45 +0300</option>
    <option value="Indian/Kerguelen">Indian/Kerguelen: Friday, February 9, 2018 - 01:45 +0500</option>
    <option value="Indian/Mahe">Indian/Mahe: Friday, February 9, 2018 - 00:45 +0400</option>
    <option value="Indian/Maldives">Indian/Maldives: Friday, February 9, 2018 - 01:45 +0500</option>
    <option value="Indian/Mauritius">Indian/Mauritius: Friday, February 9, 2018 - 00:45 +0400</option>
    <option value="Indian/Mayotte">Indian/Mayotte: Thursday, February 8, 2018 - 23:45 +0300</option>
    <option value="Indian/Reunion">Indian/Reunion: Friday, February 9, 2018 - 00:45 +0400</option>
    <option value="Pacific/Apia">Pacific/Apia: Friday, February 9, 2018 - 10:45 +1400</option>
    <option value="Pacific/Auckland">Pacific/Auckland: Friday, February 9, 2018 - 09:45 +1300</option>
    <option value="Pacific/Bougainville">Pacific/Bougainville: Friday, February 9, 2018 - 07:45 +1100</option>
    <option value="Pacific/Chatham">Pacific/Chatham: Friday, February 9, 2018 - 10:30 +1345</option>
    <option value="Pacific/Chuuk">Pacific/Chuuk: Friday, February 9, 2018 - 06:45 +1000</option>
    <option value="Pacific/Saipan">Pacific/Saipan: Friday, February 9, 2018 - 06:45 +1000</option>
    <option value="Pacific/Tahiti">Pacific/Tahiti: Thursday, February 8, 2018 - 10:45 -1000</option>
    <option value="Pacific/Tarawa">Pacific/Tarawa: Friday, February 9, 2018 - 08:45 +1200</option>
    <option value="Pacific/Tongatapu">Pacific/Tongatapu: Friday, February 9, 2018 - 09:45 +1300</option>
    <option value="Pacific/Wake">Pacific/Wake: Friday, February 9, 2018 - 08:45 +1200</option>
    <option value="Pacific/Wallis">Pacific/Wallis: Friday, February 9, 2018 - 08:45 +1200</option>
    <option value="UTC">UTC: Thursday, February 8, 2018 - 20:45 +0000</option>
  </select>

如果这还不够,我希望这是有道理和对不起的。试着想一想如何解释这一点。我不希望它在选择中执行WHERE语句,因为我不希望它仅仅因为他们的姓名&#39;而不是选择某些信息。不是&#34; john&#34;

1 个答案:

答案 0 :(得分:0)

首先,您没有告诉我们您使用哪个库来使用MySQL。我假设它是MySQLdb。

使用cursor.execute执行SQL查询后,您必须致电fetchallfetchone。在以后的情况下,您将能够customer['name']

示例:

results = cursor.execute("SELECT * FROM customers")
customer = cursor.fetchone()

你不应该使用try/except,因为在抛出任何异常或预期会出现任何异常时都没有明显的情况。