科技知識動態:Mysql數據庫安裝和使用教程

導讀跟大家講解下有關Mysql數據庫安裝和使用教程,相信小伙伴們對這個話題應該也很關注吧,現在就為小伙伴們說說Mysql數據庫安裝和使用教程,小

跟大家講解下有關Mysql數據庫安裝和使用教程,相信小伙伴們對這個話題應該也很關注吧,現在就為小伙伴們說說Mysql數據庫安裝和使用教程,小編也收集到了有關Mysql數據庫安裝和使用教程的相關資料,希望大家看到了會喜歡。

在Python網絡爬蟲中,通常是通過TXT純文本方式存儲,其實也是可以存儲在數據庫中的;同時在WAMP(Windows、Apache、MySQL、PHP或Python)開發網站中,也可以通過Python構建網頁的,所以這篇文章主要講述Python調用MySQL數據庫相關編程知識

在Python網絡爬蟲中,通常是通過TXT純文本方式存儲,其實也是可以存儲在數據庫中的;同時在WAMP(Windows、Apache、MySQL、PHP或Python)開發網站中,也可以通過Python構建網頁的,所以這篇文章主要講述Python調用MySQL數據庫相關編程知識。從以下幾個方面進行講解:

1.配置MySLQ2.SQL語句基礎知識3.Python操作MySQL基礎知識4.Python調用MySQL示例

一. 配置MySQL

首先下載mysql-5.0.96-winx64,安裝過程如下圖所示。 1.安裝MySQL 5.02.選擇手動配置、服務類型、通用多功能型和安裝路徑3.設置數據庫訪問量連接數為15、端口為3306(代碼中設置URL用到)、編碼方式為utf-84.設置默認超級root用戶的用戶名和密碼,最后安裝成功

二. SQL語句基礎知識

安裝MySQL 5.0成功后,進行數據庫的簡單操作。 1.運行MySQL輸入默認用戶密碼123456

2.創建數據庫test01和使用數據庫(第二次調用直接use database) create database test01;

顯示數據庫中包含的數據庫:show databases;

3.創建表student,其中學號為主鍵 create table student(username varchar(20),password varchar(20),stuid int primary key);

4.顯示表結構,使用語句desc student

5.向學生表中插入數據并顯示查詢的數據

6.刪除表:drop table student;

7.更新數據 update student set password='000000' where stuid='1';

8.刪除數據 Delete from student where username='eastmount;

此時MySQL操作數據庫基本講解結束,你同樣可以實現數據庫的增刪改查、事務、存儲過程等操作,建議安裝可視化的軟件來替代黑框,或使用Navicat for MySQL軟件即可。代碼如下:

Enter password: ******mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || test || test01 |+--------------------+5 rows in set (0.00 sec)mysql> use test01;Database changedmysql> show tables;Empty set (0.00 sec)mysql> create table student(username varchar(20), -> password varchar(20), -> stuid int primary key);Query OK, 0 rows affected (0.33 sec)mysql> show tables;+------------------+| Tables_in_test01 |+------------------+| student |+------------------+1 row in set (0.00 sec)mysql> desc student;+----------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+----------+-------------+------+-----+---------+-------+| username | varchar(20) | YES | | NULL | || password | varchar(20) | YES | | NULL | || stuid | int(11) | NO | PRI | NULL | |+----------+-------------+------+-----+---------+-------+3 rows in set (0.03 sec)mysql> insert student(username, password, stuid) -> values('eastmount','123456',1) -> ;Query OK, 1 row affected (0.05 sec)mysql> select * from student;+-----------+----------+-------+| username | password | stuid |+-----------+----------+-------+| eastmount | 123456 | 1 |+-----------+----------+-------+1 row in set (0.00 sec)mysql> update student set password='000000' where stuid='1';Query OK, 1 row affected (0.10 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from student;+-----------+----------+-------+| username | password | stuid |+-----------+----------+-------+| eastmount | 000000 | 1 |+-----------+----------+-------+1 row in set (0.00 sec)mysql> delete from student where username='eastmount';Query OK, 1 row affected (0.08 sec)mysql> select * from student;Empty set (0.00 sec)mysql>

三. Python調用MySQL基礎知識

通常的安裝方法是使用:pip install mysql 安裝Python的MySQL庫,但是總會報錯。常見錯誤如:Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat)mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory這些可能是驅動等問題。

正確安裝方法:建議下載一個MySQL-python-1.2.3.win-amd64-py2.7.exe文件進行安裝。官網地址:pypi.python.org/pypi/MySQL-python/下載地址:www.jb51.net/softs/73369.html

下面我們要詳細了解Python數據庫API。從Python中訪問數據庫需要接口程序,接口程序是一個Python模塊,它提供數據庫客戶端庫(通常是C語言寫成的)的接口供你訪問。注意:Python接口程序都一定要遵守Python DB-API規范。DB-API是一個規范。它定義了一系列必須的對象和數據庫存取方式,以便為各種各樣的底層數據庫系統和多種多樣的數據庫接口程序提供一致的訪問接口。DB-API為不同的數據庫提供了一致的訪問接口,在不同的數據庫之間移植代碼成為一件輕松的事情。

下面簡單介紹DB-API的使用方法。

1.模塊屬性DB-API規范里的以下特性和屬性必須提供。一個DB-API兼容模塊定義如下所示:

apilevel:模塊兼容的DB-API版本號threadsafety:線程安全級別paramstyle:支持sql語句參數風格connect():連接數據庫

Python調用MsSQL需要導入MySQLdb庫,如下:

import MySQLdb

2.connect()函數

其中主要使用的方法是connect對象。connect()方法生成一個connect對象,用于訪問數據庫,其參數如下:

