-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectDatabase.py
More file actions
31 lines (21 loc) · 812 Bytes
/
connectDatabase.py
File metadata and controls
31 lines (21 loc) · 812 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
#Importing pymysql, note this is a comment in Python (a number symbol or a hashtag :))
import pymysql
#Setting up connection variables.
host = 'localhost'
username= 'root'
password=''
dbName = 'week8'
#Connecting
conn = pymysql.connect(host, username, password, dbName)
#A database cursor is a control structure that enables traversal over the records in a database.
cursor = conn.cursor()
#SQL query we want to execute.
query = "SELECT * FROM users"
#Executing the query.
cursor.execute(query)
# data is a data structure that can hold the result of the query. cursor.fetchall() will return the entire table, cursor.fetchone() will return the first record (row).
data = cursor.fetchall()
#Printing to see the output.
print (data)
#Closing at the connection.
conn.close()