-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysqlDB.py
More file actions
73 lines (59 loc) · 2.39 KB
/
mysqlDB.py
File metadata and controls
73 lines (59 loc) · 2.39 KB
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
import mysql.connector
from mysql.connector import errorcode
class mysqlDb:
def __init__(self, user, host, passwd, database):
self.user = user
self.host = host
self.passwd = passwd
self.database = database
def connectMysql(self):
try:
cnx = mysql.connector.connect(user = self.user,
host = self.host,
password = self.passwd,
#database = self.database,不指定数据库名称
raise_on_warnings = True,
)
return cnx
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Somethin is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
return None
def connectDatabase(self):
try:
cnx = mysql.connector.connect(user = self.user,
host = self.host,
password = self.passwd,
database = self.database,
raise_on_warnings = True,
)
return cnx
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Somethin is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
return None
def createDatabase(self):
cnx = self.connectMysql()
cursor = cnx.cursor()
#sql = "create database {} default character set 'utf-8'".format(self.database)
cursor.execute("CREATE DATABASE {} DEFAULT CHARACTER SET 'UTF8MB4'".format(self.database))
cursor.close()
cnx.close()
def createTable(self, sql):
pass
def insert(self, sql, *params):
pass
def update(self, sql, *params):
pass
def delete(self, sql, *params):
pass