python 安裝操作 MySQL 數據庫.


以ubuntu和mysql為例

檢查自己的機器上面有沒有安裝數據庫

1 xpower@xpower-CW65S:~$ sudo service mysql start
2 [sudo] xpower 的密碼: 


嘗試輸入命令打開 MySQL 不同的版本會有不同的效果 , 我的什么反饋也沒有這就說明已經安裝並且啟動成功了 ( 沒有消息就是最好的消息 ) 有的 ubuntu 會有反饋 , 自己看反饋內容 . 很容易判斷 .

安裝MySQL(安裝過程中可能需要填寫一點東西 . 建議填寫一下 . )

1 xpower@xpower-CW65S:~$ sudo apt-get install mysql-server # 安裝MySQL服務端,核心 程序
2 xpower@xpower-CW65S:~$ sudo apt-get install mysql-client # 安裝MySQL客戶端

 安裝MySQLdb              

我當時嘗試了好幾種方法來安裝但是都失敗了 最后用了這個(好像是這個吧...)

xpower@xpower-CW65S:~$ sudo pip install MySQL-python

准備工作完畢 下面開始 , 實踐一發 MySQL的基礎操作溫習一下.

啟動MySQL

mysql -u root -p # 有密碼的時候的啟動方式 .

mysql -u root     # 沒有密碼的時候的啟動方式 . 

練習 MySQL 的基本操作

xpower@xpower-CW65S:~$ mysql -u root -p  # 啟動數據庫
Enter password: 
mysql> show databases;   #查看當前所有的數據庫
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| mysql_xpower       |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
mysql> use mysql_xpower # 鏈接 mysql_xpower 數據庫
Database changed
mysql> show tables;   # 查看 mysql_xpower下的所有表單 
+------------------------+
| Tables_in_mysql_xpower |
+------------------------+
| Student_information    |
| employee               |
| user                   |
+------------------------+
3 rows in set (0.00 sec)
mysql> DROP TABLE user;  # 刪除表單 user
Query OK, 0 rows affected (0.30 sec)
mysql> CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); #創建一個 user 表單 
Query OK, 0 rows affected (0.46 sec)
mysql> show tables;   # 查看 mysql下的所有表單
+------------------------+
| Tables_in_mysql_xpower |
+------------------------+
| Student_information    |
| employee               |
| user                   |
+------------------------+
3 rows in set (0.00 sec)
mysql> insert into user values('tom','12345');
Query OK, 1 row affected (0.13 sec)

mysql> insert into user values('jack','23456');
Query OK, 1 row affected (0.04 sec)

mysql> insert into user values('lucy','34567');
Query OK, 1 row affected (0.12 sec)

mysql> SHOW TABLES # 查看表單 
    -> ;
+------------------------+
| Tables_in_mysql_xpower |
+------------------------+
| Student_information    |
| employee               |
| user                   |
+------------------------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM user; 查看 user表單下所有項
+------+----------+
| name | password |
+------+----------+
| tom  | 12345    |
| jack | 23456    |
| lucy | 34567    |
+------+----------+
3 rows in set (0.00 sec)

 
mysql> DELETE FROM user WHERE NAME='lucy'; # 刪除用戶 lucy
Query OK, 0 rows affected (0.00 sec)

mysql> insert into user values('lucy','34567');
Query OK, 1 row affected (0.04 sec)

mysql> delete from user where name ='jack';
Query OK, 1 row affected (0.05 sec)

mysql> update user set password='111' where name ='lucy'; # 修改 lucy的密碼 . 
Query OK, 1 row affected (0.12 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from user
    -> ;
+------+----------+
| name | password |
+------+----------+
| tom  | 12345    |
| lucy | 111      |
+------+----------+

上面是MySQL的基本操作 , 自己動手打一遍

進入正題

>>> conn= MySQLdb.connect(
...         host='localhost',
...         port = 3306,
...         user='root',
...         passwd='密碼',
...         db = '需要鏈接的數據庫',
...         )

在這里雖然我們取得了 python 的數據庫鏈接 , 但是不能在這個對象上直接對數據庫進行操作 , 還需要獲取對應的操作游標才能進行數據庫的操作 , 所以還需要 獲取游標 .

1 >>> cur = conn.cursor()

創建一個數據庫 (cur.execute() 沒有返回被影響的行數 , 因為這里是創建數據庫 , 所以被影響的是 0L)

但是到這里語句並沒有被真正的執行必須使用MySQLdb.commit() ( git ?  ) 才是真正的執行完畢 . 到這里才是真正的執行完畢 , 這時候我們的表格才是真正的創建完成 ,  同理向表中寫入數據 , 也是一樣的操作流程 execute ==> commit 不過 , 寫入的數據的 execute , 也是少有不同 , 如下

>>> cur.execute('CREATE TABLE stu_info (name VARCHAR(20),stu_id VARCHAR(30))')
0L
>>> conn.commit()
>>> 

下面會向表單中插入數據 , 是和上面不一樣的插入方式 .

>>> cur.executemany("insert into stu_info (name,stu_id) values (%s,%s)",(("jack",18),("lucy",19)))
2L
>>> conn.commit()


>>> cur.execute("insert into stu_info (name,stu_id) values ('yuanchongyang','156
31030706')")
1L
>>> cur.execute("insert into stu_info (name,stu_id) values ('caiweiwei','1563103
0717') ")
1L
>>> conn.commit()

上面嘗試了一下 , 看看能不能和 git 一樣多個數據 一次 commit . 需要了解一下 數據庫的工作流程....

>>> cur.execute("select * from stu_info")
4L
>>> stus = cur.fetchall()
>>> stus

查看表單內容

1 >>> cur.execute("select * from stu_info")
2 4L
3 >>> stus = cur.fetchall()
4 >>> stus
5 (('yuanchongyang', '15631030706'), ('caiweiwei', '15631030717'), ('jack', '18'), ('lucy', '19'))

 

1 >>>conn.close()
2 
3 Conn.close()關閉數據庫連接

上文中我們已經知道 , 通過

>>> import MySQLdb
>>> conn = MySQLdb.connect(
...         host='localhost',
...         port = 3306,
...         user='root',
...         passwd='q.123456',
...         db = 'mysql_xpower',
...         )
>>> cur = conn.cursor()
>>> sqli = "insert into stu_info values(%s,%s)"
>>> cur.execute(sqli,('mengmeng',123456789))
1L
>>> cur.close()
>>> cur = conn.cursor()
>>> cur.execute("select * from stu_info")
5L

這個時候獲得的cur可以用於顯示 表單的內容  

>>> stus = cur.fetchall()
>>> stus
(('yuanchongyang', '15631030706'), ('caiweiwei', '15631030717'), ('jack', '18'),
 ('lucy', '19'), ('mengmeng', '123456789'))

這樣就顯示出來了 但是如果想再顯示一次怎么辦呢?

>>> cur.scroll(0,'absolute')

沒錯就是他 , 他可以讓游標指針跳到你想要的位置 . 其實還有一個fetchone這個東西和fetchall 有什么區別呢 ? 自己觀察一下 單詞就行了 .


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM