python模塊:MySQLdb模塊

1.系統必須安裝MySQL-python軟件,不然python沒有鏈接的模塊(在Linux系統)node

[root@node1 python]# yum install MySQL-pythonpython

 

2.安裝mysql數據庫mysql

[root@node1 python]# yum install mysql-server mysqlsql

[root@node1 python]# /etc/init.d/mysqld restart數據庫

Stopping              mysqld:           [  OK  ]
Starting           mysqld:           [  OK  ]
vim

[root@node1 python]#
ide

 

3.用python建立數據庫和表,並插入數據fetch

[root@node1 python]# vim mysql1.py
#!/bin/env python
#!-*- coding:UTF-8 -*-
import MySQLdb,exceptions

try:
    conn=MySQLdb.connect(host='localhost',user='root',passwd='system',port=3306)   #鏈接數據庫
    cur=conn.cursor()      #打開數據庫

    cur.execute('create database if not exists tong')      #建立數據庫
    cur.execute('use tong')
    cur.execute('create table if not exists t (a int ,b int)')    #建立表

    cur.execute('insert into tii values(1,2),(3,4)')      #插入數據
    cur.execute('commit')     #提交事物
    cur.close()
    conn.close()

except MySQLdb.Error,e:
    print "MySQL Error %d: %s" %(e.args[0],e.args[1])
[root@node1 python]# ./mysql1.py
spa

[root@node1 python]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 282
Server version: 5.6.27-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select * from tong.t;          #查詢數據
+------+------+
| a    | b    |
+------+------+
|    1 |    2 |
|    3 |    4 |
+------+------+
2 rows in set (0.00 sec)
mysql>

rest

4.利用python讀取數據(和上面數據有重複的)

#!/bin/env python
#!-*- coding:UTF-8 -*-
import MySQLdb,exceptions

try:
    conn=MySQLdb.connect(host='localhost',user='root',passwd='system',port=3306)
    cur=conn.cursor()

    cur.execute('create database if not exists tong')
    cur.execute('use tong')
    cur.execute('create table if not exists t (a int ,b int)')

    cur.execute('insert into t values(1,2),(3,4)')
    cur.execute('commit')
    cur.close()
    conn.close()

except MySQLdb.Error,e:
    print "MySQL Error %d: %s" %(e.args[0],e.args[1])

try:
    conn=MySQLdb.connect(host='localhost',user='root',passwd='system',db='tong',port=3306)
    cur=conn.cursor()

    rows=cur.execute('select * from t')
    print "there has %s rows record" %rows

    result=cur.fetchone()            #獲取下一個查詢結果集
    print result
    print "A字段: %s B字段:%s" %result

    results1=cur.fetchmany(2)       #獲取指定的行數

    for i,j in results1:
        print "字段A的值: %s  字段B的值:%s" %(i,j)

    results2=cur.fetchall()         #獲取所有行數
    print "字段A全部值       字段B全部值"
    for i,j in results2:
        print "%-20s %-20s" %(i,j)

except MySQLdb.Error,e:
    print "MySQL Error %d: %s" %(e.args[0],e.args[1])

[root@node1 python]# chmod 755 mysql1.py

[root@node1 python]# ./mysql1.py 查詢t表中全部行:10(1L, 2L)A字段: 1 B字段:2獲取兩行數據: 3  4獲取兩行數據: 1  2獲取全部行       藜取全部行3                    4                   1                    2                   3                    4                   1                    2                   3                    4                   1                    2                   3                    4                   [root@node1 python]#

相關文章
相關標籤/搜索