MySQL Connection not available

今天踩到了一个坑:用Python3.3读写mysql数据库的时候,老是报MySQL Connection not available的异常,百思不得其解!

最后搞了一个早上,终于在大海里面捞到了一根针:http://bugs.mysql.com/bug.php?id=67649


If you open an unbuffered cursor and you do not read the WHOLE result set before closing the cursor, the next query will fail with

  File "/usr/lib/python3.2/site-packages/mysql/connector/connection.py", line 1075, in cursor
    raise errors.OperationalError("MySQL Connection not available.")
mysql.connector.errors.OperationalError: MySQL Connection not available.

and all subsequent database calls will fail too.


总结下来就是说,如果你打开了一个cursor,但是没有把里面的结果集都read一遍就把它close掉了,以后就悲剧了。

后来在mysql官网的文档(http://dev.mysql.com/doc/refman/5.7/en/connector-python-api-mysqlcursor.html)上面寻寻觅觅,发现这么一句话:

You must fetch all rows before being able to execute new queries using the same connection.

哎!不看文档害死人啊!!但是我看过好几遍也没注意到这句话死得不冤啊!!!


====================2013-11-13 补充====================

看了一下Python文档里面关于sqlite3的部分,貌似并没有指出一定要把cursor里面的内容都读完才能关掉。所以测试了一下Python3里自带的sqlite3的情况,得出的结论确实是cursor里面的内容没有全部read一遍关掉,并不会有任何异常。

测试代码如下:

import sqlite3

conn = sqlite3.connect('data.db');
cursor = conn.cursor()
cursor.execute("select * from TSite")
print(cursor.fetchone())
print(cursor.fetchone())
cursor.close()
cursor = conn.cursor()
cursor.execute("select * from TSite")
print(cursor.fetchone())
print(cursor.fetchone())
cursor.close()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Legal Notice Copyright © 2017 Veritas Technologies LLC. All rights reserved. Veritas and the Veritas Logo are trademarks or registered trademarks of Veritas Technologies LLC or its affiliates in the U.S. and other countries. Other names may be trademarks of their respective owners. This product may contain third party software for which Veritas is required to provide attribution to the third party (“Third Party Programs”). Some of the Third Party Programs are available under open source or free software licenses. The License Agreement accompanying the Software does not alter any rights or obligations you may have under those open source or free software licenses. Please see the Third Party Legal Notice Appendix to this Documentation or TPIP ReadMe File accompanying this product for more information on the Third Party Programs. The product described in this document is distributed under licenses restricting its use, copying, distribution, and decompilation/reverse engineering. No part of this document may be reproduced in any form by any means without prior written authorization of Veritas Technologies LLC and its licensors, if any. THE DOCUMENTATION IS PROVIDED "AS IS" AND ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID. VERITAS TECHNOLOGIES LLC SHALL NOT BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES IN CONNECTION WITH THE FURNISHING, PERFORMANCE, OR USE OF THIS DOCUMENTATION. THE INFORMATION CONTAINED IN THIS DOCUMENTATION IS SUBJECT TO CHANGE WITHOUT NOTICE. The Licensed Software and Documentation are deemed to be commercial computer software as defined in FAR 12.212 and subject to restricted rights as defined in FAR Section 52.227-19 "Commercial Computer Software - Restricted Rights" and DFARS 227.7202, et seq. "Commercial Computer Software and Commercial Computer Software Documentation," as applicable, and any successor regulations, whether delivered by Veritas as on premises or hosted services. Any use, modification, reproduction release, performance, display or disclosure of the Licensed Software and Documentation by the U.S. Government shall be solely in accordance with the terms of this Agreement.
Working With the Domain Controller Diagnostic Utility (Part 1)This article explains how the Domain Controller Diagnostic Utility can be used to troubleshoot problems with the Active Directory. Published: Jan 15, 2009 Updated: Jan 15, 2009 Section: Articles & Tutorials :: Windows Server 2008 Author: Brien M. Posey Printable Version Adjust font size: Rating: 3.6/5 - 9 Votes 1 2 3 4 5 If you would like to be notified when Brien M Posey releases the next article in this series please sign up to the WindowsNetworking.com Real time article update newsletter. Domain controllers are the backbone of just about any Windows network. After all, if your domain controllers are not working then the Active Directory does not work either. If the Active Directory does not work, then users cannot log on, group policies cannot be enforced, and a whole slew of other features become unavailable. Fortunately, Windows ships with a tool that you can use to keep your domain controllers running smoothly. This tool is called the Domain Controller Diagnostic Utility. In this article, I will show you how to use this tool to perform basic maintenance and diagnostic tasks on your domain controllers. Before I Begin The Domain Controller Diagnostic Utility has been a part of Windows for quite some time now. For the purposes of this article, I will be working with the version of this utility that comes with Windows Server 2008. Most, if not all of the features that I will be talking about are also available in the Windows Server 2003 SP1 version. DCDIAG existed prior to Windows Server 2003 SP1, but many of the commands that are available today were first introduced in Windows Server 2003 SP1. You can access the Domain Controller Diagnostic Utility by running the DCDIAG command from a Windows command prompt. Running the Domain Controller Diagnostic Utility If you want to keep things simple, you can run the Domain Controller Diagnostic Utility by entering the DCDIAG command into a Windows Command Prompt window. Upon doing so, the utility will perform a variety of tests against the domain controller that you're connected to. You can see an example of what these tests look like in Figure A. Figure A: The Domain Controller Diagnostic Utility runs a variety of tests against a domain controller By simply entering the DCDIAG command does get the job done, but this would not be much of an article if I just told you to run the command, and left it at that. There is a lot more to the Domain Controller Diagnostic Utility than meets the eye. Before you can really appreciate all the tool's capabilities you need to become familiar with the optional parameters that you can use in conjunction with the DCDIAG command. If you look at Figure B, you can see that the DCDIAG command’s syntax is too long to even fit on a screen capture. Like most things that are really complicated the command’s syntax is not as bad as it initially appears. Once you understand how the command works, using it becomes fairly easy. Figure B: The DCDIAG command’s syntax is so long that it won’t even fit on the screen Breaking Down the Syntax As you can see in the figure above, the DCDIAG command’s basic syntax looks like this: dcdiag.exe /s:<Directory Server>[:<LDAP Port>] [/u:<Domain>\<Username> /p:*|<Password>|""] [/hqv] [/n:<Naming Context>] [/f:<Log>] [/x:XMLLog.xml] [/skip:<Test>] [/test:<Test>] Although the screen capture shown in Figure B lists what each of the command line switches does, the explanation is a bit sparse. That being the case, I am going to try to give you a better explanation of what each of the command line switches does. /H If you run the DCDIAG command with the /H parameter, it simply displays the DCDIAG command’s syntax in the manner shown in Figure B. If you look closely at the figure, you will notice that you can also use the /? Switch to display the command’s syntax. /S The /S parameter allows you to specify a home server. Essentially, this means that you can use the /S parameter to specify the name of the domain controller that you want to run DCDIAG against. As you may recall, when I ran the DCDIAG command in Figure A, I did not have to specify a home server. If you do not specify a home server, then DCDIAG will just pick one automatically. There are a couple of instances in which a specified home server will be ignored. The DCPROMO and the Register In DNS tests are run locally rather than being run against a domain controller. Therefore, if you try to specify a home server for these tests, it will be ignored. I will talk more about these tests later on. /N The /N parameter allows you to specify a domain naming context. In case you aren’t familiar with this term, every domain is represented by a domain naming context. The domain naming context stores objects for the domain such as users, computers, groups, etc. You don’t have to specify a domain naming context, but if you choose to use one, you can enter it is NetBIOS, DNS (fully qualified domain name), or distinguished name (DN) form. /U Unless you are logged on as an administrator of the domain that you are testing, you will have to supply the DCDIAG command with a set of administrative credentials that it can use. As you no doubt know, administrative credentials typically consist of a username and a password. The /U switch is used to specify the username. Since you are entering the name of an account with domain admin permissions, you will have to enter the username in domain\username format. /P The other switch that is used when entering a set of credentials is the /P switch. As you have probably already figured out, you would follow the /P switch with the password for the account that you specified through the /U switch. /A An Active Directory is often grouped into sites. A site typically represents a collection of domain controllers that all have reliable, high speed connectivity between them. For example, if an organization has two different facilities, connected together by a WAN link, each of the two facilities would typically be configured to act as its own site since the computers within the facility are on a common LAN, but there is no LAN connection between the facilities. If your organization is divided into sites, then you will be interested in the /A switch. Using this switch tells DCDIAG to test all of the domain controllers in the current site. /E The /E switch is similar to the /A switch, except that instead of telling DCDIAG to test all of the domain controllers in the current site, it tells DCDIAG to test every domain controller in the entire enterprise. /Q As you have already seen, the DCDIAG command’s output is pretty long. It can be easy for error messages to get lost in such a long output. If this happens to you, you can use the /Q switch to run DCDIAG in Quiet more, which will cause it to only list error messages. /V The /V switch is kind of the opposite of the /Q switch. While the /Q switch reduces the size of the output, the /V switch increases it. That way you can get more detailed (verbose) information on the problem that you are trying to correct. /I Sometimes DCDIAG will produce meaningless error messages that can really be confusing to less experienced administrators. If this happens to you, you can use the /I switch to tell DCDIAG to suppress any unimportant error messages. Conclusion In this article, I have introduced you to some of the basic commands used by the Domain Controller Diagnostic Utility. In Part 2, I will continue the discussion by showing you how to use a few additional command line switches, and how to specify specific tests that you would like to run.
C#停车场管理系统可以使用MySQL作为数据库来存储相关信息。下面是一个简单的示例代码,演示了如何使用C#连接MySQL数据库并进行一些基本的操作: ```csharp using System; using MySql.Data.MySqlClient; namespace ParkingSystem { class Program { static void Main(string[] args) { string connectionString = "server=localhost;user=root;database=parking_system;password=your_password"; MySqlConnection connection = new MySqlConnection(connectionString); try { connection.Open(); // 创建管理员表 string createAdminTableQuery = "CREATE TABLE IF NOT EXISTS admin (id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50), password VARCHAR(50))"; MySqlCommand createAdminTableCommand = new MySqlCommand(createAdminTableQuery, connection); createAdminTableCommand.ExecuteNonQuery(); // 创建员工表 string createEmployeeTableQuery = "CREATE TABLE IF NOT EXISTS employee (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50), position VARCHAR(50))"; MySqlCommand createEmployeeTableCommand = new MySqlCommand(createEmployeeTableQuery, connection); createEmployeeTableCommand.ExecuteNonQuery(); // 创建停车证表 string createParkingPermitTableQuery = "CREATE TABLE IF NOT EXISTS parking_permit (id INT AUTO_INCREMENT PRIMARY KEY, permit_number VARCHAR(50), employee_id INT, FOREIGN KEY (employee_id) REFERENCES employee(id))"; MySqlCommand createParkingPermitTableCommand = new MySqlCommand(createParkingPermitTableQuery, connection); createParkingPermitTableCommand.ExecuteNonQuery(); // 创建停车位表 string createParkingSpotTableQuery = "CREATE TABLE IF NOT EXISTS parking_spot (id INT AUTO_INCREMENT PRIMARY KEY, spot_number VARCHAR(50), is_available BOOLEAN)"; MySqlCommand createParkingSpotTableCommand = new MySqlCommand(createParkingSpotTableQuery, connection); createParkingSpotTableCommand.ExecuteNonQuery(); // 创建收费标准表 string createFeeTableQuery = "CREATE TABLE IF NOT EXISTS fee (id INT AUTO_INCREMENT PRIMARY KEY, vehicle_type VARCHAR(50), hourly_rate DECIMAL(10, 2))"; MySqlCommand createFeeTableCommand = new MySqlCommand(createFeeTableQuery, connection); createFeeTableCommand.ExecuteNonQuery(); Console.WriteLine("停车场管理系统MySQL数据库创建成功!"); } catch (Exception ex) { Console.WriteLine("停车场管理系统MySQL数据库创建失败:" + ex.Message); } finally { connection.Close(); } } } } ``` 请注意,上述代码中的`your_password`需要替换为你的MySQL数据库密码。此示例代码创建了一个名为`parking_system`的数据库,并在其中创建了管理员表、员工表、停车证表、停车位表和收费标准表。你可以根据实际需求修改表的结构和字段。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值