摘要:用來編寫網(wǎng)站,必須要能夠通過操作數(shù)據(jù)庫,所謂操作數(shù)據(jù)庫,就是通過實現(xiàn)對數(shù)據(jù)的連接,以及對記錄字段的各種操作。交互模式下操作數(shù)據(jù)庫之連接數(shù)據(jù)庫操作數(shù)據(jù)庫的前提是先有數(shù)據(jù)庫。先建立一個數(shù)據(jù)庫。
用Python來編寫網(wǎng)站,必須要能夠通過python操作數(shù)據(jù)庫,所謂操作數(shù)據(jù)庫,就是通過python實現(xiàn)對數(shù)據(jù)的連接,以及對記錄、字段的各種操作。上一講提到的那種操作方式,是看官直接通過交互模式來操作數(shù)據(jù)庫。
安裝python-MySQLdb要想通過python來操作數(shù)據(jù)庫,還需要在已經(jīng)安裝了mysql的基礎(chǔ)上安裝一個稱之為mysqldb的庫,它是一個接口程序,python通過它對mysql數(shù)據(jù)實現(xiàn)各種操作。
在編程中,會遇到很多類似的接口程序,通過接口程序?qū)α硗庖粋€對象進(jìn)行操作,比較簡單。接口程序就好比鑰匙,如果要開鎖,人直接用手指去捅,肯定是不行的,那么必須借助工具,插入到鎖孔中,把所打開,打開所之后,門開了,就可以操作門里面的東西了。那么打開所的工具就是接口程序。而打開所的工具會有便利與否之分,如果用這鎖的鑰匙,就便利,如果用別的工具,或許不便利(其實還分人,也就是人開鎖的水平,如果是江洋大盜或者小毛賊什么的,擅長開鎖,用別的工具也便利了),也就是接口程序不同,編碼水平不同,都是考慮因素。
這里下載python-mysqldb:https://pypi.python.org/pypi/MySQL-python/
下載之后就可以安裝了。
我這里只能演示ubuntu下安裝的過程。
sudo apt-get install python-MySQLdb
在shell中輸入上面的命令行,就安裝了??纯矗嗝春啙嵉陌惭b,請快快用ubuntu吧。我愿意做ubuntu的免費(fèi)代言。哈哈。
不管什么系統(tǒng),安裝不是難題。安裝之后,怎么知道安裝的結(jié)果呢?
>>> import MySQLdb
在python的交互模式中,輸入上面的指令,如果不報錯,恭喜你,已經(jīng)安裝好了。如果報錯,恭喜你,可以借著錯誤信息提高自己的計算機(jī)水平了,請求助于google大神。
交互模式下操作數(shù)據(jù)庫之連接數(shù)據(jù)庫操作數(shù)據(jù)庫的前提是先有數(shù)據(jù)庫。
先建立一個數(shù)據(jù)庫。
qw@qw-Latitude-E4300:~$ mysql -u root -p Enter password:
打開數(shù)據(jù)庫,正確輸入密碼之后,呈現(xiàn)下面的結(jié)果
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>
在這個狀態(tài)下,輸入如下命令,建立一個數(shù)據(jù)庫:
mysql> create database qiwsirtest character set utf8; Query OK, 1 row affected (0.00 sec)
注意上面的指令,如果僅僅輸入:create database qiwsirtest,也可以,但是,我在后面增加了character set utf8,意思是所建立的數(shù)據(jù)庫qiwsirtest,編碼是utf-8的,這樣存入漢字就不是亂碼了。
看到那一行提示:Query OK, 1 row affected (0.00 sec),就說明這個數(shù)據(jù)庫已經(jīng)建立好了,名字叫做:qiwsirtest
數(shù)據(jù)庫建立之后,就可以用python通過已經(jīng)安裝的mysqldb來連接這個名字叫做qiwsirtest的庫了。進(jìn)入到python交互模式(現(xiàn)在這個實驗室做實驗)。
>>> import MySQLdb >>> conn = MySQLdb.connect(host="localhost",user="root",passwd="123123",db="qiwsirtest",port=3306,charset="utf8")
逐個解釋上述命令的含義:
host:等號的后面應(yīng)該填寫mysql數(shù)據(jù)庫的地址,因為就數(shù)據(jù)庫就在本機(jī)上(也稱作本地),所以使用localhost,注意引號。如果在其它的服務(wù)器上,這里應(yīng)該填寫ip地址。一般中小型的網(wǎng)站,數(shù)據(jù)庫和程序都是在同一臺服務(wù)器(計算機(jī))上,就使用localhost了。
user:登錄數(shù)據(jù)庫的用戶名,這里一般填寫"root",還是要注意引號。當(dāng)然,如果是比較大型的服務(wù),數(shù)據(jù)庫會提供不同的用戶,那時候可以更改為相應(yīng)用戶。但是,不同用戶的權(quán)限可能不同,所以,在程序中,如果要操作數(shù)據(jù)庫,還要注意所擁有的權(quán)限。在這里用root,就放心了,什么權(quán)限都有啦。不過,這樣做,在大型系統(tǒng)中是應(yīng)該避免的。
passwd:上述user賬戶對應(yīng)的登錄mysql的密碼。我在上面的例子中用的密碼是"123123"。不要忘記引號。
db:就是剛剛通create命令建立的數(shù)據(jù)庫,我建立的數(shù)據(jù)庫名字是"qiwsirtest",還是要注意引號??垂偃绻⒌臄?shù)據(jù)庫名字不是這個,就寫自己所建數(shù)據(jù)庫名字。
port:一般情況,mysql的默認(rèn)端口是3306,當(dāng)mysql被安裝到服務(wù)器之后,為了能夠允許網(wǎng)絡(luò)訪問,服務(wù)器(計算機(jī))要提供一個訪問端口給它。
charset:這個設(shè)置,在很多教程中都不寫,結(jié)果在真正進(jìn)行數(shù)據(jù)存儲的時候,發(fā)現(xiàn)有亂碼。這里我將qiwsirtest這個數(shù)據(jù)庫的編碼設(shè)置為utf-8格式,這樣就允許存入漢字而無亂碼了。注意,在mysql設(shè)置中,utf-8寫成utf8,沒有中間的橫線。但是在python文件開頭和其它地方設(shè)置編碼格式的時候,要寫成utf-8。切記!
注:connect中的host、user、passwd等可以不寫,只有在寫的時候按照host、user、passwd、db(可以不寫)、port順序?qū)懢涂梢?,注意端口號port=3306還是不要省略的為好,如果沒有db在port前面,直接寫3306會報錯.
其實,關(guān)于connect的參數(shù)還不少,下面摘抄來自mysqldb官方文檔的內(nèi)容,把所有的參數(shù)都列出來,還有相關(guān)說明,請看官認(rèn)真閱讀。不過,上面幾個是常用的,其它的看情況使用。
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.
我已經(jīng)完成了數(shù)據(jù)庫的連接,雖然是在交互模式下,看官你是否也實現(xiàn)了呢?下一講,將開始講述如何操作數(shù)據(jù)庫。
《零基礎(chǔ)學(xué)python》在線教程:www.itdiffer.com,這里按照章節(jié)排列好了,恭請各位光臨,并不吝賜教。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/37432.html
摘要:用選擇要操作的數(shù)據(jù)庫,然后通過指針就可以操作這個數(shù)據(jù)庫了。這樣就在這個數(shù)據(jù)庫中創(chuàng)建了一個名為的表這是查看表的方式。樹欲靜而風(fēng)不止,小偷在行動。所以,要特別提醒諸位注意。 通過python操作數(shù)據(jù)庫的行為,除了能夠完成前面兩講中的操作之外(當(dāng)然,那是比較常用的),其實任何對數(shù)據(jù)庫進(jìn)行的操作,都能夠通過python-mysqldb來實現(xiàn)。 建立數(shù)據(jù)庫 在《用python操作數(shù)據(jù)庫(1)...
摘要:根據(jù)這個定義,在里面規(guī)定了一些占位符,通過這些占位符來說明那個位置應(yīng)該填寫什么類型的東西,這里暫且了解兩個占位符表示那個位置是整數(shù),表示那個位置應(yīng)該是字符串。啰嗦半天,占位符是不是理解了呢下面我們就用占位符來連接字符串。 上一章中已經(jīng)講到連接兩個字符串的一種方法。復(fù)習(xí)一下: >>> a= py >>> b= thon >>> a+b python 既然這是一種方法,言外之意,還有...
摘要:根據(jù)這個定義,在里面規(guī)定了一些占位符,通過這些占位符來說明那個位置應(yīng)該填寫什么類型的東西,這里暫且了解兩個占位符表示那個位置是整數(shù),表示那個位置應(yīng)該是字符串。啰嗦半天,占位符是不是理解了呢下面我們就用占位符來連接字符串。 感謝網(wǎng)友白羽毛的幫助。 上一章中已經(jīng)講到連接兩個字符串的一種方法。復(fù)習(xí)一下: >>> a= py >>> b= thon >>> a+b python 既然這...
摘要:和兩種類型數(shù)據(jù),有不少相似的地方,也有很大的區(qū)別。偏移量從開始,總元素數(shù)減結(jié)束。和轉(zhuǎn)化這個內(nèi)置函數(shù)實現(xiàn)的是將轉(zhuǎn)化為。在看例子之前,請看官在交互模式下做如下操作得到了對這個內(nèi)置函數(shù)的完整說明。 list和str兩種類型數(shù)據(jù),有不少相似的地方,也有很大的區(qū)別。本講對她們做個簡要比較,同時也是對前面有關(guān)兩者的知識復(fù)習(xí)一下,所謂溫故而知新。 相同點(diǎn) 都屬于序列類型的數(shù)據(jù) 所謂序列類型的數(shù)...
摘要:操作數(shù)據(jù)庫要對數(shù)據(jù)庫進(jìn)行操作,需要先連接它。執(zhí)行后返回值為受影響的行數(shù)。執(zhí)行單條語句但是重復(fù)執(zhí)行參數(shù)列表里的參數(shù)返回值為受影響的行數(shù)例如,要在數(shù)據(jù)表中插入一條記錄,使得,這樣做沒有報錯,并且返回一個結(jié)果,說明有一行記錄操作成功。 在上一講中已經(jīng)連接了數(shù)據(jù)庫。就數(shù)據(jù)庫而言,連接之后就要對其操作。但是,目前那個名字叫做qiwsirtest的數(shù)據(jù)僅僅是空架子,沒有什么可操作的,要操作它,就必...
閱讀 3146·2021-10-27 14:16
閱讀 2954·2021-09-24 10:33
閱讀 2362·2021-09-23 11:21
閱讀 3283·2021-09-22 15:14
閱讀 887·2019-08-30 15:55
閱讀 1749·2019-08-30 15:53
閱讀 1854·2019-08-29 11:14
閱讀 2244·2019-08-28 18:11