user:Usernamepassword:Passwordhost:Hostnamedatabase:DatabaseNamedsn:Data source name

注意并非所有的接口程序都嚴格按照這種格式,如MySQLdb。

import MySQLdbconn = MySQLdb.connect(host='localhost', db='test01', user='root', passwd='123456', port=3306, charset='utf8')

connect()對象方法如下:

close():關閉數據庫連接,或者關閉游標對象commit():提交當前事務rollback():取消當前事務cursor():創建游標或類游標對象errorhandler(cxn,errcls,errval):作為已給游標的句柄

注意,執行close()方法則上述的連接對象方法不能再使用,否則發生異常。commit()、rollback()、cursor()或許更對于支持事務的數據庫更有意義。數據庫事務(Database Transaction) ,是指作為單個邏輯工作單元執行的一系列操作,要么完整地執行,要么完全地不執行。 一旦你完成了數據庫連接,關閉了游標對象,然后在執行commit()提交你的操作,然后關閉連接。

3.游標對象上面說了connect()方法用于提供連接數據庫的接口,如果要對數據庫操作那么還需要使用游標對象。游標對象的屬性和方法:

fetchone():可以看作fetch(取出) one(一個),也就是得到結果集的下一行(一行)。fetchmany(size):可以看作fetch(取出)many(多個),這里的參數是界限,得到結果集的下幾行(幾行)fetchall():顧名思義,取得所有。execute(sql):執行數據庫操作,參數為sql語句。close():不需要游標時盡可能的關閉

下面通過簡單的示例進行講解。

四. Python調用MySQL示例

在前面數據庫中我們創建了數據庫“test01”和表“student”,同時插入了數據。那么,怎樣通過Python來顯示呢?1.查詢所有數據庫首先,我們查看本地數據庫中所包含的數據庫名稱,通過“show databases”語句。

import MySQLdb try: conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',port=3306) cur=conn.cursor() res = cur.execute('show databases') print res for data in cur.fetchall(): print '%s' % data cur.close() conn.close()except MySQLdb.Error,e: print "Mysql Error %d: %s" % (e.args[0], e.args[1])

其中通過鏈接數據庫代碼為:conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',port=3306)訪問root超級用戶,其密碼為“123456”,端口為“3306”,其結果如下:

如果不知道本地數據庫的名稱,可以通過該方法,先查詢數據庫中包含哪些數據庫,然后再連接該數據庫進行相關的操作。

2.查詢表下面介紹查詢表student中數據,代碼如下,代碼的具體含義是通過connect()連接數據庫,通過conn.cursor()定義游標,然后調用游標的excute(sql)執行數據庫操作,此處為查詢操作,再通過fetchall()函數獲取所有數據。

# coding:utf-8 import MySQLdb try: conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',port=3306, db='test01', charset='utf8') cur=conn.cursor() res = cur.execute('select * from student') print u'表中包含',res,u'條數據\n' print u'數據如下:(姓名 密碼 序號)' for data in cur.fetchall(): print '%s %s %s' % data cur.close() conn.close()except MySQLdb.Error,e: print "Mysql Error %d: %s" % (e.args[0], e.args[1])

輸出結果如圖所示:

對應的MySQL中的結果是一致的,下圖是對應的結果。

3.創建表下面這段代碼是創建一張教師表,主要是通過commit()提交數據。

# coding:utf-8 import MySQLdb try: conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',port=3306, db='test01', charset='utf8') cur=conn.cursor() #查看表 print u'插入前包含表:' cur.execute('show tables') for data in cur.fetchall(): print '%s' % data #插入數據 sql = '''create table teacher(id int not null primary key auto_increment, name char(30) not null, sex char(20) not null )''' cur.execute(sql) #查看表 print u'\n插入后包含表:' cur.execute('show tables') for data in cur.fetchall(): print '%s' % data cur.close() conn.commit() conn.close()except MySQLdb.Error,e: print "Mysql Error %d: %s" % (e.args[0], e.args[1])

輸出結果如下所示,插入教師表,包含字段:教師序號(id)、教師名稱(name)、教師性別(sex)。

插入數據也可以通過execute(sql)方法實現,如: cur.execute("insert into student values( 'yxz', '111111', '10')")但插入的新數據通常是通過變量進行賦值,而不是固定的,所以要對這條語句中的值做修改。我們可以做如下修改:

# coding:utf-8 import MySQLdb try: conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',port=3306, db='test01') cur=conn.cursor() #插入數據 sql = '''insert into student values(%s, %s, %s)''' cur.execute(sql, ('yxz','111111', '10')) #查看數據 print u'\n插入數據:' cur.execute('select * from student') for data in cur.fetchall(): print '%s %s %s' % data cur.close() conn.commit() conn.close()except MySQLdb.Error,e: print "Mysql Error %d: %s" % (e.args[0], e.args[1])

輸出結果如下所示:

>>> 插入數據:esatmount 123456 1yangxiuzhang 123456 2xiaoy 123456 3yxz 111111 10>>>

同樣,對數據庫的增刪改插都可以進行,請讀者自行閱讀。推薦資料:python使用mysql數據庫 - 蟲師后面我會結合Python爬蟲講述,如何將爬取的內容存儲在數據庫中,如我CSDN的博客,爬取博客標題、發布時間、閱讀量和評論數。

MySQL數據庫中結果如下圖所示:

【相關推薦】

1. 特別推薦:“php程序員工具箱”V0.1版本下載

2. Python免費視頻教程

3. Python基礎入門教程

以上就是Mysql數據庫安裝和使用教程的詳細內容,更多請關注php中文網其它相關文章!

來源:php中文網

免責聲明:本文由用戶上傳,如有侵權請聯系刪除!