Skip to content

Commit 84bdc94

Browse files
authored
Merge pull request #114 from CengizhanBayram/master
# add DataConnect database browser
2 parents 860591c + 12454d4 commit 84bdc94

File tree

9 files changed

+1360
-0
lines changed

9 files changed

+1360
-0
lines changed

Projects/DataConnect-/flask_web.py

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
from flask import Flask, render_template, request, redirect, url_for, flash
2+
import sqlite3
3+
import time
4+
5+
app = Flask(__name__)
6+
app.secret_key = 'your_secret_key' # Flash mesajları için gerekli
7+
8+
def get_db_connection():
9+
10+
conn = sqlite3.connect('x4sqlite1.db')
11+
conn.row_factory = sqlite3.Row
12+
return conn
13+
14+
15+
@app.route('/')
16+
def index():
17+
conn = get_db_connection()
18+
tables = conn.execute("SELECT name FROM sqlite_master WHERE type='table';").fetchall()
19+
conn.close()
20+
return render_template('index.html', tables=[table['name'] for table in tables])
21+
22+
@app.route('/table/<table_name>')
23+
def select_table(table_name):
24+
conn = get_db_connection()
25+
data = conn.execute(f'SELECT *, rowid as id FROM {table_name}').fetchall() # rowid as id eklendi
26+
columns = [column[1] for column in conn.execute(f'PRAGMA table_info({table_name})').fetchall()]
27+
columns.append('id') # id sütununu ekle
28+
conn.close()
29+
return render_template('table.html', table_name=table_name, data=data, columns=columns)
30+
31+
@app.route('/table/<table_name>/add', methods=['GET', 'POST'])
32+
def add_data(table_name):
33+
conn = get_db_connection()
34+
columns = [column[1] for column in conn.execute(f'PRAGMA table_info({table_name})').fetchall()]
35+
if request.method == 'POST':
36+
values = [request.form.get(column) for column in columns]
37+
placeholders = ', '.join(['?'] * len(columns))
38+
39+
try:
40+
conn.execute(f'INSERT INTO {table_name} ({", ".join(columns)}) VALUES ({placeholders})', values)
41+
conn.commit()
42+
except sqlite3.IntegrityError as e:
43+
if 'UNIQUE constraint failed' in str(e):
44+
conn.close()
45+
flash("Error: This operation cannot be completed because it violates a unique constraint.", "error")
46+
return redirect(url_for('select_table', table_name=table_name))
47+
else:
48+
raise
49+
conn.close()
50+
return redirect(url_for('select_table', table_name=table_name))
51+
conn.close()
52+
return render_template('add_data.html', table_name=table_name, columns=columns)
53+
54+
@app.route('/table/<table_name>/delete/<int:id>', methods=['POST'])
55+
def delete_data(table_name, id):
56+
conn = get_db_connection()
57+
conn.execute(f'DELETE FROM {table_name} WHERE rowid = ?', (id,))
58+
conn.commit()
59+
conn.close()
60+
return redirect(url_for('select_table', table_name=table_name))
61+
62+
@app.route('/table/<table_name>/update/<int:id>', methods=['GET', 'POST'])
63+
def update_data(table_name, id):
64+
conn = get_db_connection()
65+
columns = [column[1] for column in conn.execute(f'PRAGMA table_info({table_name})').fetchall()]
66+
if request.method == 'POST':
67+
values = [request.form.get(column) for column in columns]
68+
set_clause = ', '.join([f'{column} = ?' for column in columns])
69+
values.append(id)
70+
71+
try:
72+
conn.execute(f'UPDATE {table_name} SET {set_clause} WHERE rowid = ?', values)
73+
conn.commit()
74+
except sqlite3.IntegrityError as e:
75+
if 'UNIQUE constraint failed' in str(e):
76+
conn.close()
77+
flash("Error: This operation cannot be completed because it violates a unique constraint.", "error")
78+
return redirect(url_for('select_table', table_name=table_name))
79+
else:
80+
raise
81+
82+
conn.close()
83+
return redirect(url_for('select_table', table_name=table_name))
84+
85+
row = conn.execute(f'SELECT *, rowid as id FROM {table_name} WHERE rowid = ?', (id,)).fetchone() # rowid as id eklendi
86+
conn.close()
87+
return render_template('update_data.html', table_name=table_name, columns=columns, row=row)
88+
89+
@app.route('/create_table', methods=['GET', 'POST'])
90+
def create_table():
91+
if request.method == 'POST':
92+
table_name = request.form['table_name']
93+
columns = request.form['columns']
94+
conn = get_db_connection()
95+
try:
96+
conn.execute(f'CREATE TABLE {table_name} ({columns})')
97+
conn.commit()
98+
flash("Table created successfully.", "success")
99+
except sqlite3.Error as e:
100+
flash(f"Error creating table: {e}", "error")
101+
finally:
102+
conn.close()
103+
return redirect(url_for('index'))
104+
return render_template('create_table.html')
105+
106+
@app.route('/delete_table/<table_name>', methods=['POST'])
107+
def delete_table(table_name):
108+
conn = get_db_connection()
109+
try:
110+
conn.execute(f'DROP TABLE {table_name}')
111+
conn.commit()
112+
flash("Table deleted successfully.", "success")
113+
except sqlite3.Error as e:
114+
flash(f"Error deleting table: {e}", "error")
115+
finally:
116+
conn.close()
117+
return redirect(url_for('index'))
118+
119+
@app.route('/query', methods=['GET', 'POST'])
120+
def query():
121+
if request.method == 'POST':
122+
query = request.form['query']
123+
conn = get_db_connection()
124+
try:
125+
result = conn.execute(query).fetchall()
126+
columns = result[0].keys() if result else []
127+
conn.commit()
128+
flash("Query executed successfully.", "success")
129+
except sqlite3.Error as e:
130+
flash(f"Error executing query: {e}", "error")
131+
result = []
132+
columns = []
133+
finally:
134+
conn.close()
135+
return render_template('query.html', query=query, result=result, columns=columns)
136+
return render_template('query.html', query='', result=[], columns=[])
137+
138+
if __name__ == '__main__':
139+
app.run(debug=True)

