forked from yidao620c/python3-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql_db.py
More file actions
40 lines (31 loc) · 895 Bytes
/
mysql_db.py
File metadata and controls
40 lines (31 loc) · 895 Bytes
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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Topic: 利用MySQL Connector/Python 操作mysql数据库
网址:http://dev.mysql.com/doc/connector-python/en/index.html
"""
import mysql.connector
from mysql.connector import errorcode
def _connect():
config = {
'user': 'root',
'password': 'mysql',
'host': '192.168.203.95',
'database': 'hangxin',
'raise_on_warnings': True,
}
cnx = None
try:
cnx = mysql.connector.connect(**config)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
if cnx:
cnx.close()
return cnx
def _insert():
pass