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