用Python來編寫網站,必需要可以經過python操做數據庫,所謂操做數據庫,就是經過python實現對數據的鏈接,以及對記錄、字段的各類操做。上一講提到的那種操做方式,是看官直接經過交互模式來操做數據庫。html
安裝python-MySQLdb
要想經過python來操做數據庫,還須要在已經安裝了mysql的基礎上安裝一個稱之爲mysqldb的庫,它是一個接口程序,python經過它對mysql數據實現各類操做。python
在編程中,會遇到不少相似的接口程序,經過接口程序對另一個對象進行操做,比較簡單。接口程序就比如鑰匙,若是要開鎖,人直接用手指去捅,確定是不行的,那麼必須藉助工具,插入到鎖孔中,把所打開,打開所以後,門開了,就能夠操做門裏面的東西了。那麼打開所的工具就是接口程序。而打開所的工具會有便利與否之分,若是用這鎖的鑰匙,就便利,若是用別的工具,或許不便利(其實還分人,也就是人開鎖的水平,若是是江洋大盜或者小毛賊什麼的,擅長開鎖,用別的工具也便利了),也就是接口程序不一樣,編碼水平不一樣,都是考慮因素。mysql
這裏下載python-mysqldb:https://pypi.python.org/pypi/MySQL-python/sql
下載以後就能夠安裝了。shell
我這裏只能演示ubuntu下安裝的過程。數據庫
sudo apt-get install python-MySQLdb
在shell中輸入上面的命令行,就安裝了。看看,多麼簡潔的安裝,請快快用ubuntu吧。我願意作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官方文檔的內容,把全部的參數都列出來,還有相關說明,請看官認真閱讀。不過,上面幾個是經常使用的,其它的看狀況使用。
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.
我已經完成了數據庫的鏈接,雖然是在交互模式下,看官你是否也實現了呢?下一講,將開始講述如何操做數據庫。
《零基礎學python》在線教程:www.itdiffer.com,這裏按照章節排列好了,恭請各位光臨,並不吝賜教。