67rt 发表于 2016-9-2 09:14:47

Python 对mysql数据库的操作

Python 对mysql数据库的操作

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
#!/usr/bin/python
#-*- coding: utf-8 -*-
import MySQLdb
class mysql:
    def __init__(self,sql,host='127.0.0.1',username='root',password='root',dbname='dbname'):
      self.username=username
      self.password=password
      self.dbname=dbname
      self.sql=sql
      mysql.db=MySQLdb.connect(self.host,self.username,self.password,self.dbname,charset="utf8")


    #查询操作
    def query(self):
      try:
            cursor=mysql.db.cursor()
            cursor.execute(self.sql)
            data=cursor.fetchall()
            return data
            mysql.db.close()
      except Exception as e:
            print e

    #插入操作
    def insert(self):
      try:
            cursor=mysql.db.cursor()
            cursor.execute(self.sql)
            mysql.db.commit()
            mysql.db.close()
            return 'ok'
      except Exception as e:
            print e

    #删除操作
    def delete(self):
      try:
            cursor=mysql.db.cursor()
            cursor.execute(self.sql)
            mysql.db.commit()
            mysql.db.close()
      except Exception as e:
            print e

    #修改操作
    def update(self):
      try:
            cursor=mysql.db.cursor()
            cursor.execute(self.sql)
            mysql.db.commit()
            mysql.db.close()
      except Exception as e:
            print e

if __name__=="__main__":
    pass






cgkmh 发表于 2016-10-20 18:37:38

这个不需要装相关mysql插件么?
页: [1]
查看完整版本: Python 对mysql数据库的操作