#éè¿Pythonè¿æ¥æ°æ®åº ç¨Pythonæ¥ç¼åç½ç«ï¼å¿ é¡»è¦è½å¤éè¿pythonæä½æ°æ®åºï¼æè°æä½æ°æ®åºï¼å°±æ¯éè¿pythonå®ç°å¯¹æ°æ®çè¿æ¥ï¼ä»¥å对记å½ãåæ®µçåç§æä½ãä¸ä¸è®²æå°çé£ç§æä½æ¹å¼ï¼æ¯çå®ç´æ¥éè¿äº¤äºæ¨¡å¼æ¥æä½æ°æ®åºã ##å®è£ python-MySQLdb è¦æ³éè¿pythonæ¥æä½æ°æ®åºï¼è¿éè¦å¨å·²ç»å®è£ äºmysqlçåºç¡ä¸å®è£ ä¸ä¸ªç§°ä¹ä¸ºmysqldbçåºï¼å®æ¯ä¸ä¸ªæ¥å£ç¨åºï¼pythonéè¿å®å¯¹mysqlæ°æ®å®ç°åç§æä½ã å¨ç¼ç¨ä¸ï¼ä¼éå°å¾å¤ç±»ä¼¼çæ¥å£ç¨åºï¼éè¿æ¥å£ç¨åºå¯¹å¦å¤ä¸ä¸ªå¯¹è±¡è¿è¡æä½ï¼æ¯è¾ç®åãæ¥å£ç¨åºå°±å¥½æ¯é¥åï¼å¦æè¦å¼éï¼äººç´æ¥ç¨ææå»æ ï¼è¯å®æ¯ä¸è¡çï¼é£ä¹å¿ é¡»åå©å·¥å ·ï¼æå ¥å°éåä¸ï¼æææå¼ï¼æå¼æä¹åï¼é¨å¼äºï¼å°±å¯ä»¥æä½é¨éé¢çä¸è¥¿äºãé£ä¹æå¼æçå·¥å ·å°±æ¯æ¥å£ç¨åºãèæå¼æçå·¥å ·ä¼æä¾¿å©ä¸å¦ä¹åï¼å¦æç¨è¿éçé¥åï¼å°±ä¾¿å©ï¼å¦æç¨å«çå·¥å ·ï¼æè®¸ä¸ä¾¿å©ï¼å ¶å®è¿å人ï¼ä¹å°±æ¯äººå¼éçæ°´å¹³ï¼å¦ææ¯æ±æ´å¤§çæè å°æ¯è´¼ä»ä¹çï¼æ é¿å¼éï¼ç¨å«çå·¥å ·ä¹ä¾¿å©äºï¼ï¼ä¹å°±æ¯æ¥å£ç¨åºä¸åï¼ç¼ç æ°´å¹³ä¸åï¼é½æ¯èèå ç´ ã è¿éä¸è½½python-mysqldb:[https://pypi.python.org/pypi/MySQL-python/](https://pypi.python.org/pypi/MySQL-python/) ä¸è½½ä¹åå°±å¯ä»¥å®è£ äºã æè¿éåªè½æ¼ç¤ºubuntuä¸å®è£ çè¿ç¨ã sudo apt-get install python-MySQLdb å¨shellä¸è¾å ¥ä¸é¢çå½ä»¤è¡ï¼å°±å®è£ äºãççï¼å¤ä¹ç®æ´çå®è£ ï¼è¯·å¿«å¿«ç¨ubuntuå§ãææ¿æåubuntuçå 费代è¨ãååã ä¸ç®¡ä»ä¹ç³»ç»ï¼å®è£ 䏿¯é¾é¢ãå®è£ ä¹åï¼æä¹ç¥éå®è£ çç»æå¢ï¼ >>> import MySQLdb å¨pythonçäº¤äºæ¨¡å¼ä¸ï¼è¾å ¥ä¸é¢çæä»¤ï¼å¦æä¸æ¥éï¼æåä½ ï¼å·²ç»å®è£ 好äºã妿æ¥éï¼æåä½ ï¼å¯ä»¥åçéè¯¯ä¿¡æ¯æé«èªå·±çè®¡ç®æºæ°´å¹³äºï¼è¯·æ±å©äºgoogle大ç¥ã ##äº¤äºæ¨¡å¼ä¸æä½æ°æ®åºä¹è¿æ¥æ°æ®åº æä½æ°æ®åºçåææ¯å ææ°æ®åºã å 建ç«ä¸ä¸ªæ°æ®åºã qw@qw-Latitude-E4300:~$ mysql -u root -p Enter password: æå¼æ°æ®åºï¼æ£ç¡®è¾å ¥å¯ç ä¹åï¼åç°ä¸é¢çç»æ Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 373 Server version: 5.5.38-0ubuntu0.14.04.1 (Ubuntu) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> å¨è¿ä¸ªç¶æä¸ï¼è¾å ¥å¦ä¸å½ä»¤ï¼å»ºç«ä¸ä¸ªæ°æ®åºï¼ mysql> create database qiwsirtest character set utf8; Query OK, 1 row affected (0.00 sec) 注æä¸é¢çæä»¤ï¼å¦æä» ä» è¾å ¥ï¼create database qiwsirtestï¼ä¹å¯ä»¥ï¼ä½æ¯ï¼æå¨åé¢å¢å äºcharacter set utf8ï¼æææ¯æå»ºç«çæ°æ®åºqiwsirtestï¼ç¼ç æ¯utf-8çï¼è¿æ ·åå ¥æ±å就䏿¯ä¹±ç äºã çå°é£ä¸è¡æç¤ºï¼Query OK, 1 row affected (0.00 sec)ï¼å°±è¯´æè¿ä¸ªæ°æ®åºå·²ç»å»ºç«å¥½äºï¼ååå«å:qiwsirtest æ°æ®åºå»ºç«ä¹åï¼å°±å¯ä»¥ç¨pythonéè¿å·²ç»å®è£ çmysqldbæ¥è¿æ¥è¿ä¸ªååå«åqiwsirtestçåºäºãè¿å ¥å°pythonäº¤äºæ¨¡å¼ï¼ç°å¨è¿ä¸ªå®éªå®¤åå®éªï¼ã >>> import MySQLdb >>> conn = MySQLdb.connect(host="localhost",user="root",passwd="123123",db="qiwsirtest",port=3306,charset="utf8") é个解éä¸è¿°å½ä»¤çå«ä¹ï¼ - host:çå·çåé¢åºè¯¥å¡«åmysqlæ°æ®åºçå°åï¼å ä¸ºå°±æ°æ®åºå°±å¨æ¬æºä¸ï¼ä¹ç§°ä½æ¬å°ï¼ï¼æä»¥ä½¿ç¨localhostï¼æ³¨æå¼å·ã妿å¨å ¶å®çæå¡å¨ä¸ï¼è¿éåºè¯¥å¡«åipå°åãä¸è¬ä¸å°åçç½ç«ï¼æ°æ®åºåç¨åºé½æ¯å¨åä¸å°æå¡å¨ï¼è®¡ç®æºï¼ä¸ï¼å°±ä½¿ç¨localhostäºã - user:ç»å½æ°æ®åºçç¨æ·åï¼è¿éä¸è¬å¡«å"root",è¿æ¯è¦æ³¨æå¼å·ãå½ç¶ï¼å¦ææ¯æ¯è¾å¤§åçæå¡ï¼æ°æ®åºä¼æä¾ä¸åçç¨æ·ï¼é£æ¶åå¯ä»¥æ´æ¹ä¸ºç¸åºç¨æ·ã使¯ï¼ä¸åç¨æ·çæéå¯è½ä¸åï¼æä»¥ï¼å¨ç¨åºä¸ï¼å¦æè¦æä½æ°æ®åºï¼è¿è¦æ³¨æææ¥æçæéãå¨è¿éç¨rootï¼å°±æ¾å¿äºï¼ä»ä¹æé齿å¦ãä¸è¿ï¼è¿æ ·åï¼å¨å¤§åç³»ç»ä¸æ¯åºè¯¥é¿å çã - passwd:ä¸è¿°userè´¦æ·å¯¹åºçç»å½mysqlçå¯ç ãæå¨ä¸é¢çä¾åä¸ç¨çå¯ç æ¯"123123"ãä¸è¦å¿è®°å¼å·ã - db:å°±æ¯ååécreateå½ä»¤å»ºç«çæ°æ®åºï¼æå»ºç«çæ°æ®åºå忝"qiwsirtest"ï¼è¿æ¯è¦æ³¨æå¼å·ãçå®å¦æå»ºç«çæ°æ®åºåå䏿¯è¿ä¸ªï¼å°±åèªå·±æå»ºæ°æ®åºååã - port:ä¸è¬æ åµï¼mysqlçé»è®¤ç«¯å£æ¯3306ï¼å½mysql被å®è£ å°æå¡å¨ä¹åï¼ä¸ºäºè½å¤å 许ç½ç»è®¿é®ï¼æå¡å¨ï¼è®¡ç®æºï¼è¦æä¾ä¸ä¸ªè®¿é®ç«¯å£ç»å®ã - charset:è¿ä¸ªè®¾ç½®ï¼å¨å¾å¤æç¨ä¸é½ä¸åï¼ç»æå¨çæ£è¿è¡æ°æ®åå¨çæ¶åï¼åç°æä¹±ç ãè¿éæå°qiwsirtestè¿ä¸ªæ°æ®åºçç¼ç 设置为utf-8æ ¼å¼ï¼è¿æ ·å°±å 许åå ¥æ±åèæ ä¹±ç äºã注æï¼å¨mysql设置ä¸ï¼utf-8åæutf8,没æä¸é´ç横线ã使¯å¨pythonæä»¶å¼å¤´åå ¶å®å°æ¹è®¾ç½®ç¼ç æ ¼å¼çæ¶åï¼è¦åæutf-8ãåè®°ï¼ æ³¨ï¼connectä¸çhostãuserãpasswdçå¯ä»¥ä¸åï¼åªæå¨åçæ¶åæç §hostãuserãpasswdãdb(å¯ä»¥ä¸å)ãport顺åºåå°±å¯ä»¥ï¼æ³¨æç«¯å£å·port=3306è¿æ¯ä¸è¦çç¥ç为好ï¼å¦ææ²¡ædbå¨portåé¢ï¼ç´æ¥å3306伿¥é. å ¶å®ï¼å ³äºconnectçåæ°è¿ä¸å°ï¼ä¸é¢æææ¥èª[mysqldb宿¹ææ¡£çå 容](http://mysql-python.sourceforge.net/MySQLdb.html)ï¼æææçåæ°é½ååºæ¥ï¼è¿æç¸å ³è¯´æï¼è¯·çå®è®¤çé 读ãä¸è¿ï¼ä¸é¢å 个æ¯å¸¸ç¨çï¼å ¶å®ççæ åµä½¿ç¨ã connect(parameters...) >Constructor for creating a connection to the database. Returns a Connection Object. Parameters are the same as for the MySQL C API. In addition, there are a few additional keywords that correspond to what you would pass mysql_options() before connecting. Note that some parameters must be specified as keyword arguments! The default value for each parameter is NULL or zero, as appropriate. Consult the MySQL documentation for more details. The important parameters are: - host: name of host to connect to. Default: use the local host via a UNIX socket (where applicable) - user: user to authenticate as. Default: current effective user. - passwd: password to authenticate with. Default: no password. - db: database to use. Default: no default database. - port: TCP port of MySQL server. Default: standard port (3306). - unix_socket: location of UNIX socket. Default: use default location or TCP for remote hosts. - conv: type conversion dictionary. Default: a copy of MySQLdb.converters.conversions - compress: Enable protocol compression. Default: no compression. - connect_timeout: Abort if connect is not completed within given number of seconds. Default: no timeout (?) - named_pipe: Use a named pipe (Windows). Default: don't. - init_command: Initial command to issue to server upon connection. Default: Nothing. - read_default_file: MySQL configuration file to read; see the MySQL documentation for mysql_options(). - read_default_group: Default group to read; see the MySQL documentation for mysql_options(). - cursorclass: cursor class that cursor() uses, unless overridden. Default: MySQLdb.cursors.Cursor. This must be a keyword parameter. - use_unicode: If True, CHAR and VARCHAR and TEXT columns are returned as Unicode strings, using the configured character set. It is best to set the default encoding in the server configuration, or client configuration (read with read_default_file). If you change the character set after connecting (MySQL-4.1 and later), you'll need to put the correct character set name in connection.charset. If False, text-like columns are returned as normal strings, but you can always write Unicode strings. This must be a keyword parameter. - charset: If present, the connection character set will be changed to this character set, if they are not equal. Support for changing the character set requires MySQL-4.1 and later server; if the server is too old, UnsupportedError will be raised. This option implies use_unicode=True, but you can override this with use_unicode=False, though you probably shouldn't. If not present, the default character set is used. This must be a keyword parameter. - sql_mode: If present, the session SQL mode will be set to the given string. For more information on sql_mode, see the MySQL documentation. Only available for 4.1 and newer servers. If not present, the session SQL mode will be unchanged. This must be a keyword parameter. - ssl: This parameter takes a dictionary or mapping, where the keys are parameter names used by the mysql_ssl_set MySQL C API call. If this is set, it initiates an SSL connection to the server; if there is no SSL support in the client, an exception is raised. This must be a keyword parameter. æå·²ç»å®æäºæ°æ®åºçè¿æ¥ï¼è½ç¶æ¯å¨äº¤äºæ¨¡å¼ä¸ï¼çå®ä½ æ¯å¦ä¹å®ç°äºå¢ï¼ä¸ä¸è®²ï¼å°å¼å§è®²è¿°å¦ä½æä½æ°æ®åºã