当前位置:Gxlcms > 数据库问题 > python-mysql,socket

python-mysql,socket

时间:2021-07-01 10:21:17 帮助过:26人阅读

博客:http://www.cnblogs.com/wupeiqi/articles/4938499.html



二、python-mysql-API

mysql:http://www.cnblogs.com/wupeiqi/articles/5095821.html

1、查询数据

>>> import MySQLdb
>>>
>>> conn = MySQLdb.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘root‘,db=‘swconfig‘)
>>> cur = conn.cursor()
>>> reCount=cur.execute(‘select * from devinfors‘)

>>> reCount=cur.execute(‘select * from devinfors‘)
>>> data=cur.fetchall()

>>> print data
((1L, ‘BF50SW19-B3‘, ‘80.97.35.19‘, ‘101‘, ‘102‘, ‘BF50SW20-B3‘, ‘80.97.35.20‘, ‘101‘, ‘102‘, ‘B3‘, ‘11D‘, ‘A3‘, ‘07‘, 0L), (2L, ‘BF50SW03-B5‘, ‘80.97.3.19‘, ‘107‘, ‘108‘, ‘BF50SW04-B5‘, ‘80.97.3.20‘, ‘107‘, ‘108‘, ‘B3‘, ‘21F‘, ‘B2‘, ‘04‘, 0L), (3L, ‘BF50SW21-B3‘, ‘80.97.35.21‘, ‘103‘, ‘104‘, ‘BF50SW22-B3‘, ‘80.97.35.22‘, ‘103‘, ‘104‘, ‘B3‘, ‘21B‘, ‘C4‘, ‘01‘, 0L), (7L, ‘BF50SW19-B3‘, ‘80.97.35.19‘, ‘105‘, ‘106‘, ‘BF50SW20-B3‘, ‘80.97.35.20‘, ‘105‘, ‘106‘, ‘B3‘, ‘11D‘, ‘A3‘, ‘07‘, 0L), (9L, ‘BF50SW21-B3‘, ‘80.97.35.21‘, ‘107‘, ‘108‘, ‘BF50SW22-B3‘, ‘80.97.35.22‘, ‘107‘, ‘108‘, ‘B3‘, ‘21A‘, ‘D2‘, ‘07‘, 0L), (10L, ‘BF50SW21-B3‘, ‘80.97.35.21‘, ‘109‘, ‘110‘, ‘BF50SW22-B3‘, ‘80.97.35.22‘, ‘109‘, ‘110‘, ‘B3‘, ‘21A‘, ‘D2‘, ‘07‘, 0L))

>>> print reCount
6

>>> cur.close()
>>> conn.close()


2、插入数据

>>> conn = MySQLdb.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘root‘,db=‘swconfig‘)
>>> cur=conn.cursor()
>>> sql="insert into vlaninfos values(1,%s,%s,%s)"
>>> params=("80.2.238.0","802","B3")
>>> reCount=cur.execute(sql,params)

>>> conn.commit()


3、修改数据

>>> sql="update vlaninfos set vlan=%s,vlanid=%s where id=1"
>>> params=("80.4.238.0","800")
>>> reCount=cur.execute(sql,params)
>>> conn.commit()


4、删除数据

>>> sql="delete from vlaninfos where id=%s"
>>> params=(1,)
>>> reCount=cur.execute(sql,params)
>>> conn.commit()



三、socket

http://www.cnblogs.com/wupeiqi/articles/5040823.html


[root@localhost demo]# cat server.py
#!/usr/bin/python27
#coding:utf-8


import socket

s=socket.socket()
ip_port=(‘127.0.0.1‘,8888)
s.bind(ip_port)
s.listen(5)

while True:
    conn,address=s.accept()
    conn.send(‘hello.‘)
    flag=True
    while flag:
        data=conn.recv(1024)
        print data
        if data==‘exit‘:
            flag=False
        conn.send(‘welcome to python socket‘)
    conn.close()


[root@localhost demo]# cat client.py
#!/usr/bin/python27
#coding:utf-8


import socket

client=socket.socket()

ip_port=(‘127.0.0.1‘,8888)

client.connect(ip_port)

while True:
    data=client.recv(1024)
    print data
    input_str=raw_input(‘clent:‘)
    client.send(input_str)
    if input_str==‘exit‘:
        break


[root@localhost demo]#



python-mysql,socket

标签:mysql   socket   

人气教程排行