通過python操作資料庫的行為,任何對資料庫進行的操作,都能夠通過python-mysqldb來實作。
建立資料庫
之前通過
mysql>
寫SQL語句,建立了一個名字叫做mytest的資料庫,然後用下面的方式跟這個資料庫連接配接
>>> import MySQLdb
>>> conn = MySQLdb.connect(host="localhost",user="root",passwd="123123",db="mytest",charset="utf8")
在上面的連接配接中,參數
db="mytest"
其實可以省略,如果省略,就是沒有跟任何具體的資料庫連接配接,隻是連接配接了mysql。
>>> import MySQLdb
>>> conn = MySQLdb.connect("localhost","root","123123",port=3306,charset="utf8")
這種連接配接沒有指定具體資料庫,接下來就可以用類似
mysql>
互動模式下的方式進行操作。
>>> conn.select_db("mytest")
>>> cur = conn.cursor()
>>> cur.execute("select * from users")
11L
>>> cur.fetchall()
((1L, u'hiekay', u'123123', u'[email protected]'), (2L, u'python', u'123456', u'[email protected]'), (3L, u'google', u'111222', u'[email protected]'), (4L, u'facebook', u'222333', u'[email protected]'), (5L, u'github', u'333444', u'[email protected]'), (6L, u'docker', u'444555', u'[email protected]'), (7L, u'\u5854', u'9988', u'[email protected]'), (8L, u'\u5854', u'9988', u'[email protected]'), (9L, u'\u5854', u'9988', u'[email protected]'), (10L, u'\u5854', u'9988', u'[email protected]'), (11L, u'\u5f20\u4e09', u'1122', u'[email protected]'))
用
conn.select_db()
選擇要操作的資料庫,然後通過指針就可以操作這個資料庫了。其它的操作跟之前的一樣。
如果不選資料庫,而是要建立一個資料庫,如何操作?
>>> cur = conn.cursor()
>>> cur.execute("create database newtest")
1L
建立資料庫之後,就可以選擇這個資料庫,然後在這個資料庫中建立一個資料表。
>>> cur.execute("create table newusers (id int(2) primary key auto_increment, username varchar(20), age int(2), email text)")
0L
括号裡面是引号,引号裡面就是建立資料表的語句,一定是熟悉的。這樣就在newtest這個資料庫中建立了一個名為newusers的表
>>> cur.execute("show tables")
1L
>>> cur.fetchall()
((u'newusers',),)
這是檢視表的方式。當然,可以在
mysql>
互動模式下檢視是不是存在這個表。如下:
mysql> use newtest;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-------------------+
| Tables_in_newtest |
+-------------------+
| newusers |
+-------------------+
1 row in set (0.00 sec)
mysql> desc newusers;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(2) | NO | PRI | NULL | auto_increment |
| username | varchar(20) | YES | | NULL | |
| age | int(2) | YES | | NULL | |
| email | text | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
以上就通過python-mysqldb實作了對資料庫和表的建立。
當然,能建就能删除。可以自行嘗試,在這裡就不贅述,原理就是在
cur.execute()
中寫SQL語句。
關閉一切
當進行完有關資料操作之後,最後要做的就是關閉遊标(指針)和連接配接。用如下指令實作:
>>> cur.close()
>>> conn.close()
注意關閉順序,和打開的順序相反。
關于亂碼問題
這個問題是編寫web時常常困擾程式員的問題,亂碼的本質來自于編碼格式的設定混亂。是以,要特别提醒諸位注意。在用python-mysqldb的時候,為了放置亂碼,可以做如下統一設定:
- Python檔案設定編碼 utf-8(檔案前面加上 #encoding=utf-8)
- MySQL資料庫charset=utf8(資料庫的設定方法,可以網上搜尋)
- Python連接配接MySQL是加上參數 charset=utf8
- 設定Python的預設編碼為 utf-8 (sys.setdefaultencoding(utf-8),這個後面會講述)
代碼示例:
#encoding=utf-8
import sys
import MySQLdb
reload(sys)
sys.setdefaultencoding('utf-8')
db=MySQLdb.connect(user='root',charset='utf8')
MySQL的配置檔案設定也必須配置成utf8 設定 MySQL 的 my.cnf 檔案,在 [client]/[mysqld]部分都設定預設的字元集(通常在/etc/mysql/my.cnf):
[client] default-character-set = utf8
[mysqld] default-character-set = utf8