forked from NanYoMy/DHT-woodworm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.py
74 lines (66 loc) · 2.48 KB
/
mysql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#-*- coding:utf-8 -*-
import MySQLdb
import os
def createDatabase():
try:
conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='456',port=3306,charset="UTF8")
cur=conn.cursor()
cur.execute('create database if not exists dht')
conn.select_db('dht')
cur.execute('drop table if exists hash_info')
cur.execute('drop table if exists peerIP')
cur.execute('create table hash_info(hash varchar(40), info varchar(100),primary key(hash))')
cur.execute('create table peerIP(ip varchar(40), info varchar(100),primary key(ip))')
conn.commit()
cur.close()
conn.close()
except MySQLdb.Error,e:
print 'mysql error %d:%s'%(e.args[0],e.args[1])
def insertInfo(hash):
try:
conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='456',port=3306,charset="UTF8")
cur=conn.cursor()
conn.select_db('dht')
sql="insert into hash_info(hash,info) values('%s','%s')"%(hash,"the info will added later")
cur.execute(sql)
conn.commit()
cur.close()
conn.close()
except MySQLdb.Error,e:
print 'mysql error %d:%s'%(e.args[0],e.args[1])
def selectAllTable(table):
try:
conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='456',port=3306,charset="UTF8")
cur=conn.cursor()
conn.select_db('dht')
sql="select * from "+table
count=cur.execute(sql)
print "thers are %s row in table:"% count
result=cur.fetchall()
for r in result:
#print 'magnet:?xt=urn:btih:'+r[0].upper()+" info:"+r[1]
if r[1]!="error" and r[1]!="":
print r[1]
conn.commit()
cur.close()
conn.close()
except MySQLdb.Error,e:
print 'mysql error %d:%s'%(e.args[0],e.args[1])
def executeSQL(sql):
try:
conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='456',port=3306,charset="UTF8")
cur=conn.cursor()
conn.select_db('dht')
cur.execute(sql)
conn.commit()
cur.close()
conn.close()
except MySQLdb.Error,e:
print 'mysql error %d:%s'%(e.args[0],e.args[1])
#sql="update hash_info set hash_info.info='Captain's VgHD DVD 21 a0472 to a0501.iso' where hash_info.hash='5302c42cdf439cafa98f048e8367b3ff4829628e'".encode('utf-8')
#print sql
#executeSQL(sql)
#createDatabase()
#insertInfo("4cde5b50a8930315b479931f6872a3db59575366")
selectAllTable("hash_info")
#selectAllTable("peerIP")