日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
      軟件
      軟件
      文章
      搜索

      首頁編程開發(fā)其它知識 → Python連接mysql數(shù)據(jù)庫、python使用mysqldb連接數(shù)據(jù)庫

      Python連接mysql數(shù)據(jù)庫、python使用mysqldb連接數(shù)據(jù)庫

      相關(guān)軟件相關(guān)文章發(fā)表評論 來源:西西整理時間:2014/2/25 10:20:42字體大。A-A+

      作者:西西點擊:394次評論:1次標簽: Python mysql數(shù)據(jù)庫

      INOQONIv1.0.3 安卓版
      • 類型:休閑益智大小:45.3M語言:中文 評分:3.0
      • 標簽:
      立即下載

      做python的時候需要用到數(shù)據(jù)庫,于是自己重新整理了一下數(shù)據(jù)庫的知識,并且熟悉了python中MysqlDB模塊的功能和函數(shù)等接口,現(xiàn)在系統(tǒng)地來總結(jié)一下吧:

      首先你要做的還是下載相應(yīng)的模塊并且安裝啦,下載地址自己搜,網(wǎng)上有很多,安裝的話也很好辦,安裝之后python的安裝目錄下的Lib文件夾下的site-packages文件夾下的MySQLdb文件夾,這之中存放的便是該模塊的定義。準備工作做好之后我們需要在源碼中import MySQLdb

      數(shù)據(jù)庫的連接:

      模塊引入之后我們就需要和數(shù)據(jù)庫進行連接了,實例代碼如下:

      db = MySQLdb.connect("localhost","root","123456","myciti" )

      這三個關(guān)鍵參數(shù)的含義一目了然:第一個為服務(wù)器的地址,第二個為用戶名,第三個為dbms密碼,第四個為要訪問的數(shù)據(jù)庫,其實該connect函數(shù)的參數(shù)不止這些,不過由于其有默認值而且大多數(shù)情況下不用修改,因而省略了。這里做如下列表:

      host,連接的數(shù)據(jù)庫服務(wù)器主機名,默認為本地主機(localhost)。

      user,連接數(shù)據(jù)庫的用戶名,默認為當(dāng)前用戶。

      passwd,連接密碼,沒有默認值。

      db,連接的數(shù)據(jù)庫名,沒有默認值。

      conv,將文字映射到Python類型的字典。默認為MySQLdb.converters.conversions

      cursorclass,cursor()使用的種類,默認值為MySQLdb.cursors.Cursor。

      compress,啟用協(xié)議壓縮功能。

      named_pipe,在windows中,與一個命名管道相連接。

      init_command,一旦連接建立,就為數(shù)據(jù)庫服務(wù)器指定一條語句來運行。

      read_default_file,使用指定的MySQL配置文件。

      read_default_group,讀取的默認組。

      unix_socket,在unix中,連接使用的套接字,默認使用TCP。

      port,指定數(shù)據(jù)庫服務(wù)器的連接端口,默認是3306

      大家可能會注意到源碼中沒有用到端口號,這是因為MySQLdb的connect函數(shù)的該參數(shù)的默認值便是3306,如果你在安裝mysql的時候修改了數(shù)據(jù)庫的端口號,那么你就需要在源碼中加上該參數(shù)的修改值了。

      一,安裝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驅(qū)動,它是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 模塊是否可以正常導(dǎo)入。

      fnngj@fnngj-H24X:~/pyse$ python 
      Python 2.7.4 (default, Sep 26 2013, 03:20:56) 
      [GCC 4.7.3] on linux2
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import MySQLdb


      沒有報錯提示MySQLdb模塊找不到,說明安裝OK ,下面開始使用python 操作數(shù)據(jù)庫之前,我們有必要來回顧一下mysql的基本操作:

      四,mysql 的基本操作

      $ mysql -u root -p  (有密碼時)

      $ mysql -u root     (無密碼時)

      mysql> show databases;  // 查看當(dāng)前所有的數(shù)據(jù)庫
      +--------------------+
      | Database           |
      +--------------------+
      | information_schema |
      | csvt               |
      | csvt04             |
      | mysql              |
      | performance_schema |
      | test               |
      +--------------------+
      6 rows in set (0.18 sec)
      
      mysql> use test;   //作用與test數(shù)據(jù)庫
      Database changed
      mysql> show tables;   //查看test庫下面的表
      Empty set (0.00 sec)
      
      //創(chuàng)建user表,name 和password 兩個字段
      mysql> CREATE  TABLE  user (name VARCHAR(20),password VARCHAR(20));  Query OK, 0 rows affected (0.27 sec)
      
      //向user表內(nèi)插入若干條數(shù)據(jù)
      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表的數(shù)據(jù)
      mysql> select * from user;+------+----------+
      | name | password |
      +------+----------+
      | Tom  | 1321     |
      | Alen | 7875     |
      | Jack | 7455     |
      +------+----------+
      3 rows in set (0.01 sec)
      
      //刪除name 等于Jack的數(shù)據(jù)
      mysql> delete from user where name = 'Jack';Query OK, 1 rows affected (0.06 sec)
      
      //修改name等于Alen 的password 為 1111
      mysql> update user set password='1111' where name = 'Alen';Query OK, 1 row affected (0.05 sec)
      Rows matched: 1  Changed: 1  Warnings: 0
      
      //查看表內(nèi)容
      mysql> select * from user;+--------+----------+
      | name   | password |
      +--------+----------+
      | Tom    | 1321     |
      | Alen   | 1111     |
      +--------+----------+
      3 rows in set (0.00 sec)

      五,python 操作mysql數(shù)據(jù)庫基礎(chǔ)

      #coding=utf-8import MySQLdb
      
      conn= MySQLdb.connect(
              host='localhost',
              port = 3306,
              user='root',
              passwd='123456',
              db ='test',
              )
      cur = conn.cursor()#創(chuàng)建數(shù)據(jù)表#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")#插入一條數(shù)據(jù)#cur.execute("insert into student values('2','Tom','3 year 2 class','9')")#修改查詢條件的數(shù)據(jù)#cur.execute("update student set class='3 year 1 class' where name = 'Tom'")#刪除查詢條件的數(shù)據(jù)#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() 方法用于創(chuàng)建數(shù)據(jù)庫的連接,里面可以指定參數(shù):用戶名,密碼,主機等信息。

      這只是連接到了數(shù)據(jù)庫,要想操作數(shù)據(jù)庫需要創(chuàng)建游標。

      >>> cur = conn.cursor()

      通過獲取到的數(shù)據(jù)庫連接conn下的cursor()方法來創(chuàng)建游標。

      >>> cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")

      通過游標cur 操作execute()方法可以寫入純sql語句。通過execute()方法中寫如sql語句來對數(shù)據(jù)進行操作。

      >>>cur.close()

      cur.close() 關(guān)閉游標

      >>>conn.commit()

      conn.commit()方法在提交事物,在向數(shù)據(jù)庫插入一條數(shù)據(jù)時必須要有這個方法,否則數(shù)據(jù)不會被真正的插入。

      >>>conn.close()

      Conn.close()關(guān)閉數(shù)據(jù)庫連接

      六,插入數(shù)據(jù)

      通過上面execute()方法中寫入純的sql語句來插入數(shù)據(jù)并不方便。如:

      >>>cur.execute("insert into student values('2','Tom','3 year 2 class','9')")

      我要想插入新的數(shù)據(jù),必須要對這條語句中的值做修改。我們可以做如下修改:

      #coding=utf-8import MySQLdb
      
      conn= MySQLdb.connect(
              host='localhost',
              port = 3306,
              user='root',
              passwd='123456',
              db ='test',
              )
      cur = conn.cursor()#插入一條數(shù)據(jù)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()

      假如要一次向數(shù)據(jù)表中插入多條值呢?

      #coding=utf-8import MySQLdb
      
      conn= 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()方法可以一次插入多條值,執(zhí)行單挑sql語句,但是重復(fù)執(zhí)行參數(shù)列表里的參數(shù),返回值為受影響的行數(shù)。

      七,查詢數(shù)據(jù)

      也許你已經(jīng)嘗試了在python中通過

      >>>cur.execute("select * from student")

      來查詢數(shù)據(jù)表中的數(shù)據(jù),但它并沒有把表中的數(shù)據(jù)打印出來,有些失望。

      來看看這條語句獲得的是什么

      >>>aa=cur.execute("select * from student")

      >>>print aa

      5

      它獲得的只是我們的表中有多少條數(shù)據(jù)。那怎樣才能獲得表中的數(shù)據(jù)呢?進入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()方法可以幫助我們獲得表中的數(shù)據(jù),可是每次執(zhí)行cur.fetchone() 獲得的數(shù)據(jù)都不一樣,換句話說我沒執(zhí)行一次,游標會從表中的第一條數(shù)據(jù)移動到下一條數(shù)據(jù)的位置,所以,我再次執(zhí)行的時候得到的是第二條數(shù)據(jù)。

        scroll(0,'absolute') 方法可以將游標定位到表中的第一條數(shù)據(jù)。

      還是沒解決我們想要的結(jié)果,如何獲得表中的多條數(shù)據(jù)并打印出來呢?

      #coding=utf-8import MySQLdb
      
      conn= MySQLdb.connect(
              host='localhost',
              port = 3306,
              user='root',
              passwd='123456',
              db ='test',
              )
      cur = conn.cursor()#獲得表中有多少條數(shù)據(jù)aa=cur.execute("select * from student")print aa#打印表中的多少數(shù)據(jù)info = cur.fetchmany(aa)for ii in info:    print ii
      cur.close()
      conn.commit()
      conn.close()

        通過之前的print aa 我們知道當(dāng)前的表中有5條數(shù)據(jù),fetchmany()方法可以獲得多條數(shù)據(jù),但需要指定數(shù)據(jù)的條數(shù),通過一個for循環(huán)就可以把多條數(shù)據(jù)打印出啦!執(zhí)行結(jié)果如下:

      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]

        相關(guān)評論

        閱讀本文后您有什么感想? 已有人給出評價!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過難過
        • 5 囧
        • 3 圍觀圍觀
        • 2 無聊無聊

        熱門評論

        最新評論

        發(fā)表評論 查看所有評論(1)

        昵稱:
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字數(shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)