標籤:roo man rom inf 命令 ref 1.2 table change
一,安裝mysql
如果是windows 使用者,mysql 的安裝非常簡單,直接下載安裝檔案,雙擊安裝檔案一步一步進行操作即可。
Linux 下的安裝可能會更加簡單,除了下載安裝包進行安裝外,一般的linux 倉庫中都會有mysql ,我們只需要通過一個命令就可以下載安裝:
Ubuntu\deepin
>>sudo apt-get install mysql-server
>>Sudo apt-get install mysql-client
centOS/redhat
>>yum install mysql
二,安裝MySQL-python
要想使python可以操作mysql 就需要MySQL-python驅動,它是python 操作mysql必不可少的模組。
:https://pypi.python.org/pypi/MySQL-python/
下載MySQL-python-1.2.5.zip 檔案之後直接解壓。進入MySQL-python-1.2.5目錄:
>>python setup.py install
三,測試
測試非常簡單,檢查MySQLdb 模組是否可以正常匯入。
[email protected]:~/pyse$ python Python 2.7.4 (default, Sep 26 2013, 03:20:56) [GCC 4.7.3] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import MySQLdb
沒有報錯提示MySQLdb模組找不到,說明安裝OK ,下面開始使用python 操作資料庫之前,我們有必要來回顧一下mysql的基本操作:
四,mysql 的基本操作
$ mysql -u root -p (有密碼時)
$ mysql -u root (無密碼時)
mysql> show databases; // 查看當前所有的資料庫+--------------------+| Database |+--------------------+| information_schema || csvt || csvt04 || mysql || performance_schema || test |+--------------------+6 rows in set (0.18 sec)mysql> use test; //作用與test資料庫Database changedmysql> show tables; //查看test庫下面的表Empty set (0.00 sec)//建立user表,name 和password 兩個欄位mysql> CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); Query OK, 0 rows affected (0.27 sec)//向user表插入入若干條資料mysql> insert into user values(‘Tom‘,‘1321‘);Query OK, 1 row affected (0.05 sec)mysql> insert into user values(‘Alen‘,‘7875‘);Query OK, 1 row affected (0.08 sec)mysql> insert into user values(‘Jack‘,‘7455‘);Query OK, 1 row affected (0.04 sec)//查看user表的資料mysql> select * from user;+------+----------+| name | password |+------+----------+| Tom | 1321 || Alen | 7875 || Jack | 7455 |+------+----------+3 rows in set (0.01 sec)//刪除name 等於Jack的資料mysql> delete from user where name = ‘Jack‘;Query OK, 1 rows affected (0.06 sec)//修改name等於Alen 的password 為 1111mysql> update user set password=‘1111‘ where name = ‘Alen‘;Query OK, 1 row affected (0.05 sec)Rows matched: 1 Changed: 1 Warnings: 0//查看錶內容mysql> select * from user;+--------+----------+| name | password |+--------+----------+| Tom | 1321 || Alen | 1111 |+--------+----------+3 rows in set (0.00 sec)
五,python 操作mysql資料庫基礎
#coding=utf-8import MySQLdbconn= MySQLdb.connect( host=‘localhost‘, port = 3306, user=‘root‘, passwd=‘123456‘, db =‘test‘, )cur = conn.cursor()#建立資料表#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")#插入一條資料#cur.execute("insert into student values(‘2‘,‘Tom‘,‘3 year 2 class‘,‘9‘)")#修改查詢條件的資料#cur.execute("update student set class=‘3 year 1 class‘ where name = ‘Tom‘")#刪除查詢條件的資料#cur.execute("delete from student where age=‘9‘")cur.close()conn.commit()conn.close()
>>> conn = MySQLdb.connect(host=‘localhost‘,port = 3306,user=‘root‘, passwd=‘123456‘,db =‘test‘,)
Connect() 方法用於建立資料庫的串連,裡面可以指定參數:使用者名稱,密碼,主機等資訊。
這隻是串連到了資料庫,要想操作資料庫需要建立遊標。
>>> cur = conn.cursor()
通過擷取到的資料庫連接conn下的cursor()方法來建立遊標。
>>> cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")
通過遊標cur 操作execute()方法可以寫入純sql語句。通過execute()方法中寫如sql語句來對資料進行操作。
>>>cur.close()
cur.close() 關閉遊標
>>>conn.commit()
conn.commit()方法在提交事物,在向資料庫插入一條資料時必須要有這個方法,否則資料不會被真正的插入。
>>>conn.close()
Conn.close()關閉資料庫連接
六,插入資料
通過上面execute()方法中寫入純的sql語句來插入資料並不方便。如:
>>>cur.execute("insert into student values(‘2‘,‘Tom‘,‘3 year 2 class‘,‘9‘)")
我要想插入新的資料,必須要對這條語句中的值做修改。我們可以做如下修改:
#coding=utf-8import MySQLdbconn= MySQLdb.connect( host=‘localhost‘, port = 3306, user=‘root‘, passwd=‘123456‘, db =‘test‘, )cur = conn.cursor()#插入一條資料sqli="insert into student values(%s,%s,%s,%s)"cur.execute(sqli,(‘3‘,‘Huhu‘,‘2 year 1 class‘,‘7‘))cur.close()conn.commit()conn.close()
假如要一次向資料表中插入多條值呢?
#coding=utf-8import MySQLdbconn= MySQLdb.connect( host=‘localhost‘, port = 3306, user=‘root‘, passwd=‘123456‘, db =‘test‘, )cur = conn.cursor()#一次插入多條記錄sqli="insert into student values(%s,%s,%s,%s)"cur.executemany(sqli,[ (‘3‘,‘Tom‘,‘1 year 1 class‘,‘6‘), (‘3‘,‘Jack‘,‘2 year 1 class‘,‘7‘), (‘3‘,‘Yaheng‘,‘2 year 2 class‘,‘7‘), ])cur.close()conn.commit()conn.close()
executemany()方法可以一次插入多條值,執行單挑sql語句,但是重複執行參數列表裡的參數,傳回值為受影響的行數。
七,查詢資料
也許你已經嘗試了在python中通過
>>>cur.execute("select * from student")
來查詢資料表中的資料,但它並沒有把表中的資料列印出來,有些失望。
來看看這條語句獲得的是什麼
>>>aa=cur.execute("select * from student")
>>>print aa
5
它獲得的只是我們的表中有多少條資料。那怎樣才能獲得表中的資料呢?進入python shell
>>> import MySQLdb>>> conn = MySQLdb.connect(host=‘localhost‘,port = 3306,user=‘root‘, passwd=‘123456‘,db =‘test‘,)>>> cur = conn.cursor()>>> cur.execute("select * from student")5L>>> cur.fetchone()(1L, ‘Alen‘, ‘1 year 2 class‘, ‘6‘)>>> cur.fetchone()(3L, ‘Huhu‘, ‘2 year 1 class‘, ‘7‘)>>> cur.fetchone()(3L, ‘Tom‘, ‘1 year 1 class‘, ‘6‘)...>>>cur.scroll(0,‘absolute‘)
fetchone()方法可以協助我們獲得表中的資料,可是每次執行cur.fetchone() 獲得的資料都不一樣,換句話說我沒執行一次,遊標會從表中的第一條資料移動到下一條資料的位置,所以,我再次執行的時候得到的是第二條資料。
scroll(0,‘absolute‘) 方法可以將遊標定位到表中的第一條資料。
還是沒解決我們想要的結果,如何獲得表中的多條資料並列印出來呢?
#coding=utf-8import MySQLdbconn= MySQLdb.connect( host=‘localhost‘, port = 3306, user=‘root‘, passwd=‘123456‘, db =‘test‘, )cur = conn.cursor()#獲得表中有多少條資料aa=cur.execute("select * from student")print aa#列印表中的多少資料info = cur.fetchmany(aa)for ii in info: print iicur.close()conn.commit()conn.close()
通過之前的print aa 我們知道當前的表中有5條資料,fetchmany()方法可以獲得多條資料,但需要指定資料的條數,通過一個for迴圈就可以把多條資料列印出啦!執行結果如下:
5(1L, ‘Alen‘, ‘1 year 2 class‘, ‘6‘)(3L, ‘Huhu‘, ‘2 year 1 class‘, ‘7‘)(3L, ‘Tom‘, ‘1 year 1 class‘, ‘6‘)(3L, ‘Jack‘, ‘2 year 1 class‘, ‘7‘)(3L, ‘Yaheng‘, ‘2 year 2 class‘, ‘7‘)[Finished in 0.1s]
來自:http://www.cnblogs.com/fnng/p/3565912.html
python使用mysql資料庫