python数据库连接
import os
import time
import pymysql
import sys
class Myclass(object):
def __init__(self):
try:
self.db=pymysql.connect(host="localhost",port=3306,user="root",passwd="ZJM199933",db="stu",charset="utf8")
self.cursor=self.db.cursor()
self.db.autocommit(1)
print("成功")
except:
print("失败")
def insert(self):
sql="insert into student (id,name,age) values(2,'老王',21),(3,'赵六',23)";
self.cursor.execute(sql);
self.db.commit();
def find(self,name):
sql="select * from student where name='"+name+"'";
self.cursor.execute(sql);
def delete(self,name):
sql="delect from student where name='"+name+"'";
self.cursor.execute(sql);
self.db.commit();
def __del__(self):
self.db.close()
if __name__=='__main__':
testcls=Myclass()
# testcls.find(name='张三')
testcls.insert()