Projects/DataConnect-/icon.png

20.4 KB
Loading
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Add Data to {{ table_name }}</title>
7+
<style>
8+
body {
9+
font-family: Arial, sans-serif;
10+
background-color: #f0f0f0;
11+
margin: 0;
12+
display: flex;
13+
height: 100vh;
14+
}
15+
.sidebar {
16+
width: 250px;
17+
background: #333;
18+
color: white;
19+
padding: 20px;
20+
box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
21+
}
22+
.sidebar h1 {
23+
text-align: center;
24+
}
25+
.sidebar ul {
26+
list-style-type: none;
27+
padding: 0;
28+
}
29+
.sidebar li {
30+
padding: 10px;
31+
background: #444;
32+
margin-bottom: 5px;
33+
text-align: center;
34+
}
35+
.sidebar li a {
36+
color: white;
37+
text-decoration: none;
38+
}
39+
.sidebar li:hover {
40+
background: #555;
41+
}
42+
.content {
43+
flex: 1;
44+
padding: 20px;
45+
}
46+
.form-group {
47+
margin-bottom: 15px;
48+
}
49+
.form-group label {
50+
display: block;
51+
margin-bottom: 5px;
52+
}
53+
.form-group input {
54+
width: 100%;
55+
padding: 10px;
56+
border: 1px solid #ccc;
57+
}
58+
.btn {
59+
display: inline-block;
60+
padding: 10px 20px;
61+
background: #007bff;
62+
color: white;
63+
text-align: center;
64+
text-decoration: none;
65+
margin-top: 20px;
66+
cursor: pointer;
67+
}
68+
.btn:hover {
69+
background: #0056b3;
70+
}
71+
.back-btn {
72+
background: #28a745;
73+
}
74+
.back-btn:hover {
75+
background: #218838;
76+
}
77+
</style>
78+
</head>
79+
<body>
80+
<div class="sidebar">
81+
<h1>SQLite Database Browser</h1>
82+
<h2>Tables</h2>
83+
<ul>
84+
{% for table in tables %}
85+
<li><a href="{{ url_for('select_table', table_name=table) }}">{{ table }}</a></li>
86+
{% endfor %}
87+
</ul>
88+
</div>
89+
<div class="content">
90+
<h1>Add Data to {{ table_name }}</h1>
91+
<form method="POST">
92+
{% for column in columns %}
93+
<div class="form-group">
94+
<label for="{{ column }}">{{ column }}</label>
95+
<input type="text" id="{{ column }}" name="{{ column }}">
96+
</div>
97+
{% endfor %}
98+
<button type="submit" class="btn">Add Data</button>
99+
</form>
100+
<a href="{{ url_for('select_table', table_name=table_name) }}" class="btn">Back to Table</a>
101+
<a href="{{ url_for('index') }}" class="btn back-btn">Back to Main</a>
102+
</div>
103+
</body>
104+
</html>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Create Table</title>
7+
<style>
8+
body {
9+
font-family: Arial, sans-serif;
10+
background-color: #f0f0f0;
11+
margin: 0;
12+
display: flex;
13+
height: 100vh;
14+
}
15+
.sidebar {
16+
width: 250px;
17+
background: #333;
18+
color: white;
19+
padding: 20px;
20+
box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
21+
}
22+
.sidebar h1 {
23+
text-align: center;
24+
}
25+
.sidebar ul {
26+
list-style-type: none;
27+
padding: 0;
28+
}
29+
.sidebar li {
30+
padding: 10px;
31+
background: #444;
32+
margin-bottom: 5px;
33+
text-align: center;
34+
}
35+
.sidebar li a {
36+
color: white;
37+
text-decoration: none;
38+
}
39+
.sidebar li:hover {
40+
background: #555;
41+
}
42+
.content {
43+
flex: 1;
44+
padding: 20px;
45+
}
46+
.form-group {
47+
margin-bottom: 15px;
48+
}
49+
.form-group label {
50+
display: block;
51+
margin-bottom: 5px;
52+
}
53+
.form-group input {
54+
width: 100%;
55+
padding: 10px;
56+
border: 1px solid #ccc;
57+
}
58+
.btn {
59+
display: inline-block;
60+
padding: 10px 20px;
61+
background: #007bff;
62+
color: white;
63+
text-align: center;
64+
text-decoration: none;
65+
margin-top: 20px;
66+
cursor: pointer;
67+
}
68+
.btn:hover {
69+
background: #0056b3;
70+
}
71+
.back-btn {
72+
background: #28a745;
73+
}
74+
.back-btn:hover {
75+
background: #218838;
76+
}
77+
</style>
78+
</head>
79+
<body>
80+
<div class="sidebar">
81+
<h1>SQLite Database Browser</h1>
82+
<h2>Tables</h2>
83+
<ul>
84+
{% for table in tables %}
85+
<li><a href="{{ url_for('select_table', table_name=table) }}">{{ table }}</a></li>
86+
{% endfor %}
87+
</ul>
88+
</div>
89+
<div class="content">
90+
<h1>Create Table</h1>
91+
<form method="POST">
92+
<div class="form-group">
93+
<label for="table_name">Table Name</label>
94+
<input type="text" id="table_name" name="table_name" required>
95+
</div>
96+
<div class="form-group">
97+
<label for="columns">Columns (e.g., id INTEGER PRIMARY KEY, name TEXT)</label>
98+
<input type="text" id="columns" name="columns" required>
99+
</div>
100+
<button type="submit" class="btn">Create Table</button>
101+
</form>
102+
<a href="{{ url_for('index') }}" class="btn back-btn">Back to Main</a>
103+
</div>
104+
</body>
105+
</html>

0 commit comments

Comments
 (0)