大漠駝鈴

          置身浩瀚的沙漠,方向最為重要,希望此blog能向大漠駝鈴一樣,給我方向和指引。
          Java,Php,Shell,Python,服務器運維,大數據,SEO, 網站開發、運維,云服務技術支持,IM服務供應商, FreeSwitch搭建,技術支持等. 技術討論QQ群:428622099
          隨筆 - 238, 文章 - 3, 評論 - 117, 引用 - 0
          數據加載中……

          Python 連接 Mysql

          連接Mysql
          #coding=utf-8

          #MySQLdb 示例
          #
          ##################################
          import MySQLdb

          #建立和數據庫系統的連接
          conn 
          = MySQLdb.connect(host='localhost', user='root',passwd='longforfreedom')

          #獲取操作游標
          cursor 
          = conn.cursor()
          #執行SQL,創建一個數據庫.
          cursor.execute(
          """create database python """)

          #關閉連接,釋放資源
          cursor.close();
          插入數據、批量插入數據
          #coding=utf-8
          ###################################
          # @author migle
          # @date 
          2010-01-17
          ##################################
          #MySQLdb 示例
          #
          ##################################
          import MySQLdb

          #建立和數據庫系統的連接
          conn 
          = MySQLdb.connect(host='localhost', user='root',passwd='21ccvn')

          #獲取操作游標
          cursor 
          = conn.cursor()
          #執行SQL,創建一個數據庫.
          cursor.execute(
          """create database if not exists 21ccvn""")

          #選擇數據庫
          conn.select_db(
          '21ccvn');
          #執行SQL,創建一個數據表.
          cursor.execute(
          """create table 21ccvn(id int, info varchar(100)) """)

          value 
          = [1,"inserted ?"];

          #插入一條記錄
          cursor.execute(
          "insert into test values(%s,%s)",value);

          values
          =[]


          #生成插入參數值
          for i in range(20):
              values.append((i,
          'Hello mysqldb, I am recoder ' + str(i)))
          #插入多條記錄

          cursor.executemany(
          """insert into test values(%s,%s) """,values);

          #關閉連接,釋放資源
          cursor.close();
          查詢,獲取一個,獲取多個,獲取所有記錄數
          #coding=utf-8

          #
          # MySQLdb 查詢
          #
          #######################################

          import MySQLdb

          conn 
          = MySQLdb.connect(host='localhost', user='root', passwd='longforfreedom',db='21ccvn')

          cursor 
          = conn.cursor()

          count 
          = cursor.execute('select * from test')

          print 
          '總共有 %s 條記錄',count

          #獲取一條記錄,每條記錄做為一個元組返回
          print 
          "只獲取一條記錄:"
          result 
          = cursor.fetchone();
          print result
          #print 
          'ID: %s   info: %s' % (result[0],result[1])
          print 
          'ID: %s   info: %s' % result 

          #獲取5條記錄,注意由于之前執行有了fetchone(),所以游標已經指到第二條記錄了,也就是從第二條開始的所有記錄
          print 
          "只獲取5條記錄:"
          results 
          = cursor.fetchmany(5)
          for r in results:
              print r

          print 
          "獲取所有結果:"
          #重置游標位置,
          0,為偏移量,mode=absolute | relative,默認為relative,
          cursor.scroll(
          0,mode='absolute')
          #獲取所有結果
          results 
          = cursor.fetchall()
          for r in results:
              print r
          conn.close()

          posted on 2011-08-14 08:46 草原上的駱駝 閱讀(319) 評論(0)  編輯  收藏 所屬分類: Python

          主站蜘蛛池模板: 松阳县| 明溪县| 农安县| 页游| 新乡县| 镇雄县| 二手房| 综艺| 松溪县| 黄平县| 五指山市| 巨鹿县| 耿马| 嵩明县| 贵港市| 莱阳市| 新乡县| 六盘水市| 鄂托克旗| 海林市| 汉阴县| 友谊县| 马山县| 塔城市| 霞浦县| 岑溪市| 金阳县| 汉川市| 河曲县| 蒙自县| 内丘县| 广州市| 聂荣县| 惠水县| 台东县| 泽库县| 西盟| 芦山县| 凤冈县| 边坝县| 平武县|