diff --git a/add_random_users.py b/add_random_users.py new file mode 100644 index 0000000..ca8c2f6 --- /dev/null +++ b/add_random_users.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + +import json +import random +import sqlite3 +import sys + +DATABASE = 'db.sqlite3' + + +def main(): + conn = sqlite3.connect(DATABASE) + conn.row_factory = sqlite3.Row + cursor = conn.cursor() + output = cursor.execute( + 'SELECT user_id FROM users ORDER BY user_id DESC').fetchall() + existing_ids = [str(dict(row)['user_id']) for row in output] + + if len(sys.argv) < 2: + n = 10 + else: + n = int(sys.argv[1]) + + with open('/usr/share/dict/names', 'r') as name_file: + names = [row.strip('\n') for row in name_file] + + for _ in range(n): + user_id = '1' + while user_id in existing_ids: + user_id = str(random.randint(1, 1000000)) + existing_ids.append(user_id) + + username = random.choice(names) + + score = str(random.randint(0, 1000)) + + query = \ + 'INSERT INTO users (user_id, username, score) VALUES ({}, \'{}\', {})'\ + .format(user_id, username, score) + + print(query) + cursor.execute(query) + + conn.commit() + conn.close() + + +if __name__ == '__main__': + main() diff --git a/db.sqlite3 b/db.sqlite3 index b38e53f..80976ba 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