-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdbcontroller.py
More file actions
56 lines (43 loc) · 1.47 KB
/
dbcontroller.py
File metadata and controls
56 lines (43 loc) · 1.47 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
import MySQLdb
try:
import sqlite3
except ImportError:
print("sqlite3 is not installed. You can only use MySQL.")
def sqlite_connect_db(dbname):
conn = sqlite3.connect(dbname)
cur = conn.cursor()
return conn, cur
def sqlite_create_table(conn, cur):
print("creating table...")
cur.execute("""CREATE TABLE IF NOT EXISTS distance (
id INTEGER PRIMARY KEY AUTOINCREMENT,
macaddr TEXT,
pwr INTEGER,
distance INTEGER,
rpimac TEXT
)""")
conn.commit()
def sqlite_insert_data(conn, cur, data):
cur.execute("""
INSERT INTO distance VALUES(NULL,?,?,?,?)
""", (data["MAC"], data["PWR"], data["DIST"], data["RPI_MAC"]))
conn.commit()
def mysql_connect(host, user, passwd, db, charset='utf8'):
conn = MySQLdb.connect(host, user, passwd, db, charset=charset)
cur = conn.cursor()
return conn, cur
def mysql_create_table(conn, cur):
cur.execute("""CREATE TABLE IF NOT EXISTS distance(
id INT NOT NULL AUTO_INCREMENT,
macaddr VARCHAR(128),
pwr INTEGER,
distance INTEGER,
rpimac VARCHAR(128),
PRIMARY KEY (id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4""")
conn.commit()
def mysql_insert_data(conn, cur, data):
cur.execute("""
INSERT INTO distance VALUES (NULL, %s, %s, %s, %s)
""", (data["MAC"], data["PWR"], data["DIST"], data["RPI_MAC"]))
conn.commit()