大漠駝鈴

          置身浩瀚的沙漠,方向最為重要,希望此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 草原上的駱駝 閱讀(314) 評論(0)  編輯  收藏 所屬分類: Python

          主站蜘蛛池模板: 泌阳县| 长岛县| 师宗县| 阳新县| 太湖县| 项城市| 平阳县| 土默特右旗| 孝昌县| 忻州市| 福贡县| 景东| 邢台市| 中牟县| 曲阳县| 铁力市| 宜城市| 化州市| 阳春市| 富宁县| 巫溪县| 清镇市| 洱源县| 江口县| 黎川县| 枝江市| 潜山县| 石柱| 保定市| 府谷县| 柘荣县| 紫阳县| 社旗县| 龙胜| 静安区| 象州县| 格尔木市| 赣州市| 遂溪县| 阳泉市| 吴江市|