Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
be-sgbd-library
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Olivier Hugo
be-sgbd-library
Commits
647453ca
Unverified
Commit
647453ca
authored
1 year ago
by
Jangberry (Nomad-Debian)
Browse files
Options
Downloads
Patches
Plain Diff
Avancement
parent
2c10b43d
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
.gitignore
+2
-0
2 additions, 0 deletions
.gitignore
BE_SGBD_LIBRARY.ddl
+1
-0
1 addition, 0 deletions
BE_SGBD_LIBRARY.ddl
fill.ipynb
+114
-0
114 additions, 0 deletions
fill.ipynb
with
117 additions
and
0 deletions
.gitignore
0 → 100644
+
2
−
0
View file @
647453ca
.idea
.venv
\ No newline at end of file
This diff is collapsed.
Click to expand it.
BE_SGBD_LIBRARY.ddl
+
1
−
0
View file @
647453ca
...
...
@@ -12,6 +12,7 @@
-- Database Section
-- ________________
DROP DATABASE IF EXISTS MAIN;
create database MAIN;
USE MAIN;
...
...
This diff is collapsed.
Click to expand it.
fill.ipynb
0 → 100644
+
114
−
0
View file @
647453ca
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# SGBD Library\n",
"\n",
"## Usage\n",
"\n",
"1. Run a SQL server (for instance `docker run --name sgdb --env MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=1 -p 3306:3306 mariadb:latest`)\n",
"\n",
"2. *You may need to* install the following package: `apt install libmariadb-dev`\n",
"\n",
"3. Then, run the following code:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: mariadb in ./.venv/lib/python3.10/site-packages (1.1.8)\n",
"Requirement already satisfied: packaging in ./.venv/lib/python3.10/site-packages (from mariadb) (23.2)\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install mariadb\n",
"import mariadb"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"mariaDB = mariadb.connect(\n",
" user=\"root\",\n",
" host=\"172.17.0.2\", # IP of the container\n",
" # host=\"172.0.0.1\",\n",
" port=3306,\n",
")\n",
"\n",
"cur = mariaDB.cursor()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This code will apply the dll:"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"def exec_sql_file(cursor, sql_file):\n",
" statement = \"\"\n",
"\n",
" for line in open(sql_file):\n",
" if line.startswith('--'):\n",
" continue\n",
" if not line.strip().endswith(';'):\n",
" statement = statement + line\n",
" else: # when you get a line ending in ';' then exec statement and reset for next statement\n",
" statement = statement + line\n",
" try:\n",
" cursor.execute(statement)\n",
" except (mariadb.OperationalError, mariadb.ProgrammingError) as e:\n",
" print(\"\\n[WARN] MySQLError during execute statement \\n\\tArgs: '%s'\" % (str(e.args)))\n",
"\n",
" statement = \"\"\n",
"\n",
"exec_sql_file(cur, \"BE_SGBD_LIBRARY.ddl\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:markdown id: tags:
# SGBD Library
## Usage
1.
Run a SQL server (for instance
`docker run --name sgdb --env MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=1 -p 3306:3306 mariadb:latest`
)
2.
*You may need to*
install the following package:
`apt install libmariadb-dev`
3.
Then, run the following code:
%% Cell type:code id: tags:
```
python
%
pip
install
mariadb
import
mariadb
```
%% Output
Requirement already satisfied: mariadb in ./.venv/lib/python3.10/site-packages (1.1.8)
Requirement already satisfied: packaging in ./.venv/lib/python3.10/site-packages (from mariadb) (23.2)
Note: you may need to restart the kernel to use updated packages.
%% Cell type:code id: tags:
```
python
mariaDB
=
mariadb
.
connect
(
user
=
"
root
"
,
host
=
"
172.17.0.2
"
,
# IP of the container
# host="172.0.0.1",
port
=
3306
,
)
cur
=
mariaDB
.
cursor
()
```
%% Cell type:markdown id: tags:
This code will apply the dll:
%% Cell type:code id: tags:
```
python
def
exec_sql_file
(
cursor
,
sql_file
):
statement
=
""
for
line
in
open
(
sql_file
):
if
line
.
startswith
(
'
--
'
):
continue
if
not
line
.
strip
().
endswith
(
'
;
'
):
statement
=
statement
+
line
else
:
# when you get a line ending in ';' then exec statement and reset for next statement
statement
=
statement
+
line
try
:
cursor
.
execute
(
statement
)
except
(
mariadb
.
OperationalError
,
mariadb
.
ProgrammingError
)
as
e
:
print
(
"
\n
[WARN] MySQLError during execute statement
\n\t
Args:
'
%s
'"
%
(
str
(
e
.
args
)))
statement
=
""
exec_sql_file
(
cur
,
"
BE_SGBD_LIBRARY.ddl
"
)
```
%% Cell type:markdown id: tags:
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment