python 添加修改mysql

來源:互聯網
上載者:User

標籤:upd   attribute   等等   理解   into   attr   def   values   word   

工作中需要用radius,就用 ubuntu 下 freeradius + mysql  搭建一個radius 伺服器,這裡只是用python 串連 修改資料庫。

Python 2.7.12 (default, Nov 19 2016, 06:48:10)

mysql  Server version: 5.7.19-0ubuntu0.16.04.1 (Ubuntu)

碰到的問題:

1:不知道怎麼串連mysql

2:傳變數修改資料庫

經過幾天的調試,終於成功。

代碼如下:

import MySQLdb  

# 若是mysql安裝不成功,這裡就會出錯 
conn =MySQLdb.connect(host =‘127.0.0.1‘,user =‘root‘,passwd =‘casacasa‘,db =‘radius‘)

# 串連資料庫
cur=conn.cursor() 

# 建立 cur,至於目的也說不清,我的理解是為了執行cur.xxx 需要的一個變數名稱

n=1

while n < 10000:

    name = ‘test‘+str(n)

# 計劃建立一個可控循序的變數, 進一步寫入資料庫,用個迴圈可以批量寫
    print name
    aa = cur.execute("insert into radcheck (username,attribute,op,value) values (‘%s‘,‘Cleartext-Password‘,‘:=‘,‘testpwd‘);"%(name))

# 這裡是執行資料庫的insert 功能, 也可以執行其他 update, drop等等
    n += 1
bb = cur.execute("select * from radcheck;")

# 查詢下資料庫有沒有寫進去
cc=cur.fetchmany(bb)
for i in cc:
print i
cur.close()
conn.commit() 

# 這個好像沒有起作用


conn.close()

 

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.