-
Notifications
You must be signed in to change notification settings - Fork 0
/
mssql-rdg.py
75 lines (59 loc) · 2.02 KB
/
mssql-rdg.py
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
72
73
74
75
import pyodbc
import random
import string
from faker import Faker
# suite_type, stay_in_hours
def mass_update_resort_reservation(cursor):
sql = '''
insert novapark.resort_reservation
(
suite_type,
stay_in_hours
)
values
(?, ?)
'''
SERVER = 'novapark.database.windows.net'
DATABASE = 'novapark_2023-11-07T21-04Z'
USERNAME = 'admin-08'
PASSWORD = 'password@123'
connectionString = f'DRIVER={{ODBC Driver 18 for SQL Server}};SERVER={SERVER};DATABASE={DATABASE};UID={USERNAME};PWD={PASSWORD}'
def mass_insert_visitor(cursor, n):
query = '''
insert into novapark.visitor
(
first_name, last_name, ticket_no, phone, is_present,
age, num_of_visitations, gender, income
) values (
?, ?, ?, ?, ?, ?, ?, ?, ?
)
'''
for _ in range(n):
first_name = fake.first_name()
last_name = fake.last_name()
ticket_no = generate_random_letters(7)
phone = generate_random_code(10)
is_present = random.choice([0, 1])
age = random.randint(9, 82)
num_of_visitations = random.randint(1, 11)
gender = random.choice([0, 1])
income = random.uniform(52081,1554674)
cursor.execute(query,(first_name,last_name,ticket_no,phone,is_present,age,num_of_visitations,gender,income))
def generate_random_letters(length):
letters = string.ascii_letters # Contains 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
return ''.join(random.choice(letters) for _ in range(length))
def generate_random_code(length):
letters = string.digits
return ''.join(random.choice(letters) for _ in range(length))
n = 100
fake = Faker()
try:
conn = pyodbc.connect(connectionString)
cursor = conn.cursor()
mass_insert_visitor(cursor, n)
print(f'Successfully inserted {n} fake records into the database.')
conn.commit()
cursor.close()
conn.close()
except pyodbc.Error as e:
print(f"Error connecting to the database: {e}")