-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabaseconnect.py
46 lines (41 loc) · 1.19 KB
/
databaseconnect.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
import MySQLdb
class Database:
def __init__(self,tname,dbname):
self.table_name=tname
self.db_name=dbname
def connect(self):
self.db=MySQLdb.connect(host="127.0.0.1",user="root",passwd="",db=str(self.db_name),unix_socket="opt/lampp/var/mysql/mysql.sock")
def insert(self,values):
sql="Insert into "+str(self.table_name)+" (links,keyword) values ('"+str(values[1])+"','"+str(values[2])+"');"
# print sql
cursor=self.db.cursor();
try:
cursor.execute(sql)
self.db.commit()
except:
print "error"
self.db.rollback()
def insert1(self,values):
sql="Insert into "+str(self.table_name)+" (username,link_name) values ('"+str(values[0])+"','"+str(values[1])+"');"
cursor=self.db.cursor();
try:
cursor.execute(sql)
self.db.commit()
except:
print ""
self.db.rollback()
def view(self,query):
sql="select * from "+str(self.table_name)+" where username='"+str(query[0])+"'and link_name='"+str(query[1])+"';"
#print sql
try:
cursor=self.db.cursor()
cursor.execute(sql)
results=cursor.fetchall()
# print results[1]
for row in results:
u=row[1]
return u
except:
return "error"
def close(self):
self.db.close()