Python操作mysql資料庫

來源:互聯網
上載者:User

標籤:python mysql


有三種方式:mysql-connector, MySQLdb,torndb(對MySQLdb的二次封裝)

mysql-connector:遊標是弱引用,無法封裝成函數

MySQLdb:可以封裝成函數

torndb:傳回值可以轉換成字典

from __future__ import print_functionsql = (‘SELECT * from ipdata limit 10‘)# mysql-connectorprint(‘mysql-connector‘.center(50, ‘=‘))from mysql import connectorcnx = connector.Connect(host="localhost", user="root",                            password="", database="test", charset="utf8")# cnx.autocommit = Truedb0 = cnx.cursor()db0.execute(sql)for row in db0:    print(*row) # print row[0], row[1], row[2], row[3]# MySQLdbprint(‘MySQLdb‘.center(50, ‘=‘))import MySQLdbdef connect_mysql(db_host="localhost", user="root",                   passwd="",db="test", charset="utf8"):    conn = MySQLdb.connect(host=db_host, user=user, passwd=passwd, db=db, charset=charset)    conn.autocommit(True)    return conn.cursor()db1 = connect_mysql()db1.execute(sql)for row in db1:    print(*row)# torndb1print(‘torndb1‘.center(50, ‘=‘))import torndbimport simplejson as jsondb2 = torndb.Connection(    host=‘localhost‘,    database=‘test‘,    user=‘root‘,    password=‘‘,    charset="utf8")rows = db2.query(sql)for row in rows:    print(json.dumps(row, ensure_ascii=False))# # torndb2# print(‘torndb3‘.center(50, ‘=‘))# row = db2.get(sql)# print(json.dumps(row, ensure_ascii=False))## torndb3print(‘torndb2‘.center(50, ‘=‘))row = db2.get(‘SELECT * from ipdata limit 1‘)print(json.dumps(row, ensure_ascii=False))

get方法只能返回一條資料,返回多條會報錯


批量插入資料

# ! /usr/bin/python# -*- coding: utf-8 -*-sql = ‘INSERT INTO `ipdata` (`startip`,`endip`,`country`,`carrier`) VALUES (18684928,18684928,"內蒙古赤峰市巴林左旗","聯通林東鎮新城區BRAS資料機房")‘sql_tmp = ‘INSERT INTO `ipdata` (`startip`,`endip`,`country`,`carrier`) VALUES (%s, %s, %s, %s)‘values = [(16890112,16891391,"泰國","曼穀"),(16891392,16891647,"泰國","如果矽農"), (16891648,16892159,"泰國","加拉信府")]# mysql-connectorprint(‘mysql-connector‘.center(50, ‘=‘))from mysql import connectorcnx = connector.Connect(host="localhost", user="root",                            password="", database="test", charset="utf8")cnx.autocommit = Truedb0 = cnx.cursor()print db0.execute(sql)print db0.executemany(sql_tmp, values)# MySQLdbprint(‘MySQLdb‘.center(50, ‘=‘))import MySQLdbdef connect_mysql(db_host="localhost", user="root",                   passwd="",db="test", charset="utf8"):    conn = MySQLdb.connect(host=db_host, user=user, passwd=passwd, db=db, charset=charset)    conn.autocommit(True)    return conn.cursor()db1 = connect_mysql()print db1.execute(sql), db1.lastrowidprint db1.executemany(sql_tmp, values), db1.lastrowid# torndbprint(‘torndb1‘.center(50, ‘=‘))import torndbdb2 = torndb.Connection(    host=‘localhost‘,    database=‘test‘,    user=‘root‘,    password=‘‘,    charset="utf8")print db2.insert(sql)print db2.insertmany(sql_tmp, values)


本文出自 “點滴積累” 部落格,請務必保留此出處http://tianxingzhe.blog.51cto.com/3390077/1727000

Python操作mysql資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.