forked from aadityakushwaha/DWCrawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
59 lines (51 loc) · 1.48 KB
/
database.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
import mysql.connector
# Define the MySQL database configuration
mysql_config = {
"host": "localhost",
"user": "root",
"password": "PASSWORD",
}
# Connect to the MySQL server
try:
db = mysql.connector.connect(**mysql_config)
except mysql.connector.Error as err:
print(f"Error connecting to MySQL server: {err}")
exit()
# Create a cursor to execute SQL queries
cursor = db.cursor()
# Create the database (if it doesn't exist already)
try:
cursor.execute("CREATE DATABASE IF NOT EXISTS Crawler")
except mysql.connector.Error as err:
print(f"Error creating MySQL database: {err}")
db.close()
exit()
# Connect to the Crawler database
mysql_config["database"] = "Crawler"
try:
db = mysql.connector.connect(**mysql_config)
except mysql.connector.Error as err:
print(f"Error connecting to MySQL database: {err}")
exit()
# Create a cursor to execute SQL queries
cursor = db.cursor()
# Create the onion_urls table (if it doesn't exist already)
try:
cursor.execute("""
CREATE TABLE IF NOT EXISTS onion_urls (
id INT AUTO_INCREMENT PRIMARY KEY,
url VARCHAR(255) NOT NULL UNIQUE,
title LONGTEXT,
keywords TEXT,
description TEXT,
content TEXT,
image_urls TEXT,
scraped BOOLEAN DEFAULT 0
)
""")
except mysql.connector.Error as err:
print(f"Error creating MySQL table: {err}")
db.close()
exit()
# Close the database connection
db.close()