MySQLdb 是用于Python鏈接Mysql數據庫的接口,它實現了 Python 數據庫 API 規范 V2.0,基于 MySQL C API 上建立的
</blockquote>
環境準備
首先為了用Python的MySQLdb操作MySQL之前的準備條件
</ul>
? 必須確保操作系統(Linux CentOS)之上已經已經安裝了MySQL數據庫
[root@MySQL ~]# rpm -qa | grep mysql //查詢系統中是否安裝Mysql
[root@MySQL ~]# yum -y install mysql mysql-server //安裝Mysql
[root@MySQL ~]# service mysqld start //啟動Mysql
[root@MySQL ~]# chkconfig mysqld on //設置Mysql開機啟動
? 在MySQL數據庫上創建數據庫 testdb
mysql> show databases; //顯示所有數據庫
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)
mysql> create database testdb; //創建testdb數據庫
Query OK, 1 row affected (0.00 sec)
? 在testdb數據庫中創建數據表 testtab
? testdb表字段里面有 id,name,age,sex
mysql> use testdb; //進入testdb數據庫
Database changed
mysql> create table testtab( //創建testtab數據表
-> id int(4),
-> name varchar(255),
-> age int(3),
-> sex varchar(1)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> desc testtab; //顯示數據表屬性
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(4) | YES | | NULL | |
| name | varchar(255) | YES | | NULL | |
| age | int(3) | YES | | NULL | |
| sex | varchar(1) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
? 授權連接數據庫testdb使用的用戶名為"root" ,密碼為 "test123",所有權限
mysql> grant all privileges on testdb.* to 'root'@'%' identified by 'test123' with grant option;
mysql> flush privileges; //刷新剛才修改的權限,使其生效
Query OK, 0 rows affected (0.00 sec)
? 能連接MySQL保證系統中安裝了 Python MySQLdb 模塊
? 如果導入MySQLdb的輸出結果如下所示,意味著沒有安裝 MySQLdb 模塊
[root@MySQL ~]# python
Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named MySQLdb
[root@MySQL ~]# cd /usr/local/src/
[root@MySQL src]# wget http://download.sourceforge.net/sourceforge/mysql-python/MySQL-python-1.2.3.tar.gz
[root@MySQL src]# tar zxf MySQL-python-1.2.3.tar.gz
[root@MySQL src]# cd MySQL-python-1.2.3
[root@MySQL MySQL-python-1.2.3]# python setup.py build
[root@MySQL MySQL-python-1.2.3]# python setup.py install
? 如果導入MySQLdb的輸出沒有任何錯誤,意味著安裝 MySQLdb 模塊成功
[root@MySQL ~]# python
Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>
mysql> select * from VFORBOX;
+---------+------+------+
| NAME | AGE | SEX |
+---------+------+------+
| Vforbox | 18 | M |
+---------+------+------+
1 row in set (0.00 sec)
mysql> select * from VFORBOX;
+---------+------+------+
| NAME | AGE | SEX |
+---------+------+------+
| Vforbox | 18 | M |
| Vforbox | 18 | M |
+---------+------+------+
1 row in set (0.00 sec)
sql = "UPDATE VFORBOX SET AGE = AGE + 1 WHERE SEX = 'M'"
try:
#執行SQL語句
cur.execute(sql)
#提交到數據庫執行
conn=commit()
except:
conn.rollback()
關閉數據庫連接
conn.close()</pre></div>
? 執行以上腳本的輸出結果
mysql> select * from VFORBOX;
+---------+------+------+
| NAME | AGE | SEX |
+---------+------+------+
| vforbox | 21 | M |
+---------+------+------+
1 rows in set (0.00 sec)