python訪問資料庫一

來源:互聯網
上載者:User

標籤:varchar   com   nec   bsp   rac   ble   var   use   passwd   

直接存取mysql,樣本如下:

# coding:utf-8import timeimport MySQLdb# import traceback# import sysconn = MySQLdb.connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, passwd=‘root‘, db=‘dev‘, charset="utf8", )cur = conn.cursor()# cur.execute("SET NAMES utf8")try:    sql = "create table mytest1("    sql = sql + "id int(11) not null auto_increment,"    sql = sql + "channelcode varchar(100) null,"    sql = sql + "systemcode varchar(100) not null,"    sql = sql + "systemname varchar(100) not null,"    sql = sql + "primary key(id))"    print sql;    cur.execute(sql);except Exception, e:    print Exception, ":", etry:    cur.execute("insert into mytest1(systemcode,systemname) values(‘test‘,‘測試‘)")    conn.commit()    print ‘insert new mytest1.‘except Exception, e:    print Exception, ":", e    # traceback.print_exc()# except:#     info=sys.exc_info()#     print info[0],":",info[1]try:    cur.execute("update mytest1 set systemname=‘測試" + time.strftime(‘%Y-%m-%d %H:%M:%S‘) + "‘ where systemcode=‘test‘");    conn.commit()except Exception, e:    print Exception, ":", etry:    itsystem = cur.execute("select * from mytest1");    print ‘count:‘ + str(itsystem);    info = cur.fetchmany(itsystem)    for ii in info:        # if print ii,chinese string will print it‘s unicode with u\####        print  str(ii[0]) + ‘,‘ + (ii[1] == None and str(None) or ii[1]) + ‘,‘ + ii[2] + ‘,‘ + ii[3]except Exception, e:    print Exception, ":", etry:    cur.execute("delete from itsystem where systemcode=‘test‘");    conn.commit()    cur.execute("drop table mytest1");except Exception, e:    print Exception, ":", ecur.close()conn.close()

 

python訪問資料庫一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.